Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 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 12463d8b 2019-12-13 stsp #include <fcntl.h>
27 12ce7a6c 2019-08-12 stsp #include <limits.h>
28 5c860e29 2018-03-12 stsp #include <locale.h>
29 818c7501 2019-07-11 stsp #include <ctype.h>
30 99437157 2018-11-11 stsp #include <signal.h>
31 5c860e29 2018-03-12 stsp #include <stdio.h>
32 5c860e29 2018-03-12 stsp #include <stdlib.h>
33 5c860e29 2018-03-12 stsp #include <string.h>
34 5c860e29 2018-03-12 stsp #include <unistd.h>
35 c09a553d 2018-03-12 stsp #include <libgen.h>
36 c0768b0f 2018-06-10 stsp #include <time.h>
37 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 6841bf13 2019-11-29 kn #include <regex.h>
39 83cd27f8 2020-01-13 stsp #include <getopt.h>
40 5c860e29 2018-03-12 stsp
41 53ccebc2 2019-07-30 stsp #include "got_version.h"
42 f42b1b34 2018-03-12 stsp #include "got_error.h"
43 f42b1b34 2018-03-12 stsp #include "got_object.h"
44 5261c201 2018-04-01 stsp #include "got_reference.h"
45 f42b1b34 2018-03-12 stsp #include "got_repository.h"
46 1dd54920 2019-05-11 stsp #include "got_path.h"
47 e6209546 2019-08-22 stsp #include "got_cancel.h"
48 c09a553d 2018-03-12 stsp #include "got_worktree.h"
49 79109fed 2018-03-27 stsp #include "got_diff.h"
50 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
51 404c43c4 2018-06-21 stsp #include "got_blame.h"
52 63219cd2 2019-01-04 stsp #include "got_privsep.h"
53 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
54 5c860e29 2018-03-12 stsp
55 5c860e29 2018-03-12 stsp #ifndef nitems
56 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 5c860e29 2018-03-12 stsp #endif
58 99437157 2018-11-11 stsp
59 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
60 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
61 99437157 2018-11-11 stsp
62 99437157 2018-11-11 stsp static void
63 99437157 2018-11-11 stsp catch_sigint(int signo)
64 99437157 2018-11-11 stsp {
65 99437157 2018-11-11 stsp sigint_received = 1;
66 99437157 2018-11-11 stsp }
67 99437157 2018-11-11 stsp
68 99437157 2018-11-11 stsp static void
69 99437157 2018-11-11 stsp catch_sigpipe(int signo)
70 99437157 2018-11-11 stsp {
71 99437157 2018-11-11 stsp sigpipe_received = 1;
72 99437157 2018-11-11 stsp }
73 5c860e29 2018-03-12 stsp
74 99437157 2018-11-11 stsp
75 8cfb4057 2019-07-09 stsp struct got_cmd {
76 5c860e29 2018-03-12 stsp const char *cmd_name;
77 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
78 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
79 97b3a7be 2019-07-09 stsp const char *cmd_alias;
80 5c860e29 2018-03-12 stsp };
81 5c860e29 2018-03-12 stsp
82 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
83 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
84 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
86 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
89 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
90 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
91 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
92 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
93 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
94 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
95 d00136be 2019-03-26 stsp __dead static void usage_add(void);
96 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
97 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
98 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
99 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
100 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
101 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
102 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
103 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
104 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
105 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
106 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
107 5c860e29 2018-03-12 stsp
108 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
109 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
110 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
111 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
112 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
113 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
114 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
115 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
116 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
117 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
118 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
119 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
120 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
121 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
122 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
123 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
124 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
125 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
126 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
127 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
128 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
129 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
130 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
131 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
132 5c860e29 2018-03-12 stsp
133 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
134 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
135 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
136 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
137 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
138 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
139 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
140 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
141 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
142 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
143 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
144 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
145 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
146 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
147 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
148 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
149 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
150 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
152 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
153 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
154 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
155 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
156 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
157 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
158 5c860e29 2018-03-12 stsp };
159 ce5b7c56 2019-07-09 stsp
160 ce5b7c56 2019-07-09 stsp static void
161 ce5b7c56 2019-07-09 stsp list_commands(void)
162 ce5b7c56 2019-07-09 stsp {
163 ce5b7c56 2019-07-09 stsp int i;
164 ce5b7c56 2019-07-09 stsp
165 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
166 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
167 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
168 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
169 ce5b7c56 2019-07-09 stsp }
170 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
171 ce5b7c56 2019-07-09 stsp }
172 5c860e29 2018-03-12 stsp
173 5c860e29 2018-03-12 stsp int
174 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
175 5c860e29 2018-03-12 stsp {
176 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
177 5c860e29 2018-03-12 stsp unsigned int i;
178 5c860e29 2018-03-12 stsp int ch;
179 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
180 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
181 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
182 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
183 83cd27f8 2020-01-13 stsp };
184 5c860e29 2018-03-12 stsp
185 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
186 5c860e29 2018-03-12 stsp
187 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
188 5c860e29 2018-03-12 stsp switch (ch) {
189 1b6b95a8 2018-03-12 stsp case 'h':
190 1b6b95a8 2018-03-12 stsp hflag = 1;
191 53ccebc2 2019-07-30 stsp break;
192 53ccebc2 2019-07-30 stsp case 'V':
193 53ccebc2 2019-07-30 stsp Vflag = 1;
194 1b6b95a8 2018-03-12 stsp break;
195 5c860e29 2018-03-12 stsp default:
196 ce5b7c56 2019-07-09 stsp usage(hflag);
197 5c860e29 2018-03-12 stsp /* NOTREACHED */
198 5c860e29 2018-03-12 stsp }
199 5c860e29 2018-03-12 stsp }
200 5c860e29 2018-03-12 stsp
201 5c860e29 2018-03-12 stsp argc -= optind;
202 5c860e29 2018-03-12 stsp argv += optind;
203 1e70621d 2018-03-27 stsp optind = 0;
204 53ccebc2 2019-07-30 stsp
205 53ccebc2 2019-07-30 stsp if (Vflag) {
206 53ccebc2 2019-07-30 stsp got_version_print_str();
207 53ccebc2 2019-07-30 stsp return 1;
208 53ccebc2 2019-07-30 stsp }
209 5c860e29 2018-03-12 stsp
210 5c860e29 2018-03-12 stsp if (argc <= 0)
211 ce5b7c56 2019-07-09 stsp usage(hflag);
212 5c860e29 2018-03-12 stsp
213 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
214 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
215 99437157 2018-11-11 stsp
216 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
217 d7d4f210 2018-03-12 stsp const struct got_error *error;
218 d7d4f210 2018-03-12 stsp
219 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
220 5c860e29 2018-03-12 stsp
221 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
223 5c860e29 2018-03-12 stsp continue;
224 5c860e29 2018-03-12 stsp
225 1b6b95a8 2018-03-12 stsp if (hflag)
226 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
227 1b6b95a8 2018-03-12 stsp
228 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
229 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
230 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
231 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
232 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
233 70015d7a 2019-11-08 stsp !(sigint_received &&
234 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
236 d7d4f210 2018-03-12 stsp return 1;
237 d7d4f210 2018-03-12 stsp }
238 d7d4f210 2018-03-12 stsp
239 d7d4f210 2018-03-12 stsp return 0;
240 5c860e29 2018-03-12 stsp }
241 5c860e29 2018-03-12 stsp
242 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
243 ce5b7c56 2019-07-09 stsp list_commands();
244 5c860e29 2018-03-12 stsp return 1;
245 5c860e29 2018-03-12 stsp }
246 5c860e29 2018-03-12 stsp
247 4ed7e80c 2018-05-20 stsp __dead static void
248 ce5b7c56 2019-07-09 stsp usage(int hflag)
249 5c860e29 2018-03-12 stsp {
250 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
251 53ccebc2 2019-07-30 stsp getprogname());
252 ce5b7c56 2019-07-09 stsp if (hflag)
253 ce5b7c56 2019-07-09 stsp list_commands();
254 5c860e29 2018-03-12 stsp exit(1);
255 5c860e29 2018-03-12 stsp }
256 5c860e29 2018-03-12 stsp
257 0266afb7 2019-01-04 stsp static const struct got_error *
258 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
259 e2ba3d07 2019-05-13 stsp {
260 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
261 e2ba3d07 2019-05-13 stsp const char *editor;
262 8920fa04 2019-08-18 stsp
263 8920fa04 2019-08-18 stsp *abspath = NULL;
264 e2ba3d07 2019-05-13 stsp
265 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
266 e2ba3d07 2019-05-13 stsp if (editor == NULL)
267 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
268 e2ba3d07 2019-05-13 stsp
269 0ee7065d 2019-05-13 stsp if (editor) {
270 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
271 0ee7065d 2019-05-13 stsp if (err)
272 0ee7065d 2019-05-13 stsp return err;
273 0ee7065d 2019-05-13 stsp }
274 e2ba3d07 2019-05-13 stsp
275 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
276 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
277 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
278 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
279 0ee7065d 2019-05-13 stsp }
280 0ee7065d 2019-05-13 stsp
281 e2ba3d07 2019-05-13 stsp return NULL;
282 e2ba3d07 2019-05-13 stsp }
283 e2ba3d07 2019-05-13 stsp
284 e2ba3d07 2019-05-13 stsp static const struct got_error *
285 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
286 c530dc23 2019-07-23 stsp const char *worktree_path)
287 0266afb7 2019-01-04 stsp {
288 163ce85a 2019-05-13 stsp const struct got_error *err;
289 0266afb7 2019-01-04 stsp
290 37c06ea4 2019-07-15 stsp #ifdef PROFILE
291 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
292 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
293 37c06ea4 2019-07-15 stsp #endif
294 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
296 0266afb7 2019-01-04 stsp
297 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
299 0266afb7 2019-01-04 stsp
300 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
301 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
302 0266afb7 2019-01-04 stsp
303 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
304 163ce85a 2019-05-13 stsp if (err != NULL)
305 163ce85a 2019-05-13 stsp return err;
306 0266afb7 2019-01-04 stsp
307 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
308 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
309 0266afb7 2019-01-04 stsp
310 0266afb7 2019-01-04 stsp return NULL;
311 2c7829a4 2019-06-17 stsp }
312 2c7829a4 2019-06-17 stsp
313 2c7829a4 2019-06-17 stsp __dead static void
314 2c7829a4 2019-06-17 stsp usage_init(void)
315 2c7829a4 2019-06-17 stsp {
316 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
317 2c7829a4 2019-06-17 stsp exit(1);
318 2c7829a4 2019-06-17 stsp }
319 2c7829a4 2019-06-17 stsp
320 2c7829a4 2019-06-17 stsp static const struct got_error *
321 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
322 2c7829a4 2019-06-17 stsp {
323 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
324 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
325 2c7829a4 2019-06-17 stsp int ch;
326 2c7829a4 2019-06-17 stsp
327 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
328 2c7829a4 2019-06-17 stsp switch (ch) {
329 2c7829a4 2019-06-17 stsp default:
330 2c7829a4 2019-06-17 stsp usage_init();
331 2c7829a4 2019-06-17 stsp /* NOTREACHED */
332 2c7829a4 2019-06-17 stsp }
333 2c7829a4 2019-06-17 stsp }
334 2c7829a4 2019-06-17 stsp
335 2c7829a4 2019-06-17 stsp argc -= optind;
336 2c7829a4 2019-06-17 stsp argv += optind;
337 2c7829a4 2019-06-17 stsp
338 2c7829a4 2019-06-17 stsp #ifndef PROFILE
339 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 2c7829a4 2019-06-17 stsp err(1, "pledge");
341 2c7829a4 2019-06-17 stsp #endif
342 2c7829a4 2019-06-17 stsp if (argc != 1)
343 bc20e173 2019-06-17 stsp usage_init();
344 2c7829a4 2019-06-17 stsp
345 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
346 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
347 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
348 2c7829a4 2019-06-17 stsp
349 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
350 2c7829a4 2019-06-17 stsp
351 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
352 2c7829a4 2019-06-17 stsp if (error &&
353 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
354 2c7829a4 2019-06-17 stsp goto done;
355 2c7829a4 2019-06-17 stsp
356 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
357 2c7829a4 2019-06-17 stsp if (error)
358 2c7829a4 2019-06-17 stsp goto done;
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
361 2c7829a4 2019-06-17 stsp if (error != NULL)
362 3ce1b845 2019-07-15 stsp goto done;
363 3ce1b845 2019-07-15 stsp
364 3ce1b845 2019-07-15 stsp done:
365 3ce1b845 2019-07-15 stsp free(repo_path);
366 3ce1b845 2019-07-15 stsp return error;
367 3ce1b845 2019-07-15 stsp }
368 3ce1b845 2019-07-15 stsp
369 3ce1b845 2019-07-15 stsp __dead static void
370 3ce1b845 2019-07-15 stsp usage_import(void)
371 3ce1b845 2019-07-15 stsp {
372 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
373 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
374 3ce1b845 2019-07-15 stsp exit(1);
375 3ce1b845 2019-07-15 stsp }
376 3ce1b845 2019-07-15 stsp
377 3ce1b845 2019-07-15 stsp int
378 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
379 3ce1b845 2019-07-15 stsp {
380 3ce1b845 2019-07-15 stsp pid_t pid;
381 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
382 3ce1b845 2019-07-15 stsp int st = -1;
383 3ce1b845 2019-07-15 stsp
384 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
385 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
386 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
387 3ce1b845 2019-07-15 stsp
388 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
389 3ce1b845 2019-07-15 stsp case -1:
390 3ce1b845 2019-07-15 stsp goto doneediting;
391 3ce1b845 2019-07-15 stsp case 0:
392 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
393 3ce1b845 2019-07-15 stsp _exit(127);
394 3ce1b845 2019-07-15 stsp }
395 3ce1b845 2019-07-15 stsp
396 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
397 3ce1b845 2019-07-15 stsp if (errno != EINTR)
398 3ce1b845 2019-07-15 stsp break;
399 3ce1b845 2019-07-15 stsp
400 3ce1b845 2019-07-15 stsp doneediting:
401 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
402 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
403 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
404 3ce1b845 2019-07-15 stsp
405 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
406 3ce1b845 2019-07-15 stsp errno = EINTR;
407 3ce1b845 2019-07-15 stsp return -1;
408 3ce1b845 2019-07-15 stsp }
409 3ce1b845 2019-07-15 stsp
410 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
411 3ce1b845 2019-07-15 stsp }
412 3ce1b845 2019-07-15 stsp
413 3ce1b845 2019-07-15 stsp static const struct got_error *
414 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
415 3ce1b845 2019-07-15 stsp const char *initial_content)
416 3ce1b845 2019-07-15 stsp {
417 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
418 3ce1b845 2019-07-15 stsp char buf[1024];
419 3ce1b845 2019-07-15 stsp struct stat st, st2;
420 3ce1b845 2019-07-15 stsp FILE *fp;
421 3ce1b845 2019-07-15 stsp int content_changed = 0;
422 3ce1b845 2019-07-15 stsp size_t len;
423 3ce1b845 2019-07-15 stsp
424 3ce1b845 2019-07-15 stsp *logmsg = NULL;
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
427 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
428 3ce1b845 2019-07-15 stsp
429 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
430 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
436 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
437 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
438 3ce1b845 2019-07-15 stsp
439 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
440 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
441 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
442 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
443 3ce1b845 2019-07-15 stsp len = 0;
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
446 3ce1b845 2019-07-15 stsp if (fp == NULL) {
447 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
448 3ce1b845 2019-07-15 stsp goto done;
449 3ce1b845 2019-07-15 stsp }
450 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
451 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
452 3ce1b845 2019-07-15 stsp content_changed = 1;
453 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
454 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
455 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
456 3ce1b845 2019-07-15 stsp }
457 3ce1b845 2019-07-15 stsp fclose(fp);
458 3ce1b845 2019-07-15 stsp
459 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
460 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
461 3ce1b845 2019-07-15 stsp len--;
462 3ce1b845 2019-07-15 stsp }
463 3ce1b845 2019-07-15 stsp
464 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
465 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
466 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
467 3ce1b845 2019-07-15 stsp done:
468 3ce1b845 2019-07-15 stsp if (err) {
469 3ce1b845 2019-07-15 stsp free(*logmsg);
470 3ce1b845 2019-07-15 stsp *logmsg = NULL;
471 3ce1b845 2019-07-15 stsp }
472 3ce1b845 2019-07-15 stsp return err;
473 3ce1b845 2019-07-15 stsp }
474 3ce1b845 2019-07-15 stsp
475 3ce1b845 2019-07-15 stsp static const struct got_error *
476 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
477 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
478 3ce1b845 2019-07-15 stsp {
479 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
480 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
481 3ce1b845 2019-07-15 stsp int fd;
482 3ce1b845 2019-07-15 stsp
483 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
484 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
485 3ce1b845 2019-07-15 stsp branch_name) == -1)
486 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
487 3ce1b845 2019-07-15 stsp
488 ef293bdd 2019-10-21 stsp err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
489 3ce1b845 2019-07-15 stsp if (err)
490 3ce1b845 2019-07-15 stsp goto done;
491 3ce1b845 2019-07-15 stsp
492 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
493 3ce1b845 2019-07-15 stsp close(fd);
494 3ce1b845 2019-07-15 stsp
495 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
496 3ce1b845 2019-07-15 stsp done:
497 3ce1b845 2019-07-15 stsp free(initial_content);
498 3ce1b845 2019-07-15 stsp return err;
499 3ce1b845 2019-07-15 stsp }
500 3ce1b845 2019-07-15 stsp
501 3ce1b845 2019-07-15 stsp static const struct got_error *
502 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
503 3ce1b845 2019-07-15 stsp {
504 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
505 3ce1b845 2019-07-15 stsp return NULL;
506 3ce1b845 2019-07-15 stsp }
507 3ce1b845 2019-07-15 stsp
508 3ce1b845 2019-07-15 stsp static const struct got_error *
509 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
510 84792843 2019-08-09 stsp {
511 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
512 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
513 84792843 2019-08-09 stsp
514 84792843 2019-08-09 stsp *author = NULL;
515 aba9c984 2019-09-08 stsp
516 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
517 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
518 c9956ddf 2019-09-08 stsp if (name && email) {
519 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
520 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
521 aba9c984 2019-09-08 stsp return NULL;
522 aba9c984 2019-09-08 stsp }
523 84792843 2019-08-09 stsp
524 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
525 84792843 2019-08-09 stsp if (got_author == NULL) {
526 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
527 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
528 c9956ddf 2019-09-08 stsp if (name && email) {
529 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
530 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
531 c9956ddf 2019-09-08 stsp return NULL;
532 c9956ddf 2019-09-08 stsp }
533 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
534 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
535 84792843 2019-08-09 stsp }
536 84792843 2019-08-09 stsp
537 aba9c984 2019-09-08 stsp *author = strdup(got_author);
538 aba9c984 2019-09-08 stsp if (*author == NULL)
539 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
540 84792843 2019-08-09 stsp
541 84792843 2019-08-09 stsp /*
542 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
543 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
544 84792843 2019-08-09 stsp */
545 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
546 84792843 2019-08-09 stsp got_author++;
547 aba9c984 2019-09-08 stsp if (*got_author != '<') {
548 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
549 aba9c984 2019-09-08 stsp goto done;
550 aba9c984 2019-09-08 stsp }
551 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
552 84792843 2019-08-09 stsp got_author++;
553 aba9c984 2019-09-08 stsp if (*got_author != '@') {
554 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 aba9c984 2019-09-08 stsp goto done;
556 aba9c984 2019-09-08 stsp }
557 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
558 84792843 2019-08-09 stsp got_author++;
559 84792843 2019-08-09 stsp if (*got_author != '>')
560 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
561 aba9c984 2019-09-08 stsp done:
562 aba9c984 2019-09-08 stsp if (err) {
563 aba9c984 2019-09-08 stsp free(*author);
564 aba9c984 2019-09-08 stsp *author = NULL;
565 aba9c984 2019-09-08 stsp }
566 aba9c984 2019-09-08 stsp return err;
567 c9956ddf 2019-09-08 stsp }
568 c9956ddf 2019-09-08 stsp
569 c9956ddf 2019-09-08 stsp static const struct got_error *
570 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
571 c9956ddf 2019-09-08 stsp {
572 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
573 c9956ddf 2019-09-08 stsp
574 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
575 c9956ddf 2019-09-08 stsp if (homedir) {
576 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
577 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
578 c9956ddf 2019-09-08 stsp
579 c9956ddf 2019-09-08 stsp }
580 c9956ddf 2019-09-08 stsp return NULL;
581 84792843 2019-08-09 stsp }
582 84792843 2019-08-09 stsp
583 84792843 2019-08-09 stsp static const struct got_error *
584 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
585 3ce1b845 2019-07-15 stsp {
586 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
587 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
588 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
589 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
590 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
591 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
592 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
593 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
594 3ce1b845 2019-07-15 stsp int ch;
595 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
596 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
597 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
598 3ce1b845 2019-07-15 stsp
599 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
600 3ce1b845 2019-07-15 stsp
601 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
602 3ce1b845 2019-07-15 stsp switch (ch) {
603 3ce1b845 2019-07-15 stsp case 'b':
604 3ce1b845 2019-07-15 stsp branch_name = optarg;
605 3ce1b845 2019-07-15 stsp break;
606 3ce1b845 2019-07-15 stsp case 'm':
607 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
608 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
609 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
610 3ce1b845 2019-07-15 stsp goto done;
611 3ce1b845 2019-07-15 stsp }
612 3ce1b845 2019-07-15 stsp break;
613 3ce1b845 2019-07-15 stsp case 'r':
614 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
615 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
616 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
617 9ba1d308 2019-10-21 stsp optarg);
618 3ce1b845 2019-07-15 stsp goto done;
619 3ce1b845 2019-07-15 stsp }
620 3ce1b845 2019-07-15 stsp break;
621 3ce1b845 2019-07-15 stsp case 'I':
622 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
623 3ce1b845 2019-07-15 stsp break;
624 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
625 3ce1b845 2019-07-15 stsp NULL);
626 3ce1b845 2019-07-15 stsp if (error)
627 3ce1b845 2019-07-15 stsp goto done;
628 3ce1b845 2019-07-15 stsp break;
629 3ce1b845 2019-07-15 stsp default:
630 b2b65d18 2019-08-22 stsp usage_import();
631 3ce1b845 2019-07-15 stsp /* NOTREACHED */
632 3ce1b845 2019-07-15 stsp }
633 3ce1b845 2019-07-15 stsp }
634 3ce1b845 2019-07-15 stsp
635 3ce1b845 2019-07-15 stsp argc -= optind;
636 3ce1b845 2019-07-15 stsp argv += optind;
637 3ce1b845 2019-07-15 stsp
638 3ce1b845 2019-07-15 stsp #ifndef PROFILE
639 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
640 aba9c984 2019-09-08 stsp "unveil",
641 3ce1b845 2019-07-15 stsp NULL) == -1)
642 3ce1b845 2019-07-15 stsp err(1, "pledge");
643 3ce1b845 2019-07-15 stsp #endif
644 3ce1b845 2019-07-15 stsp if (argc != 1)
645 3ce1b845 2019-07-15 stsp usage_import();
646 2c7829a4 2019-06-17 stsp
647 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
648 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
649 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
650 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
651 3ce1b845 2019-07-15 stsp }
652 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
653 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
654 c9956ddf 2019-09-08 stsp if (error)
655 c9956ddf 2019-09-08 stsp goto done;
656 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
657 3ce1b845 2019-07-15 stsp if (error)
658 3ce1b845 2019-07-15 stsp goto done;
659 aba9c984 2019-09-08 stsp
660 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
661 aba9c984 2019-09-08 stsp if (error)
662 aba9c984 2019-09-08 stsp return error;
663 e560b7e0 2019-11-28 stsp
664 e560b7e0 2019-11-28 stsp /*
665 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
666 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
667 e560b7e0 2019-11-28 stsp * an unintended typo.
668 e560b7e0 2019-11-28 stsp */
669 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
670 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
671 3ce1b845 2019-07-15 stsp
672 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
673 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
674 3ce1b845 2019-07-15 stsp goto done;
675 3ce1b845 2019-07-15 stsp }
676 3ce1b845 2019-07-15 stsp
677 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
678 3ce1b845 2019-07-15 stsp if (error) {
679 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
680 3ce1b845 2019-07-15 stsp goto done;
681 3ce1b845 2019-07-15 stsp } else {
682 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
683 3ce1b845 2019-07-15 stsp "import target branch already exists");
684 3ce1b845 2019-07-15 stsp goto done;
685 3ce1b845 2019-07-15 stsp }
686 3ce1b845 2019-07-15 stsp
687 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
688 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
689 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
690 3ce1b845 2019-07-15 stsp goto done;
691 3ce1b845 2019-07-15 stsp }
692 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
693 3ce1b845 2019-07-15 stsp
694 3ce1b845 2019-07-15 stsp /*
695 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
696 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
697 3ce1b845 2019-07-15 stsp */
698 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
699 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
700 3ce1b845 2019-07-15 stsp if (error)
701 3ce1b845 2019-07-15 stsp goto done;
702 8e158b01 2019-09-22 stsp free(logmsg);
703 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
704 ef293bdd 2019-10-21 stsp path_dir, refname);
705 ef293bdd 2019-10-21 stsp if (error) {
706 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
707 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
708 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
709 3ce1b845 2019-07-15 stsp goto done;
710 ef293bdd 2019-10-21 stsp }
711 3ce1b845 2019-07-15 stsp }
712 3ce1b845 2019-07-15 stsp
713 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
714 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
715 ef293bdd 2019-10-21 stsp if (logmsg_path)
716 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
717 3ce1b845 2019-07-15 stsp goto done;
718 ef293bdd 2019-10-21 stsp }
719 3ce1b845 2019-07-15 stsp
720 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
721 ef293bdd 2019-10-21 stsp if (error) {
722 ef293bdd 2019-10-21 stsp if (logmsg_path)
723 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
724 ef293bdd 2019-10-21 stsp goto done;
725 ef293bdd 2019-10-21 stsp }
726 ef293bdd 2019-10-21 stsp
727 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
728 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
729 ef293bdd 2019-10-21 stsp if (error) {
730 ef293bdd 2019-10-21 stsp if (logmsg_path)
731 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
732 3ce1b845 2019-07-15 stsp goto done;
733 ef293bdd 2019-10-21 stsp }
734 3ce1b845 2019-07-15 stsp
735 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
736 ef293bdd 2019-10-21 stsp if (error) {
737 ef293bdd 2019-10-21 stsp if (logmsg_path)
738 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
739 3ce1b845 2019-07-15 stsp goto done;
740 ef293bdd 2019-10-21 stsp }
741 3ce1b845 2019-07-15 stsp
742 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
743 ef293bdd 2019-10-21 stsp if (error) {
744 ef293bdd 2019-10-21 stsp if (logmsg_path)
745 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
746 3ce1b845 2019-07-15 stsp goto done;
747 ef293bdd 2019-10-21 stsp }
748 3ce1b845 2019-07-15 stsp
749 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (logmsg_path)
752 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
753 3ce1b845 2019-07-15 stsp goto done;
754 ef293bdd 2019-10-21 stsp }
755 3ce1b845 2019-07-15 stsp
756 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
757 3ce1b845 2019-07-15 stsp if (error) {
758 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
759 ef293bdd 2019-10-21 stsp if (logmsg_path)
760 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
761 3ce1b845 2019-07-15 stsp goto done;
762 ef293bdd 2019-10-21 stsp }
763 3ce1b845 2019-07-15 stsp
764 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
765 3ce1b845 2019-07-15 stsp branch_ref);
766 ef293bdd 2019-10-21 stsp if (error) {
767 ef293bdd 2019-10-21 stsp if (logmsg_path)
768 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
769 3ce1b845 2019-07-15 stsp goto done;
770 ef293bdd 2019-10-21 stsp }
771 3ce1b845 2019-07-15 stsp
772 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
773 ef293bdd 2019-10-21 stsp if (error) {
774 ef293bdd 2019-10-21 stsp if (logmsg_path)
775 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
776 3ce1b845 2019-07-15 stsp goto done;
777 ef293bdd 2019-10-21 stsp }
778 3ce1b845 2019-07-15 stsp }
779 3ce1b845 2019-07-15 stsp
780 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
781 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
782 2c7829a4 2019-06-17 stsp done:
783 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
784 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
785 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
786 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
787 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
788 8e158b01 2019-09-22 stsp free(logmsg);
789 ef293bdd 2019-10-21 stsp free(logmsg_path);
790 2c7829a4 2019-06-17 stsp free(repo_path);
791 3ce1b845 2019-07-15 stsp free(editor);
792 3ce1b845 2019-07-15 stsp free(refname);
793 3ce1b845 2019-07-15 stsp free(new_commit_id);
794 3ce1b845 2019-07-15 stsp free(id_str);
795 aba9c984 2019-09-08 stsp free(author);
796 c9956ddf 2019-09-08 stsp free(gitconfig_path);
797 3ce1b845 2019-07-15 stsp if (branch_ref)
798 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
799 3ce1b845 2019-07-15 stsp if (head_ref)
800 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
801 2c7829a4 2019-06-17 stsp return error;
802 0266afb7 2019-01-04 stsp }
803 0266afb7 2019-01-04 stsp
804 4ed7e80c 2018-05-20 stsp __dead static void
805 c09a553d 2018-03-12 stsp usage_checkout(void)
806 c09a553d 2018-03-12 stsp {
807 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
808 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
809 c09a553d 2018-03-12 stsp exit(1);
810 92a684f4 2018-03-12 stsp }
811 92a684f4 2018-03-12 stsp
812 7f47418f 2019-12-20 stsp static void
813 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
814 7f47418f 2019-12-20 stsp {
815 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
816 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
817 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
818 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
819 7f47418f 2019-12-20 stsp getprogname());
820 7f47418f 2019-12-20 stsp }
821 7f47418f 2019-12-20 stsp
822 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
823 7f47418f 2019-12-20 stsp const char *worktree_path;
824 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
825 7f47418f 2019-12-20 stsp };
826 7f47418f 2019-12-20 stsp
827 1ee397ad 2019-07-12 stsp static const struct got_error *
828 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
829 92a684f4 2018-03-12 stsp {
830 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
831 a484d721 2019-06-10 stsp
832 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
833 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
834 7f47418f 2019-12-20 stsp return NULL;
835 7f47418f 2019-12-20 stsp
836 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
837 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
838 1ee397ad 2019-07-12 stsp return NULL;
839 7f47418f 2019-12-20 stsp }
840 92a684f4 2018-03-12 stsp
841 92a684f4 2018-03-12 stsp while (path[0] == '/')
842 92a684f4 2018-03-12 stsp path++;
843 92a684f4 2018-03-12 stsp
844 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
845 1ee397ad 2019-07-12 stsp return NULL;
846 99437157 2018-11-11 stsp }
847 99437157 2018-11-11 stsp
848 99437157 2018-11-11 stsp static const struct got_error *
849 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
850 99437157 2018-11-11 stsp {
851 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
852 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
853 99437157 2018-11-11 stsp return NULL;
854 8069f636 2019-01-12 stsp }
855 8069f636 2019-01-12 stsp
856 8069f636 2019-01-12 stsp static const struct got_error *
857 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
858 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
859 3aef623b 2019-10-15 stsp struct got_repository *repo)
860 8069f636 2019-01-12 stsp {
861 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
862 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
863 8069f636 2019-01-12 stsp
864 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
865 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
866 36a38700 2019-05-10 stsp if (err)
867 36a38700 2019-05-10 stsp return err;
868 8069f636 2019-01-12 stsp
869 d5bea539 2019-05-13 stsp if (yca_id == NULL)
870 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
871 8069f636 2019-01-12 stsp
872 d5bea539 2019-05-13 stsp /*
873 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
874 d5bea539 2019-05-13 stsp * and the work tree's base commit.
875 d5bea539 2019-05-13 stsp *
876 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
877 d5bea539 2019-05-13 stsp *
878 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
879 d5bea539 2019-05-13 stsp * \ /
880 d5bea539 2019-05-13 stsp * C E
881 d5bea539 2019-05-13 stsp * \ /
882 d5bea539 2019-05-13 stsp * B (yca)
883 d5bea539 2019-05-13 stsp * |
884 d5bea539 2019-05-13 stsp * A
885 d5bea539 2019-05-13 stsp *
886 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
887 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
888 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
889 d5bea539 2019-05-13 stsp */
890 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
891 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
892 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
893 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
894 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
895 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
896 8069f636 2019-01-12 stsp
897 d5bea539 2019-05-13 stsp free(yca_id);
898 d5bea539 2019-05-13 stsp return NULL;
899 c09a553d 2018-03-12 stsp }
900 a367ff0f 2019-05-14 stsp
901 a367ff0f 2019-05-14 stsp static const struct got_error *
902 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
903 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
904 a51a74b3 2019-07-27 stsp struct got_repository *repo)
905 a367ff0f 2019-05-14 stsp {
906 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
907 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
908 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
909 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
910 a367ff0f 2019-05-14 stsp
911 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
912 a367ff0f 2019-05-14 stsp if (err)
913 a51a74b3 2019-07-27 stsp goto done;
914 a51a74b3 2019-07-27 stsp
915 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
916 a51a74b3 2019-07-27 stsp is_same_branch = 1;
917 a51a74b3 2019-07-27 stsp goto done;
918 a51a74b3 2019-07-27 stsp }
919 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
920 a51a74b3 2019-07-27 stsp is_same_branch = 1;
921 a367ff0f 2019-05-14 stsp goto done;
922 a51a74b3 2019-07-27 stsp }
923 a367ff0f 2019-05-14 stsp
924 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
925 a367ff0f 2019-05-14 stsp if (err)
926 a367ff0f 2019-05-14 stsp goto done;
927 a367ff0f 2019-05-14 stsp
928 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
929 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
930 a367ff0f 2019-05-14 stsp if (err)
931 a367ff0f 2019-05-14 stsp goto done;
932 a367ff0f 2019-05-14 stsp
933 a367ff0f 2019-05-14 stsp for (;;) {
934 a367ff0f 2019-05-14 stsp struct got_object_id *id;
935 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
936 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
937 a367ff0f 2019-05-14 stsp if (err) {
938 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
939 a367ff0f 2019-05-14 stsp err = NULL;
940 ee780d5c 2020-01-04 stsp break;
941 a367ff0f 2019-05-14 stsp }
942 c09a553d 2018-03-12 stsp
943 a367ff0f 2019-05-14 stsp if (id) {
944 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
945 a51a74b3 2019-07-27 stsp break;
946 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
947 a367ff0f 2019-05-14 stsp is_same_branch = 1;
948 a367ff0f 2019-05-14 stsp break;
949 a367ff0f 2019-05-14 stsp }
950 a367ff0f 2019-05-14 stsp }
951 a367ff0f 2019-05-14 stsp }
952 a367ff0f 2019-05-14 stsp done:
953 a367ff0f 2019-05-14 stsp if (graph)
954 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
955 a367ff0f 2019-05-14 stsp free(head_commit_id);
956 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
957 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
958 04f57cb3 2019-07-25 stsp return err;
959 04f57cb3 2019-07-25 stsp }
960 04f57cb3 2019-07-25 stsp
961 04f57cb3 2019-07-25 stsp static const struct got_error *
962 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
963 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
964 04f57cb3 2019-07-25 stsp {
965 04f57cb3 2019-07-25 stsp const struct got_error *err;
966 04f57cb3 2019-07-25 stsp struct got_reference *ref;
967 303e2782 2019-08-09 stsp struct got_tag_object *tag;
968 04f57cb3 2019-07-25 stsp
969 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
970 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
971 303e2782 2019-08-09 stsp if (err == NULL) {
972 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
973 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
974 303e2782 2019-08-09 stsp if (*commit_id == NULL)
975 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
976 303e2782 2019-08-09 stsp got_object_tag_close(tag);
977 303e2782 2019-08-09 stsp return err;
978 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
979 303e2782 2019-08-09 stsp return err;
980 303e2782 2019-08-09 stsp
981 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
982 04f57cb3 2019-07-25 stsp if (err == NULL) {
983 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
984 04f57cb3 2019-07-25 stsp got_ref_close(ref);
985 04f57cb3 2019-07-25 stsp } else {
986 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
987 04f57cb3 2019-07-25 stsp return err;
988 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
989 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
990 04f57cb3 2019-07-25 stsp }
991 a367ff0f 2019-05-14 stsp return err;
992 a367ff0f 2019-05-14 stsp }
993 8069f636 2019-01-12 stsp
994 4ed7e80c 2018-05-20 stsp static const struct got_error *
995 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
996 c09a553d 2018-03-12 stsp {
997 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
998 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
999 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1000 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1001 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1002 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1003 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1004 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1005 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1006 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1007 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1008 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1009 f2ea84fa 2019-07-27 stsp
1010 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1011 c09a553d 2018-03-12 stsp
1012 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1013 0bb8a95e 2018-03-12 stsp switch (ch) {
1014 08573d5b 2019-05-14 stsp case 'b':
1015 08573d5b 2019-05-14 stsp branch_name = optarg;
1016 08573d5b 2019-05-14 stsp break;
1017 8069f636 2019-01-12 stsp case 'c':
1018 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1019 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1020 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1021 bb51a5b4 2020-01-13 stsp break;
1022 bb51a5b4 2020-01-13 stsp case 'E':
1023 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1024 8069f636 2019-01-12 stsp break;
1025 0bb8a95e 2018-03-12 stsp case 'p':
1026 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1027 0bb8a95e 2018-03-12 stsp break;
1028 0bb8a95e 2018-03-12 stsp default:
1029 2deda0b9 2019-03-07 stsp usage_checkout();
1030 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1031 0bb8a95e 2018-03-12 stsp }
1032 0bb8a95e 2018-03-12 stsp }
1033 0bb8a95e 2018-03-12 stsp
1034 0bb8a95e 2018-03-12 stsp argc -= optind;
1035 0bb8a95e 2018-03-12 stsp argv += optind;
1036 0bb8a95e 2018-03-12 stsp
1037 6715a751 2018-03-16 stsp #ifndef PROFILE
1038 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1039 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1040 c09a553d 2018-03-12 stsp err(1, "pledge");
1041 6715a751 2018-03-16 stsp #endif
1042 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1043 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1044 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1045 76089277 2018-04-01 stsp if (repo_path == NULL)
1046 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1047 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1048 76089277 2018-04-01 stsp if (cwd == NULL) {
1049 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1050 76089277 2018-04-01 stsp goto done;
1051 76089277 2018-04-01 stsp }
1052 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1053 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1054 230a42bd 2019-05-11 jcs if (base == NULL) {
1055 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1056 230a42bd 2019-05-11 jcs path_prefix);
1057 230a42bd 2019-05-11 jcs goto done;
1058 230a42bd 2019-05-11 jcs }
1059 230a42bd 2019-05-11 jcs } else {
1060 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1061 230a42bd 2019-05-11 jcs if (base == NULL) {
1062 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1063 230a42bd 2019-05-11 jcs repo_path);
1064 230a42bd 2019-05-11 jcs goto done;
1065 230a42bd 2019-05-11 jcs }
1066 76089277 2018-04-01 stsp }
1067 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1068 c09a553d 2018-03-12 stsp if (dotgit)
1069 c09a553d 2018-03-12 stsp *dotgit = '\0';
1070 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1071 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1072 c09a553d 2018-03-12 stsp free(cwd);
1073 76089277 2018-04-01 stsp goto done;
1074 c09a553d 2018-03-12 stsp }
1075 c09a553d 2018-03-12 stsp free(cwd);
1076 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1077 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1078 76089277 2018-04-01 stsp if (repo_path == NULL) {
1079 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1080 76089277 2018-04-01 stsp goto done;
1081 76089277 2018-04-01 stsp }
1082 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1083 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1084 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1085 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1086 b4b3a7dd 2019-07-22 stsp argv[1]);
1087 b4b3a7dd 2019-07-22 stsp goto done;
1088 b4b3a7dd 2019-07-22 stsp }
1089 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1090 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1091 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1092 b4b3a7dd 2019-07-22 stsp goto done;
1093 b4b3a7dd 2019-07-22 stsp }
1094 76089277 2018-04-01 stsp }
1095 c09a553d 2018-03-12 stsp } else
1096 c09a553d 2018-03-12 stsp usage_checkout();
1097 c09a553d 2018-03-12 stsp
1098 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1099 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1100 13bfb272 2019-05-10 jcs
1101 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1102 c09a553d 2018-03-12 stsp if (error != NULL)
1103 c02c541e 2019-03-29 stsp goto done;
1104 c02c541e 2019-03-29 stsp
1105 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1106 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1107 c530dc23 2019-07-23 stsp if (error) {
1108 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1109 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1110 c530dc23 2019-07-23 stsp goto done;
1111 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1112 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1113 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1114 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1115 c530dc23 2019-07-23 stsp goto done;
1116 c530dc23 2019-07-23 stsp }
1117 c530dc23 2019-07-23 stsp }
1118 c530dc23 2019-07-23 stsp
1119 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1120 c02c541e 2019-03-29 stsp if (error)
1121 c09a553d 2018-03-12 stsp goto done;
1122 8069f636 2019-01-12 stsp
1123 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1124 c09a553d 2018-03-12 stsp if (error != NULL)
1125 c09a553d 2018-03-12 stsp goto done;
1126 c09a553d 2018-03-12 stsp
1127 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1128 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1129 c09a553d 2018-03-12 stsp goto done;
1130 c09a553d 2018-03-12 stsp
1131 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1132 c09a553d 2018-03-12 stsp if (error != NULL)
1133 c09a553d 2018-03-12 stsp goto done;
1134 c09a553d 2018-03-12 stsp
1135 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1136 e5dc7198 2018-12-29 stsp path_prefix);
1137 e5dc7198 2018-12-29 stsp if (error != NULL)
1138 e5dc7198 2018-12-29 stsp goto done;
1139 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1140 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1141 49520a32 2018-12-29 stsp goto done;
1142 49520a32 2018-12-29 stsp }
1143 49520a32 2018-12-29 stsp
1144 8069f636 2019-01-12 stsp if (commit_id_str) {
1145 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1146 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1147 30837e32 2019-07-25 stsp if (error)
1148 8069f636 2019-01-12 stsp goto done;
1149 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1150 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1151 8069f636 2019-01-12 stsp if (error != NULL) {
1152 8069f636 2019-01-12 stsp free(commit_id);
1153 8069f636 2019-01-12 stsp goto done;
1154 8069f636 2019-01-12 stsp }
1155 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1156 45d344f6 2019-05-14 stsp if (error)
1157 45d344f6 2019-05-14 stsp goto done;
1158 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1159 8069f636 2019-01-12 stsp commit_id);
1160 8069f636 2019-01-12 stsp free(commit_id);
1161 8069f636 2019-01-12 stsp if (error)
1162 8069f636 2019-01-12 stsp goto done;
1163 8069f636 2019-01-12 stsp }
1164 8069f636 2019-01-12 stsp
1165 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1166 f2ea84fa 2019-07-27 stsp if (error)
1167 f2ea84fa 2019-07-27 stsp goto done;
1168 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1169 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1170 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1171 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1172 c09a553d 2018-03-12 stsp if (error != NULL)
1173 c09a553d 2018-03-12 stsp goto done;
1174 c09a553d 2018-03-12 stsp
1175 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1176 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1177 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1178 c09a553d 2018-03-12 stsp
1179 c09a553d 2018-03-12 stsp done:
1180 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1181 8069f636 2019-01-12 stsp free(commit_id_str);
1182 76089277 2018-04-01 stsp free(repo_path);
1183 507dc3bb 2018-12-29 stsp free(worktree_path);
1184 507dc3bb 2018-12-29 stsp return error;
1185 507dc3bb 2018-12-29 stsp }
1186 507dc3bb 2018-12-29 stsp
1187 507dc3bb 2018-12-29 stsp __dead static void
1188 507dc3bb 2018-12-29 stsp usage_update(void)
1189 507dc3bb 2018-12-29 stsp {
1190 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1191 507dc3bb 2018-12-29 stsp getprogname());
1192 507dc3bb 2018-12-29 stsp exit(1);
1193 507dc3bb 2018-12-29 stsp }
1194 507dc3bb 2018-12-29 stsp
1195 1ee397ad 2019-07-12 stsp static const struct got_error *
1196 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1197 507dc3bb 2018-12-29 stsp {
1198 784955db 2019-01-12 stsp int *did_something = arg;
1199 784955db 2019-01-12 stsp
1200 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1201 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1202 1ee397ad 2019-07-12 stsp return NULL;
1203 507dc3bb 2018-12-29 stsp
1204 1545c615 2019-02-10 stsp *did_something = 1;
1205 a484d721 2019-06-10 stsp
1206 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1207 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1208 1ee397ad 2019-07-12 stsp return NULL;
1209 a484d721 2019-06-10 stsp
1210 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1211 507dc3bb 2018-12-29 stsp path++;
1212 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1213 1ee397ad 2019-07-12 stsp return NULL;
1214 be7061eb 2018-12-30 stsp }
1215 be7061eb 2018-12-30 stsp
1216 be7061eb 2018-12-30 stsp static const struct got_error *
1217 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1218 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1219 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1220 a1fb16d8 2019-05-24 stsp {
1221 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1222 a1fb16d8 2019-05-24 stsp char *base_id_str;
1223 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1224 a1fb16d8 2019-05-24 stsp
1225 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1226 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1227 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1228 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1229 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1230 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1231 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1232 a1fb16d8 2019-05-24 stsp }
1233 a1fb16d8 2019-05-24 stsp
1234 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1235 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1236 a1fb16d8 2019-05-24 stsp if (err) {
1237 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1238 a1fb16d8 2019-05-24 stsp return err;
1239 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1240 a1fb16d8 2019-05-24 stsp }
1241 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1242 a1fb16d8 2019-05-24 stsp return NULL;
1243 a1fb16d8 2019-05-24 stsp
1244 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1245 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1246 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1247 a1fb16d8 2019-05-24 stsp if (err)
1248 a1fb16d8 2019-05-24 stsp return err;
1249 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1250 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1251 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1252 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1253 0ebf8283 2019-07-24 stsp return NULL;
1254 0ebf8283 2019-07-24 stsp }
1255 0ebf8283 2019-07-24 stsp
1256 0ebf8283 2019-07-24 stsp static const struct got_error *
1257 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1258 0ebf8283 2019-07-24 stsp {
1259 0ebf8283 2019-07-24 stsp const struct got_error *err;
1260 0ebf8283 2019-07-24 stsp int in_progress;
1261 0ebf8283 2019-07-24 stsp
1262 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1263 0ebf8283 2019-07-24 stsp if (err)
1264 0ebf8283 2019-07-24 stsp return err;
1265 0ebf8283 2019-07-24 stsp if (in_progress)
1266 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1267 0ebf8283 2019-07-24 stsp
1268 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1269 0ebf8283 2019-07-24 stsp if (err)
1270 0ebf8283 2019-07-24 stsp return err;
1271 0ebf8283 2019-07-24 stsp if (in_progress)
1272 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1273 0ebf8283 2019-07-24 stsp
1274 a1fb16d8 2019-05-24 stsp return NULL;
1275 a5edda0a 2019-07-27 stsp }
1276 a5edda0a 2019-07-27 stsp
1277 a5edda0a 2019-07-27 stsp static const struct got_error *
1278 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1279 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1280 a5edda0a 2019-07-27 stsp {
1281 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1282 a5edda0a 2019-07-27 stsp char *path;
1283 a5edda0a 2019-07-27 stsp int i;
1284 a5edda0a 2019-07-27 stsp
1285 a5edda0a 2019-07-27 stsp if (argc == 0) {
1286 a5edda0a 2019-07-27 stsp path = strdup("");
1287 a5edda0a 2019-07-27 stsp if (path == NULL)
1288 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1289 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1290 a5edda0a 2019-07-27 stsp }
1291 a5edda0a 2019-07-27 stsp
1292 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1293 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1294 a5edda0a 2019-07-27 stsp if (err)
1295 a5edda0a 2019-07-27 stsp break;
1296 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1297 a5edda0a 2019-07-27 stsp if (err) {
1298 a5edda0a 2019-07-27 stsp free(path);
1299 a5edda0a 2019-07-27 stsp break;
1300 a5edda0a 2019-07-27 stsp }
1301 a5edda0a 2019-07-27 stsp }
1302 a5edda0a 2019-07-27 stsp
1303 a5edda0a 2019-07-27 stsp return err;
1304 a1fb16d8 2019-05-24 stsp }
1305 a1fb16d8 2019-05-24 stsp
1306 a1fb16d8 2019-05-24 stsp static const struct got_error *
1307 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1308 507dc3bb 2018-12-29 stsp {
1309 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1310 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1311 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1312 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1313 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1314 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1315 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1316 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1317 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1318 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1319 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1320 507dc3bb 2018-12-29 stsp
1321 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1322 f2ea84fa 2019-07-27 stsp
1323 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1324 507dc3bb 2018-12-29 stsp switch (ch) {
1325 024e9686 2019-05-14 stsp case 'b':
1326 024e9686 2019-05-14 stsp branch_name = optarg;
1327 024e9686 2019-05-14 stsp break;
1328 507dc3bb 2018-12-29 stsp case 'c':
1329 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1330 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1331 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1332 507dc3bb 2018-12-29 stsp break;
1333 507dc3bb 2018-12-29 stsp default:
1334 2deda0b9 2019-03-07 stsp usage_update();
1335 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1336 507dc3bb 2018-12-29 stsp }
1337 507dc3bb 2018-12-29 stsp }
1338 507dc3bb 2018-12-29 stsp
1339 507dc3bb 2018-12-29 stsp argc -= optind;
1340 507dc3bb 2018-12-29 stsp argv += optind;
1341 507dc3bb 2018-12-29 stsp
1342 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1343 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1344 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1345 507dc3bb 2018-12-29 stsp err(1, "pledge");
1346 507dc3bb 2018-12-29 stsp #endif
1347 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1348 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1349 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1350 c4cdcb68 2019-04-03 stsp goto done;
1351 c4cdcb68 2019-04-03 stsp }
1352 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1353 7d5807f4 2019-07-11 stsp if (error)
1354 7d5807f4 2019-07-11 stsp goto done;
1355 7d5807f4 2019-07-11 stsp
1356 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1357 f2ea84fa 2019-07-27 stsp if (error)
1358 f2ea84fa 2019-07-27 stsp goto done;
1359 507dc3bb 2018-12-29 stsp
1360 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1361 c9956ddf 2019-09-08 stsp NULL);
1362 507dc3bb 2018-12-29 stsp if (error != NULL)
1363 507dc3bb 2018-12-29 stsp goto done;
1364 507dc3bb 2018-12-29 stsp
1365 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1366 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1367 087fb88c 2019-08-04 stsp if (error)
1368 087fb88c 2019-08-04 stsp goto done;
1369 087fb88c 2019-08-04 stsp
1370 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1371 0266afb7 2019-01-04 stsp if (error)
1372 0266afb7 2019-01-04 stsp goto done;
1373 0266afb7 2019-01-04 stsp
1374 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1375 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1376 024e9686 2019-05-14 stsp if (error != NULL)
1377 024e9686 2019-05-14 stsp goto done;
1378 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1379 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1380 9c4b8182 2019-01-02 stsp if (error != NULL)
1381 9c4b8182 2019-01-02 stsp goto done;
1382 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1383 507dc3bb 2018-12-29 stsp if (error != NULL)
1384 507dc3bb 2018-12-29 stsp goto done;
1385 507dc3bb 2018-12-29 stsp } else {
1386 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1387 04f57cb3 2019-07-25 stsp free(commit_id_str);
1388 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1389 30837e32 2019-07-25 stsp if (error)
1390 a297e751 2019-07-11 stsp goto done;
1391 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1392 a297e751 2019-07-11 stsp if (error)
1393 507dc3bb 2018-12-29 stsp goto done;
1394 507dc3bb 2018-12-29 stsp }
1395 35c965b2 2018-12-29 stsp
1396 a1fb16d8 2019-05-24 stsp if (branch_name) {
1397 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1398 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1399 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1400 f2ea84fa 2019-07-27 stsp continue;
1401 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1402 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1403 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1404 024e9686 2019-05-14 stsp goto done;
1405 024e9686 2019-05-14 stsp }
1406 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1407 024e9686 2019-05-14 stsp if (error)
1408 024e9686 2019-05-14 stsp goto done;
1409 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1410 3aef623b 2019-10-15 stsp repo);
1411 a367ff0f 2019-05-14 stsp free(head_commit_id);
1412 024e9686 2019-05-14 stsp if (error != NULL)
1413 024e9686 2019-05-14 stsp goto done;
1414 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1415 a367ff0f 2019-05-14 stsp if (error)
1416 a367ff0f 2019-05-14 stsp goto done;
1417 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1418 024e9686 2019-05-14 stsp if (error)
1419 024e9686 2019-05-14 stsp goto done;
1420 024e9686 2019-05-14 stsp } else {
1421 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1422 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1423 a367ff0f 2019-05-14 stsp if (error != NULL) {
1424 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1425 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1426 024e9686 2019-05-14 stsp goto done;
1427 a367ff0f 2019-05-14 stsp }
1428 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1429 a367ff0f 2019-05-14 stsp if (error)
1430 a367ff0f 2019-05-14 stsp goto done;
1431 024e9686 2019-05-14 stsp }
1432 507dc3bb 2018-12-29 stsp
1433 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1434 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1435 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1436 507dc3bb 2018-12-29 stsp commit_id);
1437 507dc3bb 2018-12-29 stsp if (error)
1438 507dc3bb 2018-12-29 stsp goto done;
1439 507dc3bb 2018-12-29 stsp }
1440 507dc3bb 2018-12-29 stsp
1441 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1442 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1443 507dc3bb 2018-12-29 stsp if (error != NULL)
1444 507dc3bb 2018-12-29 stsp goto done;
1445 9c4b8182 2019-01-02 stsp
1446 784955db 2019-01-12 stsp if (did_something)
1447 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1448 784955db 2019-01-12 stsp else
1449 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1450 507dc3bb 2018-12-29 stsp done:
1451 c09a553d 2018-03-12 stsp free(worktree_path);
1452 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1453 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1454 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1455 507dc3bb 2018-12-29 stsp free(commit_id);
1456 9c4b8182 2019-01-02 stsp free(commit_id_str);
1457 c09a553d 2018-03-12 stsp return error;
1458 c09a553d 2018-03-12 stsp }
1459 c09a553d 2018-03-12 stsp
1460 f42b1b34 2018-03-12 stsp static const struct got_error *
1461 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1462 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1463 63035f9f 2019-10-06 stsp struct got_repository *repo)
1464 5c860e29 2018-03-12 stsp {
1465 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1466 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1467 79109fed 2018-03-27 stsp
1468 44392932 2019-08-25 stsp if (blob_id1) {
1469 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1470 44392932 2019-08-25 stsp if (err)
1471 44392932 2019-08-25 stsp goto done;
1472 44392932 2019-08-25 stsp }
1473 79109fed 2018-03-27 stsp
1474 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1475 44392932 2019-08-25 stsp if (err)
1476 44392932 2019-08-25 stsp goto done;
1477 79109fed 2018-03-27 stsp
1478 44392932 2019-08-25 stsp while (path[0] == '/')
1479 44392932 2019-08-25 stsp path++;
1480 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1481 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1482 44392932 2019-08-25 stsp done:
1483 44392932 2019-08-25 stsp if (blob1)
1484 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1485 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1486 44392932 2019-08-25 stsp return err;
1487 44392932 2019-08-25 stsp }
1488 44392932 2019-08-25 stsp
1489 44392932 2019-08-25 stsp static const struct got_error *
1490 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1491 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1492 63035f9f 2019-10-06 stsp struct got_repository *repo)
1493 44392932 2019-08-25 stsp {
1494 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1495 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1496 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1497 44392932 2019-08-25 stsp
1498 44392932 2019-08-25 stsp if (tree_id1) {
1499 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1500 79109fed 2018-03-27 stsp if (err)
1501 44392932 2019-08-25 stsp goto done;
1502 79109fed 2018-03-27 stsp }
1503 79109fed 2018-03-27 stsp
1504 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1505 0f2b3dca 2018-12-22 stsp if (err)
1506 0f2b3dca 2018-12-22 stsp goto done;
1507 0f2b3dca 2018-12-22 stsp
1508 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1509 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1510 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1511 44392932 2019-08-25 stsp while (path[0] == '/')
1512 44392932 2019-08-25 stsp path++;
1513 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1514 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1515 0f2b3dca 2018-12-22 stsp done:
1516 79109fed 2018-03-27 stsp if (tree1)
1517 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1518 366e0a5f 2019-10-10 stsp if (tree2)
1519 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1520 44392932 2019-08-25 stsp return err;
1521 44392932 2019-08-25 stsp }
1522 44392932 2019-08-25 stsp
1523 44392932 2019-08-25 stsp static const struct got_error *
1524 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1525 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1526 44392932 2019-08-25 stsp {
1527 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1528 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1529 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1530 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1531 44392932 2019-08-25 stsp struct got_object_qid *qid;
1532 44392932 2019-08-25 stsp
1533 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1534 44392932 2019-08-25 stsp if (qid != NULL) {
1535 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1536 44392932 2019-08-25 stsp qid->id);
1537 44392932 2019-08-25 stsp if (err)
1538 44392932 2019-08-25 stsp return err;
1539 44392932 2019-08-25 stsp }
1540 44392932 2019-08-25 stsp
1541 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1542 44392932 2019-08-25 stsp int obj_type;
1543 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1544 44392932 2019-08-25 stsp if (err)
1545 44392932 2019-08-25 stsp goto done;
1546 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1547 44392932 2019-08-25 stsp if (err) {
1548 44392932 2019-08-25 stsp free(obj_id2);
1549 44392932 2019-08-25 stsp goto done;
1550 44392932 2019-08-25 stsp }
1551 44392932 2019-08-25 stsp if (pcommit) {
1552 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1553 44392932 2019-08-25 stsp qid->id, path);
1554 44392932 2019-08-25 stsp if (err) {
1555 44392932 2019-08-25 stsp free(obj_id2);
1556 44392932 2019-08-25 stsp goto done;
1557 44392932 2019-08-25 stsp }
1558 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1559 44392932 2019-08-25 stsp if (err) {
1560 44392932 2019-08-25 stsp free(obj_id2);
1561 44392932 2019-08-25 stsp goto done;
1562 44392932 2019-08-25 stsp }
1563 44392932 2019-08-25 stsp }
1564 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1565 44392932 2019-08-25 stsp if (err) {
1566 44392932 2019-08-25 stsp free(obj_id2);
1567 44392932 2019-08-25 stsp goto done;
1568 44392932 2019-08-25 stsp }
1569 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1570 44392932 2019-08-25 stsp switch (obj_type) {
1571 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1572 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1573 63035f9f 2019-10-06 stsp 0, repo);
1574 44392932 2019-08-25 stsp break;
1575 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1576 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1577 63035f9f 2019-10-06 stsp 0, repo);
1578 44392932 2019-08-25 stsp break;
1579 44392932 2019-08-25 stsp default:
1580 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1581 44392932 2019-08-25 stsp break;
1582 44392932 2019-08-25 stsp }
1583 44392932 2019-08-25 stsp free(obj_id1);
1584 44392932 2019-08-25 stsp free(obj_id2);
1585 44392932 2019-08-25 stsp } else {
1586 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1587 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1588 44392932 2019-08-25 stsp if (err)
1589 44392932 2019-08-25 stsp goto done;
1590 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1591 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1592 44392932 2019-08-25 stsp if (err)
1593 44392932 2019-08-25 stsp goto done;
1594 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1595 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1596 44392932 2019-08-25 stsp }
1597 44392932 2019-08-25 stsp
1598 44392932 2019-08-25 stsp done:
1599 0f2b3dca 2018-12-22 stsp free(id_str1);
1600 0f2b3dca 2018-12-22 stsp free(id_str2);
1601 44392932 2019-08-25 stsp if (pcommit)
1602 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1603 79109fed 2018-03-27 stsp return err;
1604 79109fed 2018-03-27 stsp }
1605 79109fed 2018-03-27 stsp
1606 4bb494d5 2018-06-16 stsp static char *
1607 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1608 6c281f94 2018-06-11 stsp {
1609 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1610 09867e48 2019-08-13 stsp char *p, *s;
1611 09867e48 2019-08-13 stsp
1612 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1613 09867e48 2019-08-13 stsp if (tm == NULL)
1614 09867e48 2019-08-13 stsp return NULL;
1615 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1616 09867e48 2019-08-13 stsp if (s == NULL)
1617 09867e48 2019-08-13 stsp return NULL;
1618 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1619 6c281f94 2018-06-11 stsp if (p)
1620 6c281f94 2018-06-11 stsp *p = '\0';
1621 6c281f94 2018-06-11 stsp return s;
1622 6c281f94 2018-06-11 stsp }
1623 dc424a06 2019-08-07 stsp
1624 6841bf13 2019-11-29 kn static const struct got_error *
1625 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1626 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1627 6841bf13 2019-11-29 kn {
1628 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1629 6841bf13 2019-11-29 kn regmatch_t regmatch;
1630 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1631 6841bf13 2019-11-29 kn
1632 6841bf13 2019-11-29 kn *have_match = 0;
1633 6841bf13 2019-11-29 kn
1634 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1635 6841bf13 2019-11-29 kn if (err)
1636 6841bf13 2019-11-29 kn return err;
1637 6841bf13 2019-11-29 kn
1638 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1639 6841bf13 2019-11-29 kn if (err)
1640 6841bf13 2019-11-29 kn goto done;
1641 6841bf13 2019-11-29 kn
1642 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1643 6841bf13 2019-11-29 kn *have_match = 1;
1644 6841bf13 2019-11-29 kn done:
1645 6841bf13 2019-11-29 kn free(id_str);
1646 6841bf13 2019-11-29 kn free(logmsg);
1647 6841bf13 2019-11-29 kn return err;
1648 6841bf13 2019-11-29 kn }
1649 6841bf13 2019-11-29 kn
1650 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1651 6c281f94 2018-06-11 stsp
1652 79109fed 2018-03-27 stsp static const struct got_error *
1653 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1654 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1655 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1656 79109fed 2018-03-27 stsp {
1657 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1658 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1659 6c281f94 2018-06-11 stsp char datebuf[26];
1660 45d799e2 2018-12-23 stsp time_t committer_time;
1661 45d799e2 2018-12-23 stsp const char *author, *committer;
1662 199a4027 2019-02-02 stsp char *refs_str = NULL;
1663 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1664 5c860e29 2018-03-12 stsp
1665 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1666 199a4027 2019-02-02 stsp char *s;
1667 199a4027 2019-02-02 stsp const char *name;
1668 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1669 a436ad14 2019-08-13 stsp int cmp;
1670 a436ad14 2019-08-13 stsp
1671 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1672 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1673 d9498b20 2019-02-05 stsp continue;
1674 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1675 199a4027 2019-02-02 stsp name += 5;
1676 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1677 7143d404 2019-03-12 stsp continue;
1678 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1679 e34f9ed6 2019-02-02 stsp name += 6;
1680 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1681 141c2bff 2019-02-04 stsp name += 8;
1682 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1683 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1684 5d844a1e 2019-08-13 stsp if (err) {
1685 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1686 5d844a1e 2019-08-13 stsp return err;
1687 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1688 5d844a1e 2019-08-13 stsp err = NULL;
1689 5d844a1e 2019-08-13 stsp tag = NULL;
1690 5d844a1e 2019-08-13 stsp }
1691 a436ad14 2019-08-13 stsp }
1692 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1693 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1694 a436ad14 2019-08-13 stsp if (tag)
1695 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1696 a436ad14 2019-08-13 stsp if (cmp != 0)
1697 a436ad14 2019-08-13 stsp continue;
1698 199a4027 2019-02-02 stsp s = refs_str;
1699 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1700 199a4027 2019-02-02 stsp name) == -1) {
1701 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1702 199a4027 2019-02-02 stsp free(s);
1703 34ca4898 2019-08-13 stsp return err;
1704 199a4027 2019-02-02 stsp }
1705 199a4027 2019-02-02 stsp free(s);
1706 199a4027 2019-02-02 stsp }
1707 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1708 8bf5b3c9 2018-03-17 stsp if (err)
1709 8bf5b3c9 2018-03-17 stsp return err;
1710 788c352e 2018-06-16 stsp
1711 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1712 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1713 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1714 832c249c 2018-06-10 stsp free(id_str);
1715 d3d493d7 2019-02-21 stsp id_str = NULL;
1716 d3d493d7 2019-02-21 stsp free(refs_str);
1717 d3d493d7 2019-02-21 stsp refs_str = NULL;
1718 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1719 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1720 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1721 09867e48 2019-08-13 stsp if (datestr)
1722 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1723 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1724 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1725 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1726 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1727 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1728 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1729 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1730 3fe1abad 2018-06-10 stsp int n = 1;
1731 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1732 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1733 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1734 a0603db2 2018-06-10 stsp if (err)
1735 a0603db2 2018-06-10 stsp return err;
1736 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1737 a0603db2 2018-06-10 stsp free(id_str);
1738 a0603db2 2018-06-10 stsp }
1739 a0603db2 2018-06-10 stsp }
1740 832c249c 2018-06-10 stsp
1741 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1742 5943eee2 2019-08-13 stsp if (err)
1743 5943eee2 2019-08-13 stsp return err;
1744 8bf5b3c9 2018-03-17 stsp
1745 621015ac 2018-07-23 stsp logmsg = logmsg0;
1746 832c249c 2018-06-10 stsp do {
1747 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1748 832c249c 2018-06-10 stsp if (line)
1749 832c249c 2018-06-10 stsp printf(" %s\n", line);
1750 832c249c 2018-06-10 stsp } while (line);
1751 621015ac 2018-07-23 stsp free(logmsg0);
1752 832c249c 2018-06-10 stsp
1753 971751ac 2018-03-27 stsp if (show_patch) {
1754 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1755 971751ac 2018-03-27 stsp if (err == 0)
1756 971751ac 2018-03-27 stsp printf("\n");
1757 971751ac 2018-03-27 stsp }
1758 07862c20 2018-09-15 stsp
1759 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1760 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1761 07862c20 2018-09-15 stsp return err;
1762 f42b1b34 2018-03-12 stsp }
1763 5c860e29 2018-03-12 stsp
1764 f42b1b34 2018-03-12 stsp static const struct got_error *
1765 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1766 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1767 463a997a 2019-12-08 kn int diff_context, int limit, int first_parent_traversal,
1768 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1769 f42b1b34 2018-03-12 stsp {
1770 f42b1b34 2018-03-12 stsp const struct got_error *err;
1771 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1772 6841bf13 2019-11-29 kn regex_t regex;
1773 6841bf13 2019-11-29 kn int have_match;
1774 372ccdbb 2018-06-10 stsp
1775 6841bf13 2019-11-29 kn if (search_pattern &&
1776 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1777 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1778 6841bf13 2019-11-29 kn
1779 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, path, first_parent_traversal);
1780 f42b1b34 2018-03-12 stsp if (err)
1781 f42b1b34 2018-03-12 stsp return err;
1782 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1783 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1784 372ccdbb 2018-06-10 stsp if (err)
1785 fcc85cad 2018-07-22 stsp goto done;
1786 656b1f76 2019-05-11 jcs for (;;) {
1787 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1788 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1789 8bf5b3c9 2018-03-17 stsp
1790 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1791 84453469 2018-11-11 stsp break;
1792 84453469 2018-11-11 stsp
1793 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1794 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1795 372ccdbb 2018-06-10 stsp if (err) {
1796 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1797 9ba79e04 2018-06-11 stsp err = NULL;
1798 ee780d5c 2020-01-04 stsp break;
1799 7e665116 2018-04-02 stsp }
1800 b43fbaa0 2018-06-11 stsp if (id == NULL)
1801 7e665116 2018-04-02 stsp break;
1802 b43fbaa0 2018-06-11 stsp
1803 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1804 b43fbaa0 2018-06-11 stsp if (err)
1805 fcc85cad 2018-07-22 stsp break;
1806 6841bf13 2019-11-29 kn
1807 6841bf13 2019-11-29 kn if (search_pattern) {
1808 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
1809 6841bf13 2019-11-29 kn if (err) {
1810 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1811 6841bf13 2019-11-29 kn break;
1812 6841bf13 2019-11-29 kn }
1813 6841bf13 2019-11-29 kn if (have_match == 0) {
1814 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1815 6841bf13 2019-11-29 kn continue;
1816 6841bf13 2019-11-29 kn }
1817 6841bf13 2019-11-29 kn }
1818 6841bf13 2019-11-29 kn
1819 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1820 44392932 2019-08-25 stsp diff_context, refs);
1821 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1822 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1823 7e665116 2018-04-02 stsp break;
1824 31cedeaf 2018-09-15 stsp }
1825 fcc85cad 2018-07-22 stsp done:
1826 6841bf13 2019-11-29 kn if (search_pattern)
1827 6841bf13 2019-11-29 kn regfree(&regex);
1828 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1829 f42b1b34 2018-03-12 stsp return err;
1830 f42b1b34 2018-03-12 stsp }
1831 5c860e29 2018-03-12 stsp
1832 4ed7e80c 2018-05-20 stsp __dead static void
1833 6f3d1eb0 2018-03-12 stsp usage_log(void)
1834 6f3d1eb0 2018-03-12 stsp {
1835 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1836 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1837 6f3d1eb0 2018-03-12 stsp exit(1);
1838 b1ebc001 2019-08-13 stsp }
1839 b1ebc001 2019-08-13 stsp
1840 b1ebc001 2019-08-13 stsp static int
1841 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1842 b1ebc001 2019-08-13 stsp {
1843 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1844 b1ebc001 2019-08-13 stsp long long n;
1845 b1ebc001 2019-08-13 stsp const char *errstr;
1846 b1ebc001 2019-08-13 stsp
1847 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1848 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1849 b1ebc001 2019-08-13 stsp return 0;
1850 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1851 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1852 b1ebc001 2019-08-13 stsp return 0;
1853 b1ebc001 2019-08-13 stsp return n;
1854 6f3d1eb0 2018-03-12 stsp }
1855 6f3d1eb0 2018-03-12 stsp
1856 4ed7e80c 2018-05-20 stsp static const struct got_error *
1857 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1858 f42b1b34 2018-03-12 stsp {
1859 f42b1b34 2018-03-12 stsp const struct got_error *error;
1860 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1861 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1862 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1863 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1864 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1865 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
1866 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
1867 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1868 64a96a6d 2018-04-01 stsp const char *errstr;
1869 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1870 1b3893a2 2019-03-18 stsp
1871 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1872 5c860e29 2018-03-12 stsp
1873 6715a751 2018-03-16 stsp #ifndef PROFILE
1874 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1875 6098196c 2019-01-04 stsp NULL)
1876 ad242220 2018-09-08 stsp == -1)
1877 f42b1b34 2018-03-12 stsp err(1, "pledge");
1878 6715a751 2018-03-16 stsp #endif
1879 79109fed 2018-03-27 stsp
1880 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1881 b1ebc001 2019-08-13 stsp
1882 6841bf13 2019-11-29 kn while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1883 79109fed 2018-03-27 stsp switch (ch) {
1884 79109fed 2018-03-27 stsp case 'p':
1885 79109fed 2018-03-27 stsp show_patch = 1;
1886 d142fc45 2018-04-01 stsp break;
1887 d142fc45 2018-04-01 stsp case 'c':
1888 d142fc45 2018-04-01 stsp start_commit = optarg;
1889 64a96a6d 2018-04-01 stsp break;
1890 c0cc5c62 2018-10-18 stsp case 'C':
1891 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1892 4a8520aa 2018-10-18 stsp &errstr);
1893 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1894 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1895 c0cc5c62 2018-10-18 stsp break;
1896 64a96a6d 2018-04-01 stsp case 'l':
1897 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1898 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1899 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1900 cc54c501 2019-07-15 stsp break;
1901 cc54c501 2019-07-15 stsp case 'f':
1902 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1903 a0603db2 2018-06-10 stsp break;
1904 04ca23f4 2018-07-16 stsp case 'r':
1905 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1906 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1907 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1908 9ba1d308 2019-10-21 stsp optarg);
1909 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1910 04ca23f4 2018-07-16 stsp break;
1911 6841bf13 2019-11-29 kn case 's':
1912 6841bf13 2019-11-29 kn search_pattern = optarg;
1913 6841bf13 2019-11-29 kn break;
1914 79109fed 2018-03-27 stsp default:
1915 2deda0b9 2019-03-07 stsp usage_log();
1916 79109fed 2018-03-27 stsp /* NOTREACHED */
1917 79109fed 2018-03-27 stsp }
1918 79109fed 2018-03-27 stsp }
1919 79109fed 2018-03-27 stsp
1920 79109fed 2018-03-27 stsp argc -= optind;
1921 79109fed 2018-03-27 stsp argv += optind;
1922 f42b1b34 2018-03-12 stsp
1923 dc1edbfa 2019-11-29 kn if (diff_context == -1)
1924 dc1edbfa 2019-11-29 kn diff_context = 3;
1925 dc1edbfa 2019-11-29 kn else if (!show_patch)
1926 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
1927 dc1edbfa 2019-11-29 kn
1928 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1929 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1930 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1931 04ca23f4 2018-07-16 stsp goto done;
1932 04ca23f4 2018-07-16 stsp }
1933 cffc0aa4 2019-02-05 stsp
1934 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1935 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1936 cffc0aa4 2019-02-05 stsp goto done;
1937 cffc0aa4 2019-02-05 stsp error = NULL;
1938 cffc0aa4 2019-02-05 stsp
1939 e7301579 2019-03-18 stsp if (argc == 0) {
1940 cbd1af7a 2019-03-18 stsp path = strdup("");
1941 e7301579 2019-03-18 stsp if (path == NULL) {
1942 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1943 cbd1af7a 2019-03-18 stsp goto done;
1944 e7301579 2019-03-18 stsp }
1945 e7301579 2019-03-18 stsp } else if (argc == 1) {
1946 e7301579 2019-03-18 stsp if (worktree) {
1947 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1948 e7301579 2019-03-18 stsp argv[0]);
1949 e7301579 2019-03-18 stsp if (error)
1950 e7301579 2019-03-18 stsp goto done;
1951 e7301579 2019-03-18 stsp } else {
1952 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1953 e7301579 2019-03-18 stsp if (path == NULL) {
1954 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1955 e7301579 2019-03-18 stsp goto done;
1956 e7301579 2019-03-18 stsp }
1957 e7301579 2019-03-18 stsp }
1958 cbd1af7a 2019-03-18 stsp } else
1959 cbd1af7a 2019-03-18 stsp usage_log();
1960 cbd1af7a 2019-03-18 stsp
1961 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1962 5486daa2 2019-05-11 stsp repo_path = worktree ?
1963 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1964 5486daa2 2019-05-11 stsp }
1965 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1966 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1967 cffc0aa4 2019-02-05 stsp goto done;
1968 04ca23f4 2018-07-16 stsp }
1969 6098196c 2019-01-04 stsp
1970 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1971 d7d4f210 2018-03-12 stsp if (error != NULL)
1972 04ca23f4 2018-07-16 stsp goto done;
1973 f42b1b34 2018-03-12 stsp
1974 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1975 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1976 c02c541e 2019-03-29 stsp if (error)
1977 c02c541e 2019-03-29 stsp goto done;
1978 c02c541e 2019-03-29 stsp
1979 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1980 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1981 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1982 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1983 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1984 3235492e 2018-04-01 stsp if (error != NULL)
1985 3235492e 2018-04-01 stsp return error;
1986 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1987 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1988 3235492e 2018-04-01 stsp if (error != NULL)
1989 3235492e 2018-04-01 stsp return error;
1990 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1991 3235492e 2018-04-01 stsp } else {
1992 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1993 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1994 3235492e 2018-04-01 stsp if (error == NULL) {
1995 1caad61b 2019-02-01 stsp int obj_type;
1996 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1997 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1998 d1f2edc9 2018-06-13 stsp if (error != NULL)
1999 1caad61b 2019-02-01 stsp goto done;
2000 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2001 1caad61b 2019-02-01 stsp if (error != NULL)
2002 1caad61b 2019-02-01 stsp goto done;
2003 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2004 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2005 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2006 1caad61b 2019-02-01 stsp if (error != NULL)
2007 1caad61b 2019-02-01 stsp goto done;
2008 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2009 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2010 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2011 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2012 1caad61b 2019-02-01 stsp goto done;
2013 1caad61b 2019-02-01 stsp }
2014 1caad61b 2019-02-01 stsp free(id);
2015 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2016 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2017 1caad61b 2019-02-01 stsp if (id == NULL)
2018 638f9024 2019-05-13 stsp error = got_error_from_errno(
2019 230a42bd 2019-05-11 jcs "got_object_id_dup");
2020 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2021 1caad61b 2019-02-01 stsp if (error)
2022 1caad61b 2019-02-01 stsp goto done;
2023 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2024 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2025 1caad61b 2019-02-01 stsp goto done;
2026 1caad61b 2019-02-01 stsp }
2027 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2028 d1f2edc9 2018-06-13 stsp if (error != NULL)
2029 1caad61b 2019-02-01 stsp goto done;
2030 d1f2edc9 2018-06-13 stsp }
2031 15a94983 2018-12-23 stsp if (commit == NULL) {
2032 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2033 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2034 d1f2edc9 2018-06-13 stsp if (error != NULL)
2035 d1f2edc9 2018-06-13 stsp return error;
2036 3235492e 2018-04-01 stsp }
2037 3235492e 2018-04-01 stsp }
2038 d7d4f210 2018-03-12 stsp if (error != NULL)
2039 04ca23f4 2018-07-16 stsp goto done;
2040 04ca23f4 2018-07-16 stsp
2041 deeabeae 2019-08-27 stsp if (worktree) {
2042 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2043 deeabeae 2019-08-27 stsp char *p;
2044 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2045 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2046 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2047 deeabeae 2019-08-27 stsp goto done;
2048 deeabeae 2019-08-27 stsp }
2049 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2050 deeabeae 2019-08-27 stsp free(p);
2051 deeabeae 2019-08-27 stsp } else
2052 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2053 04ca23f4 2018-07-16 stsp if (error != NULL)
2054 04ca23f4 2018-07-16 stsp goto done;
2055 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2056 04ca23f4 2018-07-16 stsp free(path);
2057 04ca23f4 2018-07-16 stsp path = in_repo_path;
2058 04ca23f4 2018-07-16 stsp }
2059 199a4027 2019-02-02 stsp
2060 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2061 199a4027 2019-02-02 stsp if (error)
2062 199a4027 2019-02-02 stsp goto done;
2063 04ca23f4 2018-07-16 stsp
2064 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2065 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
2066 04ca23f4 2018-07-16 stsp done:
2067 04ca23f4 2018-07-16 stsp free(path);
2068 04ca23f4 2018-07-16 stsp free(repo_path);
2069 04ca23f4 2018-07-16 stsp free(cwd);
2070 f42b1b34 2018-03-12 stsp free(id);
2071 cffc0aa4 2019-02-05 stsp if (worktree)
2072 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2073 ad242220 2018-09-08 stsp if (repo) {
2074 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2075 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2076 ad242220 2018-09-08 stsp if (error == NULL)
2077 ad242220 2018-09-08 stsp error = repo_error;
2078 ad242220 2018-09-08 stsp }
2079 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2080 8bf5b3c9 2018-03-17 stsp return error;
2081 5c860e29 2018-03-12 stsp }
2082 5c860e29 2018-03-12 stsp
2083 4ed7e80c 2018-05-20 stsp __dead static void
2084 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2085 3f8b7d6a 2018-04-01 stsp {
2086 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2087 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2088 3f8b7d6a 2018-04-01 stsp exit(1);
2089 b00d56cd 2018-04-01 stsp }
2090 b00d56cd 2018-04-01 stsp
2091 b72f483a 2019-02-05 stsp struct print_diff_arg {
2092 b72f483a 2019-02-05 stsp struct got_repository *repo;
2093 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2094 b72f483a 2019-02-05 stsp int diff_context;
2095 3fc0c068 2019-02-10 stsp const char *id_str;
2096 3fc0c068 2019-02-10 stsp int header_shown;
2097 98eaaa12 2019-08-03 stsp int diff_staged;
2098 63035f9f 2019-10-06 stsp int ignore_whitespace;
2099 b72f483a 2019-02-05 stsp };
2100 b72f483a 2019-02-05 stsp
2101 4ed7e80c 2018-05-20 stsp static const struct got_error *
2102 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2103 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2104 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2105 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2106 b72f483a 2019-02-05 stsp {
2107 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2108 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2109 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2110 12463d8b 2019-12-13 stsp int fd = -1;
2111 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2112 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2113 b72f483a 2019-02-05 stsp struct stat sb;
2114 b72f483a 2019-02-05 stsp
2115 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2116 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2117 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2118 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2119 98eaaa12 2019-08-03 stsp return NULL;
2120 98eaaa12 2019-08-03 stsp } else {
2121 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2122 98eaaa12 2019-08-03 stsp return NULL;
2123 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2124 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2125 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2126 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2127 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2128 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2129 98eaaa12 2019-08-03 stsp return NULL;
2130 98eaaa12 2019-08-03 stsp }
2131 3fc0c068 2019-02-10 stsp
2132 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2133 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2134 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2135 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2136 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2137 3fc0c068 2019-02-10 stsp }
2138 b72f483a 2019-02-05 stsp
2139 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2140 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2141 98eaaa12 2019-08-03 stsp switch (staged_status) {
2142 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2143 98eaaa12 2019-08-03 stsp label1 = path;
2144 98eaaa12 2019-08-03 stsp label2 = path;
2145 98eaaa12 2019-08-03 stsp break;
2146 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2147 98eaaa12 2019-08-03 stsp label2 = path;
2148 98eaaa12 2019-08-03 stsp break;
2149 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2150 98eaaa12 2019-08-03 stsp label1 = path;
2151 98eaaa12 2019-08-03 stsp break;
2152 98eaaa12 2019-08-03 stsp default:
2153 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2154 98eaaa12 2019-08-03 stsp }
2155 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2156 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2157 63035f9f 2019-10-06 stsp a->repo, stdout);
2158 98eaaa12 2019-08-03 stsp }
2159 98eaaa12 2019-08-03 stsp
2160 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2161 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2162 4ce46740 2019-08-08 stsp char *id_str;
2163 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2164 408b4ebc 2019-08-03 stsp 8192);
2165 4ce46740 2019-08-08 stsp if (err)
2166 4ce46740 2019-08-08 stsp goto done;
2167 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2168 4ce46740 2019-08-08 stsp if (err)
2169 4ce46740 2019-08-08 stsp goto done;
2170 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2171 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2172 4ce46740 2019-08-08 stsp free(id_str);
2173 4ce46740 2019-08-08 stsp goto done;
2174 4ce46740 2019-08-08 stsp }
2175 4ce46740 2019-08-08 stsp free(id_str);
2176 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2177 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2178 4ce46740 2019-08-08 stsp if (err)
2179 4ce46740 2019-08-08 stsp goto done;
2180 4ce46740 2019-08-08 stsp }
2181 d00136be 2019-03-26 stsp
2182 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2183 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2184 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2185 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2186 2ec1f75b 2019-03-26 stsp goto done;
2187 2ec1f75b 2019-03-26 stsp }
2188 b72f483a 2019-02-05 stsp
2189 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2190 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2191 12463d8b 2019-12-13 stsp if (fd == -1) {
2192 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2193 12463d8b 2019-12-13 stsp goto done;
2194 12463d8b 2019-12-13 stsp }
2195 12463d8b 2019-12-13 stsp } else {
2196 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2197 12463d8b 2019-12-13 stsp if (fd == -1) {
2198 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2199 12463d8b 2019-12-13 stsp goto done;
2200 12463d8b 2019-12-13 stsp }
2201 12463d8b 2019-12-13 stsp }
2202 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2203 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2204 2ec1f75b 2019-03-26 stsp goto done;
2205 2ec1f75b 2019-03-26 stsp }
2206 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2207 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2208 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2209 2ec1f75b 2019-03-26 stsp goto done;
2210 2ec1f75b 2019-03-26 stsp }
2211 12463d8b 2019-12-13 stsp fd = -1;
2212 2ec1f75b 2019-03-26 stsp } else
2213 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2214 b72f483a 2019-02-05 stsp
2215 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2216 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2217 b72f483a 2019-02-05 stsp done:
2218 b72f483a 2019-02-05 stsp if (blob1)
2219 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2220 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2221 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2222 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2223 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2224 b72f483a 2019-02-05 stsp free(abspath);
2225 927df6b7 2019-02-10 stsp return err;
2226 927df6b7 2019-02-10 stsp }
2227 927df6b7 2019-02-10 stsp
2228 927df6b7 2019-02-10 stsp static const struct got_error *
2229 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
2230 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
2231 01073a5d 2019-08-22 stsp struct got_repository *repo)
2232 0adb7196 2019-08-11 stsp {
2233 0adb7196 2019-08-11 stsp const struct got_error *err;
2234 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
2235 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
2236 0adb7196 2019-08-11 stsp
2237 0adb7196 2019-08-11 stsp *id = NULL;
2238 0adb7196 2019-08-11 stsp *label = NULL;
2239 d24820bf 2019-08-11 stsp
2240 01073a5d 2019-08-22 stsp if (resolve_tags) {
2241 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2242 01073a5d 2019-08-22 stsp repo);
2243 01073a5d 2019-08-22 stsp if (err == NULL) {
2244 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
2245 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
2246 01073a5d 2019-08-22 stsp if (*id == NULL)
2247 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
2248 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
2249 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
2250 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
2251 32f0ab81 2019-08-28 hiltjo free(*id);
2252 01073a5d 2019-08-22 stsp *id = NULL;
2253 01073a5d 2019-08-22 stsp }
2254 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
2255 01073a5d 2019-08-22 stsp return err;
2256 562580bc 2020-01-14 stsp } else if (err->code != GOT_ERR_OBJ_TYPE &&
2257 562580bc 2020-01-14 stsp err->code != GOT_ERR_NO_OBJ)
2258 01073a5d 2019-08-22 stsp return err;
2259 01073a5d 2019-08-22 stsp }
2260 0adb7196 2019-08-11 stsp
2261 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2262 0adb7196 2019-08-11 stsp if (err) {
2263 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2264 0adb7196 2019-08-11 stsp return err;
2265 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
2266 0adb7196 2019-08-11 stsp if (err != NULL)
2267 0adb7196 2019-08-11 stsp goto done;
2268 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
2269 0adb7196 2019-08-11 stsp if (*label == NULL) {
2270 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2271 0adb7196 2019-08-11 stsp goto done;
2272 0adb7196 2019-08-11 stsp }
2273 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
2274 0adb7196 2019-08-11 stsp } else {
2275 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
2276 0adb7196 2019-08-11 stsp if (*label == NULL) {
2277 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2278 0adb7196 2019-08-11 stsp goto done;
2279 0adb7196 2019-08-11 stsp }
2280 0adb7196 2019-08-11 stsp }
2281 0adb7196 2019-08-11 stsp done:
2282 0adb7196 2019-08-11 stsp if (ref)
2283 0adb7196 2019-08-11 stsp got_ref_close(ref);
2284 0adb7196 2019-08-11 stsp return err;
2285 0adb7196 2019-08-11 stsp }
2286 0adb7196 2019-08-11 stsp
2287 0adb7196 2019-08-11 stsp
2288 0adb7196 2019-08-11 stsp static const struct got_error *
2289 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2290 b00d56cd 2018-04-01 stsp {
2291 b00d56cd 2018-04-01 stsp const struct got_error *error;
2292 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2293 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2294 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2295 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2296 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2297 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2298 15a94983 2018-12-23 stsp int type1, type2;
2299 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2300 c0cc5c62 2018-10-18 stsp const char *errstr;
2301 927df6b7 2019-02-10 stsp char *path = NULL;
2302 b00d56cd 2018-04-01 stsp
2303 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2304 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2305 25eccc22 2019-01-04 stsp NULL) == -1)
2306 b00d56cd 2018-04-01 stsp err(1, "pledge");
2307 b00d56cd 2018-04-01 stsp #endif
2308 b00d56cd 2018-04-01 stsp
2309 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2310 b00d56cd 2018-04-01 stsp switch (ch) {
2311 c0cc5c62 2018-10-18 stsp case 'C':
2312 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2313 dfcab68b 2019-11-29 kn &errstr);
2314 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2315 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2316 c0cc5c62 2018-10-18 stsp break;
2317 b72f483a 2019-02-05 stsp case 'r':
2318 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2319 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2320 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2321 9ba1d308 2019-10-21 stsp optarg);
2322 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2323 b72f483a 2019-02-05 stsp break;
2324 98eaaa12 2019-08-03 stsp case 's':
2325 98eaaa12 2019-08-03 stsp diff_staged = 1;
2326 63035f9f 2019-10-06 stsp break;
2327 63035f9f 2019-10-06 stsp case 'w':
2328 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2329 98eaaa12 2019-08-03 stsp break;
2330 b00d56cd 2018-04-01 stsp default:
2331 2deda0b9 2019-03-07 stsp usage_diff();
2332 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2333 b00d56cd 2018-04-01 stsp }
2334 b00d56cd 2018-04-01 stsp }
2335 b00d56cd 2018-04-01 stsp
2336 b00d56cd 2018-04-01 stsp argc -= optind;
2337 b00d56cd 2018-04-01 stsp argv += optind;
2338 b00d56cd 2018-04-01 stsp
2339 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2340 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2341 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2342 b72f483a 2019-02-05 stsp goto done;
2343 b72f483a 2019-02-05 stsp }
2344 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2345 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2346 927df6b7 2019-02-10 stsp goto done;
2347 927df6b7 2019-02-10 stsp if (argc <= 1) {
2348 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2349 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2350 927df6b7 2019-02-10 stsp goto done;
2351 927df6b7 2019-02-10 stsp }
2352 b72f483a 2019-02-05 stsp if (repo_path)
2353 b72f483a 2019-02-05 stsp errx(1,
2354 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2355 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2356 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2357 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2358 927df6b7 2019-02-10 stsp goto done;
2359 927df6b7 2019-02-10 stsp }
2360 927df6b7 2019-02-10 stsp if (argc == 1) {
2361 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2362 cbd1af7a 2019-03-18 stsp argv[0]);
2363 927df6b7 2019-02-10 stsp if (error)
2364 927df6b7 2019-02-10 stsp goto done;
2365 927df6b7 2019-02-10 stsp } else {
2366 927df6b7 2019-02-10 stsp path = strdup("");
2367 927df6b7 2019-02-10 stsp if (path == NULL) {
2368 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2369 927df6b7 2019-02-10 stsp goto done;
2370 927df6b7 2019-02-10 stsp }
2371 927df6b7 2019-02-10 stsp }
2372 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2373 98eaaa12 2019-08-03 stsp if (diff_staged)
2374 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2375 98eaaa12 2019-08-03 stsp "objects in repository");
2376 15a94983 2018-12-23 stsp id_str1 = argv[0];
2377 15a94983 2018-12-23 stsp id_str2 = argv[1];
2378 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2379 30db809c 2019-06-05 stsp repo_path =
2380 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2381 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2382 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2383 30db809c 2019-06-05 stsp goto done;
2384 30db809c 2019-06-05 stsp }
2385 30db809c 2019-06-05 stsp }
2386 b00d56cd 2018-04-01 stsp } else
2387 b00d56cd 2018-04-01 stsp usage_diff();
2388 25eccc22 2019-01-04 stsp
2389 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2390 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2391 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2392 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2393 b72f483a 2019-02-05 stsp }
2394 b00d56cd 2018-04-01 stsp
2395 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2396 76089277 2018-04-01 stsp free(repo_path);
2397 b00d56cd 2018-04-01 stsp if (error != NULL)
2398 b00d56cd 2018-04-01 stsp goto done;
2399 b00d56cd 2018-04-01 stsp
2400 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2401 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2402 c02c541e 2019-03-29 stsp if (error)
2403 c02c541e 2019-03-29 stsp goto done;
2404 c02c541e 2019-03-29 stsp
2405 30db809c 2019-06-05 stsp if (argc <= 1) {
2406 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2407 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2408 b72f483a 2019-02-05 stsp char *id_str;
2409 72ea6654 2019-07-27 stsp
2410 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2411 72ea6654 2019-07-27 stsp
2412 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2413 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2414 b72f483a 2019-02-05 stsp if (error)
2415 b72f483a 2019-02-05 stsp goto done;
2416 b72f483a 2019-02-05 stsp arg.repo = repo;
2417 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2418 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2419 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2420 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2421 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2422 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2423 b72f483a 2019-02-05 stsp
2424 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2425 72ea6654 2019-07-27 stsp if (error)
2426 72ea6654 2019-07-27 stsp goto done;
2427 72ea6654 2019-07-27 stsp
2428 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2429 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2430 b72f483a 2019-02-05 stsp free(id_str);
2431 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2432 b72f483a 2019-02-05 stsp goto done;
2433 b72f483a 2019-02-05 stsp }
2434 b72f483a 2019-02-05 stsp
2435 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2436 01073a5d 2019-08-22 stsp repo);
2437 0adb7196 2019-08-11 stsp if (error)
2438 0adb7196 2019-08-11 stsp goto done;
2439 b00d56cd 2018-04-01 stsp
2440 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2441 01073a5d 2019-08-22 stsp repo);
2442 0adb7196 2019-08-11 stsp if (error)
2443 0adb7196 2019-08-11 stsp goto done;
2444 b00d56cd 2018-04-01 stsp
2445 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2446 15a94983 2018-12-23 stsp if (error)
2447 15a94983 2018-12-23 stsp goto done;
2448 15a94983 2018-12-23 stsp
2449 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2450 15a94983 2018-12-23 stsp if (error)
2451 15a94983 2018-12-23 stsp goto done;
2452 15a94983 2018-12-23 stsp
2453 15a94983 2018-12-23 stsp if (type1 != type2) {
2454 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2455 b00d56cd 2018-04-01 stsp goto done;
2456 b00d56cd 2018-04-01 stsp }
2457 b00d56cd 2018-04-01 stsp
2458 15a94983 2018-12-23 stsp switch (type1) {
2459 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2460 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2461 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2462 b00d56cd 2018-04-01 stsp break;
2463 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2464 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2465 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2466 b00d56cd 2018-04-01 stsp break;
2467 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2468 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2469 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2470 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2471 b00d56cd 2018-04-01 stsp break;
2472 b00d56cd 2018-04-01 stsp default:
2473 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2474 b00d56cd 2018-04-01 stsp }
2475 b00d56cd 2018-04-01 stsp
2476 b00d56cd 2018-04-01 stsp done:
2477 5e70831e 2019-05-28 stsp free(label1);
2478 5e70831e 2019-05-28 stsp free(label2);
2479 15a94983 2018-12-23 stsp free(id1);
2480 15a94983 2018-12-23 stsp free(id2);
2481 927df6b7 2019-02-10 stsp free(path);
2482 b72f483a 2019-02-05 stsp if (worktree)
2483 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2484 ad242220 2018-09-08 stsp if (repo) {
2485 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2486 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2487 ad242220 2018-09-08 stsp if (error == NULL)
2488 ad242220 2018-09-08 stsp error = repo_error;
2489 ad242220 2018-09-08 stsp }
2490 b00d56cd 2018-04-01 stsp return error;
2491 404c43c4 2018-06-21 stsp }
2492 404c43c4 2018-06-21 stsp
2493 404c43c4 2018-06-21 stsp __dead static void
2494 404c43c4 2018-06-21 stsp usage_blame(void)
2495 404c43c4 2018-06-21 stsp {
2496 9270e621 2019-02-05 stsp fprintf(stderr,
2497 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2498 404c43c4 2018-06-21 stsp getprogname());
2499 404c43c4 2018-06-21 stsp exit(1);
2500 b00d56cd 2018-04-01 stsp }
2501 b00d56cd 2018-04-01 stsp
2502 28315671 2019-08-14 stsp struct blame_line {
2503 28315671 2019-08-14 stsp int annotated;
2504 28315671 2019-08-14 stsp char *id_str;
2505 82f6abb8 2019-08-14 stsp char *committer;
2506 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2507 28315671 2019-08-14 stsp };
2508 28315671 2019-08-14 stsp
2509 28315671 2019-08-14 stsp struct blame_cb_args {
2510 28315671 2019-08-14 stsp struct blame_line *lines;
2511 28315671 2019-08-14 stsp int nlines;
2512 7ef28ff8 2019-08-14 stsp int nlines_prec;
2513 28315671 2019-08-14 stsp int lineno_cur;
2514 28315671 2019-08-14 stsp off_t *line_offsets;
2515 28315671 2019-08-14 stsp FILE *f;
2516 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2517 28315671 2019-08-14 stsp };
2518 28315671 2019-08-14 stsp
2519 404c43c4 2018-06-21 stsp static const struct got_error *
2520 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2521 28315671 2019-08-14 stsp {
2522 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2523 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2524 28315671 2019-08-14 stsp struct blame_line *bline;
2525 82f6abb8 2019-08-14 stsp char *line = NULL;
2526 28315671 2019-08-14 stsp size_t linesize = 0;
2527 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2528 28315671 2019-08-14 stsp off_t offset;
2529 bcb49d15 2019-08-14 stsp struct tm tm;
2530 bcb49d15 2019-08-14 stsp time_t committer_time;
2531 28315671 2019-08-14 stsp
2532 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2533 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2534 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2535 28315671 2019-08-14 stsp
2536 28315671 2019-08-14 stsp if (sigint_received)
2537 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2538 28315671 2019-08-14 stsp
2539 2839f8b9 2019-08-18 stsp if (lineno == -1)
2540 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2541 2839f8b9 2019-08-18 stsp
2542 28315671 2019-08-14 stsp /* Annotate this line. */
2543 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2544 28315671 2019-08-14 stsp if (bline->annotated)
2545 28315671 2019-08-14 stsp return NULL;
2546 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2547 28315671 2019-08-14 stsp if (err)
2548 28315671 2019-08-14 stsp return err;
2549 82f6abb8 2019-08-14 stsp
2550 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2551 82f6abb8 2019-08-14 stsp if (err)
2552 82f6abb8 2019-08-14 stsp goto done;
2553 82f6abb8 2019-08-14 stsp
2554 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2555 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2556 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2557 bcb49d15 2019-08-14 stsp goto done;
2558 bcb49d15 2019-08-14 stsp }
2559 bcb49d15 2019-08-14 stsp
2560 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2561 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2562 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2563 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2564 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2565 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2566 82f6abb8 2019-08-14 stsp goto done;
2567 82f6abb8 2019-08-14 stsp }
2568 28315671 2019-08-14 stsp bline->annotated = 1;
2569 28315671 2019-08-14 stsp
2570 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2571 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2572 28315671 2019-08-14 stsp if (!bline->annotated)
2573 82f6abb8 2019-08-14 stsp goto done;
2574 28315671 2019-08-14 stsp
2575 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2576 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2577 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2578 82f6abb8 2019-08-14 stsp goto done;
2579 82f6abb8 2019-08-14 stsp }
2580 28315671 2019-08-14 stsp
2581 28315671 2019-08-14 stsp while (bline->annotated) {
2582 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2583 82f6abb8 2019-08-14 stsp size_t len;
2584 82f6abb8 2019-08-14 stsp
2585 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2586 28315671 2019-08-14 stsp if (ferror(a->f))
2587 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2588 28315671 2019-08-14 stsp break;
2589 28315671 2019-08-14 stsp }
2590 82f6abb8 2019-08-14 stsp
2591 82f6abb8 2019-08-14 stsp committer = bline->committer;
2592 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2593 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2594 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2595 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2596 82f6abb8 2019-08-14 stsp if (at)
2597 82f6abb8 2019-08-14 stsp *at = '\0';
2598 82f6abb8 2019-08-14 stsp len = strlen(committer);
2599 82f6abb8 2019-08-14 stsp if (len >= 9)
2600 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2601 28315671 2019-08-14 stsp
2602 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2603 28315671 2019-08-14 stsp if (nl)
2604 28315671 2019-08-14 stsp *nl = '\0';
2605 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2606 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2607 28315671 2019-08-14 stsp
2608 28315671 2019-08-14 stsp a->lineno_cur++;
2609 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2610 28315671 2019-08-14 stsp }
2611 82f6abb8 2019-08-14 stsp done:
2612 82f6abb8 2019-08-14 stsp if (commit)
2613 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2614 28315671 2019-08-14 stsp free(line);
2615 28315671 2019-08-14 stsp return err;
2616 28315671 2019-08-14 stsp }
2617 28315671 2019-08-14 stsp
2618 28315671 2019-08-14 stsp static const struct got_error *
2619 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2620 404c43c4 2018-06-21 stsp {
2621 404c43c4 2018-06-21 stsp const struct got_error *error;
2622 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2623 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2624 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2625 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2626 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2627 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2628 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2629 28315671 2019-08-14 stsp struct blame_cb_args bca;
2630 28315671 2019-08-14 stsp int ch, obj_type, i;
2631 b02560ec 2019-08-19 stsp size_t filesize;
2632 90f3c347 2019-08-19 stsp
2633 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2634 404c43c4 2018-06-21 stsp
2635 404c43c4 2018-06-21 stsp #ifndef PROFILE
2636 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2637 36e2fb66 2019-01-04 stsp NULL) == -1)
2638 404c43c4 2018-06-21 stsp err(1, "pledge");
2639 404c43c4 2018-06-21 stsp #endif
2640 404c43c4 2018-06-21 stsp
2641 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2642 404c43c4 2018-06-21 stsp switch (ch) {
2643 404c43c4 2018-06-21 stsp case 'c':
2644 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2645 404c43c4 2018-06-21 stsp break;
2646 66bea077 2018-08-02 stsp case 'r':
2647 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2648 66bea077 2018-08-02 stsp if (repo_path == NULL)
2649 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2650 9ba1d308 2019-10-21 stsp optarg);
2651 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2652 66bea077 2018-08-02 stsp break;
2653 404c43c4 2018-06-21 stsp default:
2654 2deda0b9 2019-03-07 stsp usage_blame();
2655 404c43c4 2018-06-21 stsp /* NOTREACHED */
2656 404c43c4 2018-06-21 stsp }
2657 404c43c4 2018-06-21 stsp }
2658 404c43c4 2018-06-21 stsp
2659 404c43c4 2018-06-21 stsp argc -= optind;
2660 404c43c4 2018-06-21 stsp argv += optind;
2661 404c43c4 2018-06-21 stsp
2662 a39318fd 2018-08-02 stsp if (argc == 1)
2663 404c43c4 2018-06-21 stsp path = argv[0];
2664 a39318fd 2018-08-02 stsp else
2665 404c43c4 2018-06-21 stsp usage_blame();
2666 404c43c4 2018-06-21 stsp
2667 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2668 66bea077 2018-08-02 stsp if (cwd == NULL) {
2669 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2670 66bea077 2018-08-02 stsp goto done;
2671 66bea077 2018-08-02 stsp }
2672 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2673 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2674 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2675 66bea077 2018-08-02 stsp goto done;
2676 0c06baac 2019-02-05 stsp else
2677 0c06baac 2019-02-05 stsp error = NULL;
2678 0c06baac 2019-02-05 stsp if (worktree) {
2679 0c06baac 2019-02-05 stsp repo_path =
2680 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2681 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2682 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2683 0c06baac 2019-02-05 stsp if (error)
2684 0c06baac 2019-02-05 stsp goto done;
2685 0c06baac 2019-02-05 stsp } else {
2686 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2687 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2688 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2689 0c06baac 2019-02-05 stsp goto done;
2690 0c06baac 2019-02-05 stsp }
2691 66bea077 2018-08-02 stsp }
2692 66bea077 2018-08-02 stsp }
2693 36e2fb66 2019-01-04 stsp
2694 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2695 c02c541e 2019-03-29 stsp if (error != NULL)
2696 36e2fb66 2019-01-04 stsp goto done;
2697 66bea077 2018-08-02 stsp
2698 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2699 c02c541e 2019-03-29 stsp if (error)
2700 404c43c4 2018-06-21 stsp goto done;
2701 404c43c4 2018-06-21 stsp
2702 0c06baac 2019-02-05 stsp if (worktree) {
2703 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2704 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2705 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2706 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2707 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2708 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2709 6efaaa2d 2019-02-05 stsp path) == -1) {
2710 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2711 0c06baac 2019-02-05 stsp goto done;
2712 0c06baac 2019-02-05 stsp }
2713 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2714 0c06baac 2019-02-05 stsp free(p);
2715 0c06baac 2019-02-05 stsp } else {
2716 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2717 0c06baac 2019-02-05 stsp }
2718 0c06baac 2019-02-05 stsp if (error)
2719 66bea077 2018-08-02 stsp goto done;
2720 66bea077 2018-08-02 stsp
2721 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2722 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2723 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2724 404c43c4 2018-06-21 stsp if (error != NULL)
2725 66bea077 2018-08-02 stsp goto done;
2726 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2727 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2728 404c43c4 2018-06-21 stsp if (error != NULL)
2729 66bea077 2018-08-02 stsp goto done;
2730 404c43c4 2018-06-21 stsp } else {
2731 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2732 30837e32 2019-07-25 stsp if (error)
2733 66bea077 2018-08-02 stsp goto done;
2734 404c43c4 2018-06-21 stsp }
2735 404c43c4 2018-06-21 stsp
2736 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2737 28315671 2019-08-14 stsp if (error)
2738 28315671 2019-08-14 stsp goto done;
2739 28315671 2019-08-14 stsp if (obj_id == NULL) {
2740 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2741 28315671 2019-08-14 stsp goto done;
2742 28315671 2019-08-14 stsp }
2743 28315671 2019-08-14 stsp
2744 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2745 28315671 2019-08-14 stsp if (error)
2746 28315671 2019-08-14 stsp goto done;
2747 28315671 2019-08-14 stsp
2748 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2749 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2750 28315671 2019-08-14 stsp goto done;
2751 28315671 2019-08-14 stsp }
2752 28315671 2019-08-14 stsp
2753 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2754 28315671 2019-08-14 stsp if (error)
2755 28315671 2019-08-14 stsp goto done;
2756 28315671 2019-08-14 stsp bca.f = got_opentemp();
2757 28315671 2019-08-14 stsp if (bca.f == NULL) {
2758 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2759 28315671 2019-08-14 stsp goto done;
2760 28315671 2019-08-14 stsp }
2761 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2762 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2763 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2764 28315671 2019-08-14 stsp goto done;
2765 28315671 2019-08-14 stsp
2766 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2767 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2768 b02560ec 2019-08-19 stsp bca.nlines--;
2769 b02560ec 2019-08-19 stsp
2770 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2771 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2772 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2773 28315671 2019-08-14 stsp goto done;
2774 28315671 2019-08-14 stsp }
2775 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2776 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2777 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2778 7ef28ff8 2019-08-14 stsp while (i > 0) {
2779 7ef28ff8 2019-08-14 stsp i /= 10;
2780 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2781 7ef28ff8 2019-08-14 stsp }
2782 82f6abb8 2019-08-14 stsp bca.repo = repo;
2783 7ef28ff8 2019-08-14 stsp
2784 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2785 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2786 28315671 2019-08-14 stsp if (error)
2787 28315671 2019-08-14 stsp goto done;
2788 404c43c4 2018-06-21 stsp done:
2789 66bea077 2018-08-02 stsp free(in_repo_path);
2790 66bea077 2018-08-02 stsp free(repo_path);
2791 66bea077 2018-08-02 stsp free(cwd);
2792 404c43c4 2018-06-21 stsp free(commit_id);
2793 28315671 2019-08-14 stsp free(obj_id);
2794 28315671 2019-08-14 stsp if (blob)
2795 28315671 2019-08-14 stsp got_object_blob_close(blob);
2796 0c06baac 2019-02-05 stsp if (worktree)
2797 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2798 ad242220 2018-09-08 stsp if (repo) {
2799 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2800 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2801 ad242220 2018-09-08 stsp if (error == NULL)
2802 ad242220 2018-09-08 stsp error = repo_error;
2803 ad242220 2018-09-08 stsp }
2804 3affba96 2019-09-22 stsp if (bca.lines) {
2805 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2806 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2807 3affba96 2019-09-22 stsp free(bline->id_str);
2808 3affba96 2019-09-22 stsp free(bline->committer);
2809 3affba96 2019-09-22 stsp }
2810 3affba96 2019-09-22 stsp free(bca.lines);
2811 28315671 2019-08-14 stsp }
2812 28315671 2019-08-14 stsp free(bca.line_offsets);
2813 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2814 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2815 404c43c4 2018-06-21 stsp return error;
2816 5de5890b 2018-10-18 stsp }
2817 5de5890b 2018-10-18 stsp
2818 5de5890b 2018-10-18 stsp __dead static void
2819 5de5890b 2018-10-18 stsp usage_tree(void)
2820 5de5890b 2018-10-18 stsp {
2821 c1669e2e 2019-01-09 stsp fprintf(stderr,
2822 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2823 5de5890b 2018-10-18 stsp getprogname());
2824 5de5890b 2018-10-18 stsp exit(1);
2825 5de5890b 2018-10-18 stsp }
2826 5de5890b 2018-10-18 stsp
2827 c1669e2e 2019-01-09 stsp static void
2828 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2829 c1669e2e 2019-01-09 stsp const char *root_path)
2830 c1669e2e 2019-01-09 stsp {
2831 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2832 848d6979 2019-08-12 stsp const char *modestr = "";
2833 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2834 5de5890b 2018-10-18 stsp
2835 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2836 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2837 c1669e2e 2019-01-09 stsp path++;
2838 c1669e2e 2019-01-09 stsp
2839 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2840 63c5ca5d 2019-08-24 stsp modestr = "$";
2841 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2842 848d6979 2019-08-12 stsp modestr = "@";
2843 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2844 848d6979 2019-08-12 stsp modestr = "/";
2845 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2846 848d6979 2019-08-12 stsp modestr = "*";
2847 848d6979 2019-08-12 stsp
2848 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2849 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2850 c1669e2e 2019-01-09 stsp }
2851 c1669e2e 2019-01-09 stsp
2852 5de5890b 2018-10-18 stsp static const struct got_error *
2853 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2854 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2855 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2856 5de5890b 2018-10-18 stsp {
2857 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2858 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2859 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2860 56e0773d 2019-11-28 stsp int nentries, i;
2861 5de5890b 2018-10-18 stsp
2862 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2863 5de5890b 2018-10-18 stsp if (err)
2864 5de5890b 2018-10-18 stsp goto done;
2865 5de5890b 2018-10-18 stsp
2866 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2867 5de5890b 2018-10-18 stsp if (err)
2868 5de5890b 2018-10-18 stsp goto done;
2869 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2870 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2871 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2872 5de5890b 2018-10-18 stsp char *id = NULL;
2873 84453469 2018-11-11 stsp
2874 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2875 84453469 2018-11-11 stsp break;
2876 84453469 2018-11-11 stsp
2877 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2878 5de5890b 2018-10-18 stsp if (show_ids) {
2879 5de5890b 2018-10-18 stsp char *id_str;
2880 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2881 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2882 5de5890b 2018-10-18 stsp if (err)
2883 5de5890b 2018-10-18 stsp goto done;
2884 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2885 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2886 5de5890b 2018-10-18 stsp free(id_str);
2887 5de5890b 2018-10-18 stsp goto done;
2888 5de5890b 2018-10-18 stsp }
2889 5de5890b 2018-10-18 stsp free(id_str);
2890 5de5890b 2018-10-18 stsp }
2891 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2892 5de5890b 2018-10-18 stsp free(id);
2893 c1669e2e 2019-01-09 stsp
2894 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2895 c1669e2e 2019-01-09 stsp char *child_path;
2896 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2897 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2898 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
2899 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2900 c1669e2e 2019-01-09 stsp goto done;
2901 c1669e2e 2019-01-09 stsp }
2902 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2903 c1669e2e 2019-01-09 stsp root_path, repo);
2904 c1669e2e 2019-01-09 stsp free(child_path);
2905 c1669e2e 2019-01-09 stsp if (err)
2906 c1669e2e 2019-01-09 stsp goto done;
2907 c1669e2e 2019-01-09 stsp }
2908 5de5890b 2018-10-18 stsp }
2909 5de5890b 2018-10-18 stsp done:
2910 5de5890b 2018-10-18 stsp if (tree)
2911 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2912 5de5890b 2018-10-18 stsp free(tree_id);
2913 5de5890b 2018-10-18 stsp return err;
2914 404c43c4 2018-06-21 stsp }
2915 404c43c4 2018-06-21 stsp
2916 5de5890b 2018-10-18 stsp static const struct got_error *
2917 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2918 5de5890b 2018-10-18 stsp {
2919 5de5890b 2018-10-18 stsp const struct got_error *error;
2920 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2921 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2922 7a2c19d6 2019-02-05 stsp const char *path;
2923 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2924 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2925 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2926 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2927 5de5890b 2018-10-18 stsp int ch;
2928 5de5890b 2018-10-18 stsp
2929 5de5890b 2018-10-18 stsp #ifndef PROFILE
2930 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2931 0f8d269b 2019-01-04 stsp NULL) == -1)
2932 5de5890b 2018-10-18 stsp err(1, "pledge");
2933 5de5890b 2018-10-18 stsp #endif
2934 5de5890b 2018-10-18 stsp
2935 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2936 5de5890b 2018-10-18 stsp switch (ch) {
2937 5de5890b 2018-10-18 stsp case 'c':
2938 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2939 5de5890b 2018-10-18 stsp break;
2940 5de5890b 2018-10-18 stsp case 'r':
2941 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2942 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2943 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2944 9ba1d308 2019-10-21 stsp optarg);
2945 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2946 5de5890b 2018-10-18 stsp break;
2947 5de5890b 2018-10-18 stsp case 'i':
2948 5de5890b 2018-10-18 stsp show_ids = 1;
2949 5de5890b 2018-10-18 stsp break;
2950 c1669e2e 2019-01-09 stsp case 'R':
2951 c1669e2e 2019-01-09 stsp recurse = 1;
2952 c1669e2e 2019-01-09 stsp break;
2953 5de5890b 2018-10-18 stsp default:
2954 2deda0b9 2019-03-07 stsp usage_tree();
2955 5de5890b 2018-10-18 stsp /* NOTREACHED */
2956 5de5890b 2018-10-18 stsp }
2957 5de5890b 2018-10-18 stsp }
2958 5de5890b 2018-10-18 stsp
2959 5de5890b 2018-10-18 stsp argc -= optind;
2960 5de5890b 2018-10-18 stsp argv += optind;
2961 5de5890b 2018-10-18 stsp
2962 5de5890b 2018-10-18 stsp if (argc == 1)
2963 5de5890b 2018-10-18 stsp path = argv[0];
2964 5de5890b 2018-10-18 stsp else if (argc > 1)
2965 5de5890b 2018-10-18 stsp usage_tree();
2966 5de5890b 2018-10-18 stsp else
2967 7a2c19d6 2019-02-05 stsp path = NULL;
2968 7a2c19d6 2019-02-05 stsp
2969 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2970 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2971 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2972 9bf7a39b 2019-02-05 stsp goto done;
2973 9bf7a39b 2019-02-05 stsp }
2974 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2975 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2976 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2977 7a2c19d6 2019-02-05 stsp goto done;
2978 7a2c19d6 2019-02-05 stsp else
2979 7a2c19d6 2019-02-05 stsp error = NULL;
2980 7a2c19d6 2019-02-05 stsp if (worktree) {
2981 7a2c19d6 2019-02-05 stsp repo_path =
2982 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2983 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2984 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2985 7a2c19d6 2019-02-05 stsp if (error)
2986 7a2c19d6 2019-02-05 stsp goto done;
2987 7a2c19d6 2019-02-05 stsp } else {
2988 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2989 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2990 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2991 7a2c19d6 2019-02-05 stsp goto done;
2992 7a2c19d6 2019-02-05 stsp }
2993 5de5890b 2018-10-18 stsp }
2994 5de5890b 2018-10-18 stsp }
2995 5de5890b 2018-10-18 stsp
2996 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2997 5de5890b 2018-10-18 stsp if (error != NULL)
2998 c02c541e 2019-03-29 stsp goto done;
2999 c02c541e 2019-03-29 stsp
3000 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3001 c02c541e 2019-03-29 stsp if (error)
3002 5de5890b 2018-10-18 stsp goto done;
3003 5de5890b 2018-10-18 stsp
3004 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3005 9bf7a39b 2019-02-05 stsp if (worktree) {
3006 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3007 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3008 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3009 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3010 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3011 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3012 9bf7a39b 2019-02-05 stsp goto done;
3013 9bf7a39b 2019-02-05 stsp }
3014 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
3015 9bf7a39b 2019-02-05 stsp free(p);
3016 9bf7a39b 2019-02-05 stsp if (error)
3017 9bf7a39b 2019-02-05 stsp goto done;
3018 9bf7a39b 2019-02-05 stsp } else
3019 9bf7a39b 2019-02-05 stsp path = "/";
3020 9bf7a39b 2019-02-05 stsp }
3021 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3022 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3023 9bf7a39b 2019-02-05 stsp if (error != NULL)
3024 9bf7a39b 2019-02-05 stsp goto done;
3025 9bf7a39b 2019-02-05 stsp }
3026 5de5890b 2018-10-18 stsp
3027 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3028 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3029 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3030 5de5890b 2018-10-18 stsp if (error != NULL)
3031 5de5890b 2018-10-18 stsp goto done;
3032 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3033 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3034 5de5890b 2018-10-18 stsp if (error != NULL)
3035 5de5890b 2018-10-18 stsp goto done;
3036 5de5890b 2018-10-18 stsp } else {
3037 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3038 30837e32 2019-07-25 stsp if (error)
3039 5de5890b 2018-10-18 stsp goto done;
3040 5de5890b 2018-10-18 stsp }
3041 5de5890b 2018-10-18 stsp
3042 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3043 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3044 5de5890b 2018-10-18 stsp done:
3045 5de5890b 2018-10-18 stsp free(in_repo_path);
3046 5de5890b 2018-10-18 stsp free(repo_path);
3047 5de5890b 2018-10-18 stsp free(cwd);
3048 5de5890b 2018-10-18 stsp free(commit_id);
3049 7a2c19d6 2019-02-05 stsp if (worktree)
3050 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3051 5de5890b 2018-10-18 stsp if (repo) {
3052 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3053 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3054 5de5890b 2018-10-18 stsp if (error == NULL)
3055 5de5890b 2018-10-18 stsp error = repo_error;
3056 5de5890b 2018-10-18 stsp }
3057 5de5890b 2018-10-18 stsp return error;
3058 5de5890b 2018-10-18 stsp }
3059 5de5890b 2018-10-18 stsp
3060 6bad629b 2019-02-04 stsp __dead static void
3061 6bad629b 2019-02-04 stsp usage_status(void)
3062 6bad629b 2019-02-04 stsp {
3063 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3064 6bad629b 2019-02-04 stsp exit(1);
3065 6bad629b 2019-02-04 stsp }
3066 5c860e29 2018-03-12 stsp
3067 b72f483a 2019-02-05 stsp static const struct got_error *
3068 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3069 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3070 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3071 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3072 6bad629b 2019-02-04 stsp {
3073 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3074 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3075 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3076 b72f483a 2019-02-05 stsp return NULL;
3077 6bad629b 2019-02-04 stsp }
3078 5c860e29 2018-03-12 stsp
3079 6bad629b 2019-02-04 stsp static const struct got_error *
3080 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3081 6bad629b 2019-02-04 stsp {
3082 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3083 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3084 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3085 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3086 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3087 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3088 a5edda0a 2019-07-27 stsp int ch;
3089 5c860e29 2018-03-12 stsp
3090 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3091 72ea6654 2019-07-27 stsp
3092 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3093 6bad629b 2019-02-04 stsp switch (ch) {
3094 5c860e29 2018-03-12 stsp default:
3095 2deda0b9 2019-03-07 stsp usage_status();
3096 6bad629b 2019-02-04 stsp /* NOTREACHED */
3097 5c860e29 2018-03-12 stsp }
3098 5c860e29 2018-03-12 stsp }
3099 5c860e29 2018-03-12 stsp
3100 6bad629b 2019-02-04 stsp argc -= optind;
3101 6bad629b 2019-02-04 stsp argv += optind;
3102 5c860e29 2018-03-12 stsp
3103 6bad629b 2019-02-04 stsp #ifndef PROFILE
3104 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3105 6bad629b 2019-02-04 stsp NULL) == -1)
3106 6bad629b 2019-02-04 stsp err(1, "pledge");
3107 f42b1b34 2018-03-12 stsp #endif
3108 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3109 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3110 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3111 927df6b7 2019-02-10 stsp goto done;
3112 927df6b7 2019-02-10 stsp }
3113 927df6b7 2019-02-10 stsp
3114 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3115 927df6b7 2019-02-10 stsp if (error != NULL)
3116 a5edda0a 2019-07-27 stsp goto done;
3117 6bad629b 2019-02-04 stsp
3118 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3119 c9956ddf 2019-09-08 stsp NULL);
3120 6bad629b 2019-02-04 stsp if (error != NULL)
3121 6bad629b 2019-02-04 stsp goto done;
3122 6bad629b 2019-02-04 stsp
3123 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3124 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3125 087fb88c 2019-08-04 stsp if (error)
3126 087fb88c 2019-08-04 stsp goto done;
3127 087fb88c 2019-08-04 stsp
3128 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3129 6bad629b 2019-02-04 stsp if (error)
3130 6bad629b 2019-02-04 stsp goto done;
3131 6bad629b 2019-02-04 stsp
3132 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3133 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3134 6bad629b 2019-02-04 stsp done:
3135 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3136 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3137 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3138 927df6b7 2019-02-10 stsp free(cwd);
3139 d0eebce4 2019-03-11 stsp return error;
3140 d0eebce4 2019-03-11 stsp }
3141 d0eebce4 2019-03-11 stsp
3142 d0eebce4 2019-03-11 stsp __dead static void
3143 d0eebce4 2019-03-11 stsp usage_ref(void)
3144 d0eebce4 2019-03-11 stsp {
3145 d0eebce4 2019-03-11 stsp fprintf(stderr,
3146 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3147 d0eebce4 2019-03-11 stsp getprogname());
3148 d0eebce4 2019-03-11 stsp exit(1);
3149 d0eebce4 2019-03-11 stsp }
3150 d0eebce4 2019-03-11 stsp
3151 d0eebce4 2019-03-11 stsp static const struct got_error *
3152 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3153 d0eebce4 2019-03-11 stsp {
3154 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3155 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3156 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3157 d0eebce4 2019-03-11 stsp
3158 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3159 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3160 d0eebce4 2019-03-11 stsp if (err)
3161 d0eebce4 2019-03-11 stsp return err;
3162 d0eebce4 2019-03-11 stsp
3163 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3164 d0eebce4 2019-03-11 stsp char *refstr;
3165 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3166 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3167 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3168 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3169 d0eebce4 2019-03-11 stsp free(refstr);
3170 d0eebce4 2019-03-11 stsp }
3171 d0eebce4 2019-03-11 stsp
3172 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3173 d0eebce4 2019-03-11 stsp return NULL;
3174 d0eebce4 2019-03-11 stsp }
3175 d0eebce4 2019-03-11 stsp
3176 d0eebce4 2019-03-11 stsp static const struct got_error *
3177 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3178 d0eebce4 2019-03-11 stsp {
3179 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3180 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3181 d0eebce4 2019-03-11 stsp
3182 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3183 d0eebce4 2019-03-11 stsp if (err)
3184 d0eebce4 2019-03-11 stsp return err;
3185 d0eebce4 2019-03-11 stsp
3186 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3187 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3188 d0eebce4 2019-03-11 stsp return err;
3189 d0eebce4 2019-03-11 stsp }
3190 d0eebce4 2019-03-11 stsp
3191 d0eebce4 2019-03-11 stsp static const struct got_error *
3192 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3193 d0eebce4 2019-03-11 stsp {
3194 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3195 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3196 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3197 d1644381 2019-07-14 stsp
3198 d1644381 2019-07-14 stsp /*
3199 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3200 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3201 d1644381 2019-07-14 stsp * an unintended typo.
3202 d1644381 2019-07-14 stsp */
3203 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3204 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3205 d0eebce4 2019-03-11 stsp
3206 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3207 dd88155e 2019-06-29 stsp repo);
3208 d83d9d5c 2019-05-13 stsp if (err) {
3209 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3210 d0eebce4 2019-03-11 stsp
3211 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3212 d83d9d5c 2019-05-13 stsp return err;
3213 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3214 d83d9d5c 2019-05-13 stsp if (err)
3215 d83d9d5c 2019-05-13 stsp return err;
3216 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3217 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3218 d83d9d5c 2019-05-13 stsp if (err)
3219 d83d9d5c 2019-05-13 stsp return err;
3220 d83d9d5c 2019-05-13 stsp }
3221 d83d9d5c 2019-05-13 stsp
3222 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3223 d0eebce4 2019-03-11 stsp if (err)
3224 d0eebce4 2019-03-11 stsp goto done;
3225 d0eebce4 2019-03-11 stsp
3226 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3227 d0eebce4 2019-03-11 stsp done:
3228 d0eebce4 2019-03-11 stsp if (ref)
3229 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3230 d0eebce4 2019-03-11 stsp free(id);
3231 d1c1ae5f 2019-08-12 stsp return err;
3232 d1c1ae5f 2019-08-12 stsp }
3233 d1c1ae5f 2019-08-12 stsp
3234 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3235 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3236 d1c1ae5f 2019-08-12 stsp {
3237 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3238 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3239 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3240 d1c1ae5f 2019-08-12 stsp
3241 d1c1ae5f 2019-08-12 stsp /*
3242 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3243 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3244 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3245 d1c1ae5f 2019-08-12 stsp */
3246 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3247 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3248 d1c1ae5f 2019-08-12 stsp
3249 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3250 d1c1ae5f 2019-08-12 stsp if (err)
3251 d1c1ae5f 2019-08-12 stsp return err;
3252 d1c1ae5f 2019-08-12 stsp
3253 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3254 d1c1ae5f 2019-08-12 stsp if (err)
3255 d1c1ae5f 2019-08-12 stsp goto done;
3256 d1c1ae5f 2019-08-12 stsp
3257 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3258 d1c1ae5f 2019-08-12 stsp done:
3259 d1c1ae5f 2019-08-12 stsp if (target_ref)
3260 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3261 d1c1ae5f 2019-08-12 stsp if (ref)
3262 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3263 d0eebce4 2019-03-11 stsp return err;
3264 d0eebce4 2019-03-11 stsp }
3265 d0eebce4 2019-03-11 stsp
3266 d0eebce4 2019-03-11 stsp static const struct got_error *
3267 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3268 d0eebce4 2019-03-11 stsp {
3269 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3270 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3271 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3272 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3273 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3274 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3275 d0eebce4 2019-03-11 stsp
3276 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3277 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3278 d0eebce4 2019-03-11 stsp switch (ch) {
3279 d0eebce4 2019-03-11 stsp case 'd':
3280 d0eebce4 2019-03-11 stsp delref = optarg;
3281 d0eebce4 2019-03-11 stsp break;
3282 d0eebce4 2019-03-11 stsp case 'r':
3283 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3284 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3285 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3286 9ba1d308 2019-10-21 stsp optarg);
3287 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3288 d0eebce4 2019-03-11 stsp break;
3289 d0eebce4 2019-03-11 stsp case 'l':
3290 d0eebce4 2019-03-11 stsp do_list = 1;
3291 d1c1ae5f 2019-08-12 stsp break;
3292 d1c1ae5f 2019-08-12 stsp case 's':
3293 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3294 d0eebce4 2019-03-11 stsp break;
3295 d0eebce4 2019-03-11 stsp default:
3296 d0eebce4 2019-03-11 stsp usage_ref();
3297 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3298 d0eebce4 2019-03-11 stsp }
3299 d0eebce4 2019-03-11 stsp }
3300 d0eebce4 2019-03-11 stsp
3301 d0eebce4 2019-03-11 stsp if (do_list && delref)
3302 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3303 d0eebce4 2019-03-11 stsp
3304 d0eebce4 2019-03-11 stsp argc -= optind;
3305 d0eebce4 2019-03-11 stsp argv += optind;
3306 d0eebce4 2019-03-11 stsp
3307 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3308 d1c1ae5f 2019-08-12 stsp if (create_symref)
3309 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3310 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3311 d0eebce4 2019-03-11 stsp if (argc > 0)
3312 d0eebce4 2019-03-11 stsp usage_ref();
3313 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3314 d0eebce4 2019-03-11 stsp usage_ref();
3315 d0eebce4 2019-03-11 stsp
3316 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3317 e0b57350 2019-03-12 stsp if (do_list) {
3318 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3319 e0b57350 2019-03-12 stsp NULL) == -1)
3320 e0b57350 2019-03-12 stsp err(1, "pledge");
3321 e0b57350 2019-03-12 stsp } else {
3322 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3323 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3324 e0b57350 2019-03-12 stsp err(1, "pledge");
3325 e0b57350 2019-03-12 stsp }
3326 d0eebce4 2019-03-11 stsp #endif
3327 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3328 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3329 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3330 d0eebce4 2019-03-11 stsp goto done;
3331 d0eebce4 2019-03-11 stsp }
3332 d0eebce4 2019-03-11 stsp
3333 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3334 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3335 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3336 d0eebce4 2019-03-11 stsp goto done;
3337 d0eebce4 2019-03-11 stsp else
3338 d0eebce4 2019-03-11 stsp error = NULL;
3339 d0eebce4 2019-03-11 stsp if (worktree) {
3340 d0eebce4 2019-03-11 stsp repo_path =
3341 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3342 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3343 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3344 d0eebce4 2019-03-11 stsp if (error)
3345 d0eebce4 2019-03-11 stsp goto done;
3346 d0eebce4 2019-03-11 stsp } else {
3347 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3348 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3349 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3350 d0eebce4 2019-03-11 stsp goto done;
3351 d0eebce4 2019-03-11 stsp }
3352 d0eebce4 2019-03-11 stsp }
3353 d0eebce4 2019-03-11 stsp }
3354 d0eebce4 2019-03-11 stsp
3355 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3356 d0eebce4 2019-03-11 stsp if (error != NULL)
3357 d0eebce4 2019-03-11 stsp goto done;
3358 d0eebce4 2019-03-11 stsp
3359 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3360 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3361 c02c541e 2019-03-29 stsp if (error)
3362 c02c541e 2019-03-29 stsp goto done;
3363 c02c541e 2019-03-29 stsp
3364 d0eebce4 2019-03-11 stsp if (do_list)
3365 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3366 d0eebce4 2019-03-11 stsp else if (delref)
3367 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3368 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3369 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3370 d0eebce4 2019-03-11 stsp else
3371 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3372 4e759de4 2019-06-26 stsp done:
3373 4e759de4 2019-06-26 stsp if (repo)
3374 4e759de4 2019-06-26 stsp got_repo_close(repo);
3375 4e759de4 2019-06-26 stsp if (worktree)
3376 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3377 4e759de4 2019-06-26 stsp free(cwd);
3378 4e759de4 2019-06-26 stsp free(repo_path);
3379 4e759de4 2019-06-26 stsp return error;
3380 4e759de4 2019-06-26 stsp }
3381 4e759de4 2019-06-26 stsp
3382 4e759de4 2019-06-26 stsp __dead static void
3383 4e759de4 2019-06-26 stsp usage_branch(void)
3384 4e759de4 2019-06-26 stsp {
3385 4e759de4 2019-06-26 stsp fprintf(stderr,
3386 a74f7e83 2019-11-10 stsp "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3387 a74f7e83 2019-11-10 stsp "[name]\n", getprogname());
3388 4e759de4 2019-06-26 stsp exit(1);
3389 4e759de4 2019-06-26 stsp }
3390 4e759de4 2019-06-26 stsp
3391 4e759de4 2019-06-26 stsp static const struct got_error *
3392 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3393 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3394 4e99b47e 2019-10-04 stsp {
3395 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3396 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3397 4e99b47e 2019-10-04 stsp char *refstr;
3398 4e99b47e 2019-10-04 stsp
3399 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3400 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3401 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3402 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3403 4e99b47e 2019-10-04 stsp
3404 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3405 4e99b47e 2019-10-04 stsp if (err)
3406 4e99b47e 2019-10-04 stsp return err;
3407 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3408 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3409 4e99b47e 2019-10-04 stsp marker = "* ";
3410 4e99b47e 2019-10-04 stsp else
3411 4e99b47e 2019-10-04 stsp marker = "~ ";
3412 4e99b47e 2019-10-04 stsp free(id);
3413 4e99b47e 2019-10-04 stsp }
3414 4e99b47e 2019-10-04 stsp
3415 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3416 4e99b47e 2019-10-04 stsp refname += 11;
3417 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3418 4e99b47e 2019-10-04 stsp refname += 18;
3419 4e99b47e 2019-10-04 stsp
3420 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3421 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3422 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3423 4e99b47e 2019-10-04 stsp
3424 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3425 4e99b47e 2019-10-04 stsp free(refstr);
3426 4e99b47e 2019-10-04 stsp return NULL;
3427 4e99b47e 2019-10-04 stsp }
3428 4e99b47e 2019-10-04 stsp
3429 4e99b47e 2019-10-04 stsp static const struct got_error *
3430 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3431 ad89fa31 2019-10-04 stsp {
3432 ad89fa31 2019-10-04 stsp const char *refname;
3433 ad89fa31 2019-10-04 stsp
3434 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3435 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3436 ad89fa31 2019-10-04 stsp
3437 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3438 ad89fa31 2019-10-04 stsp
3439 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3440 ad89fa31 2019-10-04 stsp refname += 11;
3441 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3442 ad89fa31 2019-10-04 stsp refname += 18;
3443 ad89fa31 2019-10-04 stsp
3444 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3445 ad89fa31 2019-10-04 stsp
3446 ad89fa31 2019-10-04 stsp return NULL;
3447 ad89fa31 2019-10-04 stsp }
3448 ad89fa31 2019-10-04 stsp
3449 ad89fa31 2019-10-04 stsp static const struct got_error *
3450 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3451 4e759de4 2019-06-26 stsp {
3452 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3453 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3454 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3455 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3456 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3457 4e759de4 2019-06-26 stsp
3458 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3459 ba882ee3 2019-07-11 stsp
3460 4e99b47e 2019-10-04 stsp if (worktree) {
3461 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3462 4e99b47e 2019-10-04 stsp worktree);
3463 4e99b47e 2019-10-04 stsp if (err)
3464 4e99b47e 2019-10-04 stsp return err;
3465 4e99b47e 2019-10-04 stsp
3466 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3467 4e99b47e 2019-10-04 stsp worktree);
3468 4e99b47e 2019-10-04 stsp if (err)
3469 4e99b47e 2019-10-04 stsp return err;
3470 4e99b47e 2019-10-04 stsp
3471 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3472 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3473 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3474 4e99b47e 2019-10-04 stsp if (err)
3475 4e99b47e 2019-10-04 stsp return err;
3476 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3477 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3478 4e99b47e 2019-10-04 stsp }
3479 4e99b47e 2019-10-04 stsp }
3480 4e99b47e 2019-10-04 stsp
3481 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3482 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3483 4e759de4 2019-06-26 stsp if (err)
3484 4e759de4 2019-06-26 stsp return err;
3485 4e759de4 2019-06-26 stsp
3486 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3487 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3488 4e759de4 2019-06-26 stsp
3489 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3490 4e759de4 2019-06-26 stsp return NULL;
3491 4e759de4 2019-06-26 stsp }
3492 4e759de4 2019-06-26 stsp
3493 4e759de4 2019-06-26 stsp static const struct got_error *
3494 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3495 45cd4e47 2019-08-25 stsp const char *branch_name)
3496 4e759de4 2019-06-26 stsp {
3497 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3498 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3499 4e759de4 2019-06-26 stsp char *refname;
3500 4e759de4 2019-06-26 stsp
3501 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3502 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3503 4e759de4 2019-06-26 stsp
3504 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3505 4e759de4 2019-06-26 stsp if (err)
3506 4e759de4 2019-06-26 stsp goto done;
3507 4e759de4 2019-06-26 stsp
3508 45cd4e47 2019-08-25 stsp if (worktree &&
3509 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3510 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3511 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3512 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3513 45cd4e47 2019-08-25 stsp goto done;
3514 45cd4e47 2019-08-25 stsp }
3515 45cd4e47 2019-08-25 stsp
3516 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3517 4e759de4 2019-06-26 stsp done:
3518 45cd4e47 2019-08-25 stsp if (ref)
3519 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3520 4e759de4 2019-06-26 stsp free(refname);
3521 4e759de4 2019-06-26 stsp return err;
3522 4e759de4 2019-06-26 stsp }
3523 4e759de4 2019-06-26 stsp
3524 4e759de4 2019-06-26 stsp static const struct got_error *
3525 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3526 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3527 4e759de4 2019-06-26 stsp {
3528 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3529 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3530 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3531 d3f84d51 2019-07-11 stsp
3532 d3f84d51 2019-07-11 stsp /*
3533 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3534 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3535 d3f84d51 2019-07-11 stsp * an unintended typo.
3536 d3f84d51 2019-07-11 stsp */
3537 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3538 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3539 4e759de4 2019-06-26 stsp
3540 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3541 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3542 4e759de4 2019-06-26 stsp goto done;
3543 4e759de4 2019-06-26 stsp }
3544 4e759de4 2019-06-26 stsp
3545 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3546 4e759de4 2019-06-26 stsp if (err == NULL) {
3547 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3548 4e759de4 2019-06-26 stsp goto done;
3549 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3550 4e759de4 2019-06-26 stsp goto done;
3551 4e759de4 2019-06-26 stsp
3552 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3553 4e759de4 2019-06-26 stsp if (err)
3554 4e759de4 2019-06-26 stsp goto done;
3555 4e759de4 2019-06-26 stsp
3556 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3557 d0eebce4 2019-03-11 stsp done:
3558 4e759de4 2019-06-26 stsp if (ref)
3559 4e759de4 2019-06-26 stsp got_ref_close(ref);
3560 4e759de4 2019-06-26 stsp free(base_refname);
3561 4e759de4 2019-06-26 stsp free(refname);
3562 4e759de4 2019-06-26 stsp return err;
3563 4e759de4 2019-06-26 stsp }
3564 4e759de4 2019-06-26 stsp
3565 4e759de4 2019-06-26 stsp static const struct got_error *
3566 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3567 4e759de4 2019-06-26 stsp {
3568 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3569 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3570 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3571 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3572 ad89fa31 2019-10-04 stsp int ch, do_list = 0, do_show = 0;
3573 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3574 4e759de4 2019-06-26 stsp
3575 a74f7e83 2019-11-10 stsp while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3576 4e759de4 2019-06-26 stsp switch (ch) {
3577 a74f7e83 2019-11-10 stsp case 'c':
3578 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3579 a74f7e83 2019-11-10 stsp break;
3580 4e759de4 2019-06-26 stsp case 'd':
3581 4e759de4 2019-06-26 stsp delref = optarg;
3582 4e759de4 2019-06-26 stsp break;
3583 4e759de4 2019-06-26 stsp case 'r':
3584 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3585 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3586 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3587 9ba1d308 2019-10-21 stsp optarg);
3588 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3589 4e759de4 2019-06-26 stsp break;
3590 4e759de4 2019-06-26 stsp case 'l':
3591 4e759de4 2019-06-26 stsp do_list = 1;
3592 4e759de4 2019-06-26 stsp break;
3593 4e759de4 2019-06-26 stsp default:
3594 4e759de4 2019-06-26 stsp usage_branch();
3595 4e759de4 2019-06-26 stsp /* NOTREACHED */
3596 4e759de4 2019-06-26 stsp }
3597 4e759de4 2019-06-26 stsp }
3598 4e759de4 2019-06-26 stsp
3599 4e759de4 2019-06-26 stsp if (do_list && delref)
3600 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3601 4e759de4 2019-06-26 stsp
3602 4e759de4 2019-06-26 stsp argc -= optind;
3603 4e759de4 2019-06-26 stsp argv += optind;
3604 ad89fa31 2019-10-04 stsp
3605 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3606 ad89fa31 2019-10-04 stsp do_show = 1;
3607 4e759de4 2019-06-26 stsp
3608 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3609 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3610 a74f7e83 2019-11-10 stsp
3611 4e759de4 2019-06-26 stsp if (do_list || delref) {
3612 4e759de4 2019-06-26 stsp if (argc > 0)
3613 4e759de4 2019-06-26 stsp usage_branch();
3614 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3615 4e759de4 2019-06-26 stsp usage_branch();
3616 4e759de4 2019-06-26 stsp
3617 4e759de4 2019-06-26 stsp #ifndef PROFILE
3618 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3619 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3620 4e759de4 2019-06-26 stsp NULL) == -1)
3621 4e759de4 2019-06-26 stsp err(1, "pledge");
3622 4e759de4 2019-06-26 stsp } else {
3623 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3624 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3625 4e759de4 2019-06-26 stsp err(1, "pledge");
3626 4e759de4 2019-06-26 stsp }
3627 4e759de4 2019-06-26 stsp #endif
3628 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3629 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3630 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3631 4e759de4 2019-06-26 stsp goto done;
3632 4e759de4 2019-06-26 stsp }
3633 4e759de4 2019-06-26 stsp
3634 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3635 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3636 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3637 4e759de4 2019-06-26 stsp goto done;
3638 4e759de4 2019-06-26 stsp else
3639 4e759de4 2019-06-26 stsp error = NULL;
3640 4e759de4 2019-06-26 stsp if (worktree) {
3641 4e759de4 2019-06-26 stsp repo_path =
3642 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3643 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3644 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3645 4e759de4 2019-06-26 stsp if (error)
3646 4e759de4 2019-06-26 stsp goto done;
3647 4e759de4 2019-06-26 stsp } else {
3648 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3649 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3650 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3651 4e759de4 2019-06-26 stsp goto done;
3652 4e759de4 2019-06-26 stsp }
3653 4e759de4 2019-06-26 stsp }
3654 4e759de4 2019-06-26 stsp }
3655 4e759de4 2019-06-26 stsp
3656 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3657 4e759de4 2019-06-26 stsp if (error != NULL)
3658 4e759de4 2019-06-26 stsp goto done;
3659 4e759de4 2019-06-26 stsp
3660 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3661 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3662 4e759de4 2019-06-26 stsp if (error)
3663 4e759de4 2019-06-26 stsp goto done;
3664 4e759de4 2019-06-26 stsp
3665 ad89fa31 2019-10-04 stsp if (do_show)
3666 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3667 ad89fa31 2019-10-04 stsp else if (do_list)
3668 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3669 4e759de4 2019-06-26 stsp else if (delref)
3670 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3671 4e759de4 2019-06-26 stsp else {
3672 a74f7e83 2019-11-10 stsp struct got_object_id *commit_id;
3673 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3674 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3675 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3676 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3677 a74f7e83 2019-11-10 stsp error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3678 a74f7e83 2019-11-10 stsp if (error)
3679 a74f7e83 2019-11-10 stsp goto done;
3680 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3681 a74f7e83 2019-11-10 stsp free(commit_id);
3682 4e759de4 2019-06-26 stsp }
3683 4e759de4 2019-06-26 stsp done:
3684 d0eebce4 2019-03-11 stsp if (repo)
3685 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3686 d0eebce4 2019-03-11 stsp if (worktree)
3687 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3688 d0eebce4 2019-03-11 stsp free(cwd);
3689 d0eebce4 2019-03-11 stsp free(repo_path);
3690 d00136be 2019-03-26 stsp return error;
3691 d00136be 2019-03-26 stsp }
3692 d00136be 2019-03-26 stsp
3693 8e7bd50a 2019-08-22 stsp
3694 d00136be 2019-03-26 stsp __dead static void
3695 8e7bd50a 2019-08-22 stsp usage_tag(void)
3696 8e7bd50a 2019-08-22 stsp {
3697 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3698 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3699 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3700 8e7bd50a 2019-08-22 stsp exit(1);
3701 b8bad2ba 2019-08-23 stsp }
3702 b8bad2ba 2019-08-23 stsp
3703 b8bad2ba 2019-08-23 stsp #if 0
3704 b8bad2ba 2019-08-23 stsp static const struct got_error *
3705 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3706 b8bad2ba 2019-08-23 stsp {
3707 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3708 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3709 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3710 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3711 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3712 b8bad2ba 2019-08-23 stsp
3713 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3714 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3715 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3716 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3717 b8bad2ba 2019-08-23 stsp if (err)
3718 b8bad2ba 2019-08-23 stsp return err;
3719 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3720 b8bad2ba 2019-08-23 stsp continue;
3721 b8bad2ba 2019-08-23 stsp } else {
3722 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3723 b8bad2ba 2019-08-23 stsp if (err)
3724 b8bad2ba 2019-08-23 stsp break;
3725 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3726 b8bad2ba 2019-08-23 stsp free(re_id);
3727 b8bad2ba 2019-08-23 stsp if (err)
3728 b8bad2ba 2019-08-23 stsp break;
3729 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3730 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3731 b8bad2ba 2019-08-23 stsp }
3732 b8bad2ba 2019-08-23 stsp
3733 b8bad2ba 2019-08-23 stsp while (se) {
3734 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3735 b8bad2ba 2019-08-23 stsp if (err)
3736 b8bad2ba 2019-08-23 stsp break;
3737 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3738 b8bad2ba 2019-08-23 stsp free(se_id);
3739 b8bad2ba 2019-08-23 stsp if (err)
3740 b8bad2ba 2019-08-23 stsp break;
3741 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3742 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3743 b8bad2ba 2019-08-23 stsp
3744 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3745 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3746 b8bad2ba 2019-08-23 stsp if (err)
3747 b8bad2ba 2019-08-23 stsp return err;
3748 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3749 b8bad2ba 2019-08-23 stsp break;
3750 b8bad2ba 2019-08-23 stsp }
3751 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3752 b8bad2ba 2019-08-23 stsp continue;
3753 b8bad2ba 2019-08-23 stsp }
3754 b8bad2ba 2019-08-23 stsp }
3755 b8bad2ba 2019-08-23 stsp done:
3756 b8bad2ba 2019-08-23 stsp return err;
3757 8e7bd50a 2019-08-22 stsp }
3758 b8bad2ba 2019-08-23 stsp #endif
3759 8e7bd50a 2019-08-22 stsp
3760 8e7bd50a 2019-08-22 stsp static const struct got_error *
3761 b8bad2ba 2019-08-23 stsp cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3762 b8bad2ba 2019-08-23 stsp struct got_reference *ref2)
3763 b8bad2ba 2019-08-23 stsp {
3764 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3765 b8bad2ba 2019-08-23 stsp struct got_repository *repo = arg;
3766 b8bad2ba 2019-08-23 stsp struct got_object_id *id1, *id2 = NULL;
3767 b8bad2ba 2019-08-23 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3768 d4efa91b 2020-01-14 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3769 b8bad2ba 2019-08-23 stsp time_t time1, time2;
3770 b8bad2ba 2019-08-23 stsp
3771 b8bad2ba 2019-08-23 stsp *cmp = 0;
3772 b8bad2ba 2019-08-23 stsp
3773 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id1, repo, ref1);
3774 b8bad2ba 2019-08-23 stsp if (err)
3775 b8bad2ba 2019-08-23 stsp return err;
3776 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag1, repo, id1);
3777 d4efa91b 2020-01-14 stsp if (err) {
3778 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE)
3779 d4efa91b 2020-01-14 stsp goto done;
3780 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3781 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit1, repo, id1);
3782 d4efa91b 2020-01-14 stsp if (err)
3783 d4efa91b 2020-01-14 stsp goto done;
3784 d4efa91b 2020-01-14 stsp time1 = got_object_commit_get_committer_time(commit1);
3785 d4efa91b 2020-01-14 stsp } else
3786 d4efa91b 2020-01-14 stsp time1 = got_object_tag_get_tagger_time(tag1);
3787 b8bad2ba 2019-08-23 stsp
3788 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id2, repo, ref2);
3789 b8bad2ba 2019-08-23 stsp if (err)
3790 b8bad2ba 2019-08-23 stsp goto done;
3791 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag2, repo, id2);
3792 d4efa91b 2020-01-14 stsp if (err) {
3793 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE)
3794 d4efa91b 2020-01-14 stsp goto done;
3795 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3796 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit2, repo, id2);
3797 d4efa91b 2020-01-14 stsp if (err)
3798 d4efa91b 2020-01-14 stsp goto done;
3799 d4efa91b 2020-01-14 stsp time2 = got_object_commit_get_committer_time(commit2);
3800 d4efa91b 2020-01-14 stsp } else
3801 d4efa91b 2020-01-14 stsp time2 = got_object_tag_get_tagger_time(tag2);
3802 b8bad2ba 2019-08-23 stsp
3803 b8bad2ba 2019-08-23 stsp /* Put latest tags first. */
3804 b8bad2ba 2019-08-23 stsp if (time1 < time2)
3805 b8bad2ba 2019-08-23 stsp *cmp = 1;
3806 b8bad2ba 2019-08-23 stsp else if (time1 > time2)
3807 b8bad2ba 2019-08-23 stsp *cmp = -1;
3808 b8bad2ba 2019-08-23 stsp else
3809 b8bad2ba 2019-08-23 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3810 b8bad2ba 2019-08-23 stsp done:
3811 b8bad2ba 2019-08-23 stsp free(id1);
3812 b8bad2ba 2019-08-23 stsp free(id2);
3813 b8bad2ba 2019-08-23 stsp if (tag1)
3814 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag1);
3815 b8bad2ba 2019-08-23 stsp if (tag2)
3816 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag2);
3817 d4efa91b 2020-01-14 stsp if (commit1)
3818 d4efa91b 2020-01-14 stsp got_object_commit_close(commit1);
3819 d4efa91b 2020-01-14 stsp if (commit2)
3820 d4efa91b 2020-01-14 stsp got_object_commit_close(commit2);
3821 b8bad2ba 2019-08-23 stsp return err;
3822 b8bad2ba 2019-08-23 stsp }
3823 b8bad2ba 2019-08-23 stsp
3824 b8bad2ba 2019-08-23 stsp static const struct got_error *
3825 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3826 8e7bd50a 2019-08-22 stsp {
3827 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3828 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3829 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3830 8e7bd50a 2019-08-22 stsp
3831 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3832 8e7bd50a 2019-08-22 stsp
3833 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3834 8e7bd50a 2019-08-22 stsp if (err)
3835 8e7bd50a 2019-08-22 stsp return err;
3836 8e7bd50a 2019-08-22 stsp
3837 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3838 8e7bd50a 2019-08-22 stsp const char *refname;
3839 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3840 8e7bd50a 2019-08-22 stsp char datebuf[26];
3841 d4efa91b 2020-01-14 stsp const char *tagger;
3842 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3843 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3844 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3845 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
3846 8e7bd50a 2019-08-22 stsp
3847 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3848 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3849 8e7bd50a 2019-08-22 stsp continue;
3850 8e7bd50a 2019-08-22 stsp refname += 10;
3851 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3852 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3853 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3854 8e7bd50a 2019-08-22 stsp break;
3855 8e7bd50a 2019-08-22 stsp }
3856 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3857 8e7bd50a 2019-08-22 stsp free(refstr);
3858 8e7bd50a 2019-08-22 stsp
3859 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3860 8e7bd50a 2019-08-22 stsp if (err)
3861 8e7bd50a 2019-08-22 stsp break;
3862 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3863 d4efa91b 2020-01-14 stsp if (err) {
3864 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3865 d4efa91b 2020-01-14 stsp free(id);
3866 d4efa91b 2020-01-14 stsp break;
3867 d4efa91b 2020-01-14 stsp }
3868 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3869 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
3870 d4efa91b 2020-01-14 stsp if (err) {
3871 d4efa91b 2020-01-14 stsp free(id);
3872 d4efa91b 2020-01-14 stsp break;
3873 d4efa91b 2020-01-14 stsp }
3874 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
3875 d4efa91b 2020-01-14 stsp tagger_time =
3876 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
3877 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
3878 d4efa91b 2020-01-14 stsp free(id);
3879 d4efa91b 2020-01-14 stsp if (err)
3880 d4efa91b 2020-01-14 stsp break;
3881 d4efa91b 2020-01-14 stsp } else {
3882 d4efa91b 2020-01-14 stsp free(id);
3883 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
3884 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3885 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
3886 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
3887 d4efa91b 2020-01-14 stsp if (err)
3888 d4efa91b 2020-01-14 stsp break;
3889 d4efa91b 2020-01-14 stsp }
3890 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
3891 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3892 2417344c 2019-08-23 stsp if (datestr)
3893 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3894 d4efa91b 2020-01-14 stsp if (commit)
3895 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3896 d4efa91b 2020-01-14 stsp else {
3897 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
3898 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
3899 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3900 d4efa91b 2020-01-14 stsp id_str);
3901 d4efa91b 2020-01-14 stsp break;
3902 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
3903 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3904 d4efa91b 2020-01-14 stsp id_str);
3905 d4efa91b 2020-01-14 stsp break;
3906 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
3907 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3908 d4efa91b 2020-01-14 stsp id_str);
3909 d4efa91b 2020-01-14 stsp break;
3910 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
3911 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3912 d4efa91b 2020-01-14 stsp id_str);
3913 d4efa91b 2020-01-14 stsp break;
3914 d4efa91b 2020-01-14 stsp default:
3915 d4efa91b 2020-01-14 stsp break;
3916 d4efa91b 2020-01-14 stsp }
3917 8e7bd50a 2019-08-22 stsp }
3918 8e7bd50a 2019-08-22 stsp free(id_str);
3919 d4efa91b 2020-01-14 stsp if (commit) {
3920 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
3921 d4efa91b 2020-01-14 stsp if (err)
3922 d4efa91b 2020-01-14 stsp break;
3923 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
3924 d4efa91b 2020-01-14 stsp } else {
3925 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3926 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
3927 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
3928 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
3929 d4efa91b 2020-01-14 stsp break;
3930 d4efa91b 2020-01-14 stsp }
3931 8e7bd50a 2019-08-22 stsp }
3932 8e7bd50a 2019-08-22 stsp
3933 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3934 8e7bd50a 2019-08-22 stsp do {
3935 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3936 8e7bd50a 2019-08-22 stsp if (line)
3937 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3938 8e7bd50a 2019-08-22 stsp } while (line);
3939 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3940 8e7bd50a 2019-08-22 stsp }
3941 8e7bd50a 2019-08-22 stsp
3942 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3943 8e7bd50a 2019-08-22 stsp return NULL;
3944 8e7bd50a 2019-08-22 stsp }
3945 8e7bd50a 2019-08-22 stsp
3946 8e7bd50a 2019-08-22 stsp static const struct got_error *
3947 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3948 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3949 8e7bd50a 2019-08-22 stsp {
3950 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3951 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3952 f372d5cd 2019-10-21 stsp char *editor = NULL;
3953 8e7bd50a 2019-08-22 stsp int fd = -1;
3954 8e7bd50a 2019-08-22 stsp
3955 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3956 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3957 8e7bd50a 2019-08-22 stsp goto done;
3958 8e7bd50a 2019-08-22 stsp }
3959 8e7bd50a 2019-08-22 stsp
3960 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3961 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3962 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3963 8e7bd50a 2019-08-22 stsp goto done;
3964 8e7bd50a 2019-08-22 stsp }
3965 8e7bd50a 2019-08-22 stsp
3966 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3967 8e7bd50a 2019-08-22 stsp if (err)
3968 8e7bd50a 2019-08-22 stsp goto done;
3969 8e7bd50a 2019-08-22 stsp
3970 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3971 8e7bd50a 2019-08-22 stsp close(fd);
3972 8e7bd50a 2019-08-22 stsp
3973 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3974 8e7bd50a 2019-08-22 stsp if (err)
3975 8e7bd50a 2019-08-22 stsp goto done;
3976 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3977 8e7bd50a 2019-08-22 stsp done:
3978 8e7bd50a 2019-08-22 stsp free(initial_content);
3979 8e7bd50a 2019-08-22 stsp free(template);
3980 8e7bd50a 2019-08-22 stsp free(editor);
3981 8e7bd50a 2019-08-22 stsp
3982 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3983 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3984 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3985 8e7bd50a 2019-08-22 stsp if (err) {
3986 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3987 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3988 8e7bd50a 2019-08-22 stsp }
3989 8e7bd50a 2019-08-22 stsp }
3990 8e7bd50a 2019-08-22 stsp return err;
3991 8e7bd50a 2019-08-22 stsp }
3992 8e7bd50a 2019-08-22 stsp
3993 8e7bd50a 2019-08-22 stsp static const struct got_error *
3994 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3995 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3996 8e7bd50a 2019-08-22 stsp {
3997 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3998 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3999 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4000 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4001 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4002 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4003 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4004 8e7bd50a 2019-08-22 stsp
4005 8e7bd50a 2019-08-22 stsp /*
4006 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4007 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4008 8e7bd50a 2019-08-22 stsp * an unintended typo.
4009 8e7bd50a 2019-08-22 stsp */
4010 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4011 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4012 8e7bd50a 2019-08-22 stsp
4013 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4014 8e7bd50a 2019-08-22 stsp if (err)
4015 8e7bd50a 2019-08-22 stsp return err;
4016 8e7bd50a 2019-08-22 stsp
4017 8e7bd50a 2019-08-22 stsp err = match_object_id(&commit_id, &label, commit_arg,
4018 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4019 8e7bd50a 2019-08-22 stsp if (err)
4020 8e7bd50a 2019-08-22 stsp goto done;
4021 8e7bd50a 2019-08-22 stsp
4022 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4023 8e7bd50a 2019-08-22 stsp if (err)
4024 8e7bd50a 2019-08-22 stsp goto done;
4025 8e7bd50a 2019-08-22 stsp
4026 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4027 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4028 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4029 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4030 8e7bd50a 2019-08-22 stsp goto done;
4031 8e7bd50a 2019-08-22 stsp }
4032 8e7bd50a 2019-08-22 stsp tag_name += 10;
4033 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4034 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4035 8e7bd50a 2019-08-22 stsp goto done;
4036 8e7bd50a 2019-08-22 stsp }
4037 8e7bd50a 2019-08-22 stsp
4038 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4039 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4040 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4041 8e7bd50a 2019-08-22 stsp goto done;
4042 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4043 8e7bd50a 2019-08-22 stsp goto done;
4044 8e7bd50a 2019-08-22 stsp
4045 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4046 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4047 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4048 f372d5cd 2019-10-21 stsp if (err) {
4049 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4050 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4051 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4052 8e7bd50a 2019-08-22 stsp goto done;
4053 f372d5cd 2019-10-21 stsp }
4054 8e7bd50a 2019-08-22 stsp }
4055 8e7bd50a 2019-08-22 stsp
4056 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4057 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4058 f372d5cd 2019-10-21 stsp if (err) {
4059 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4060 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4061 8e7bd50a 2019-08-22 stsp goto done;
4062 f372d5cd 2019-10-21 stsp }
4063 8e7bd50a 2019-08-22 stsp
4064 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4065 f372d5cd 2019-10-21 stsp if (err) {
4066 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4067 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4068 8e7bd50a 2019-08-22 stsp goto done;
4069 f372d5cd 2019-10-21 stsp }
4070 8e7bd50a 2019-08-22 stsp
4071 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4072 f372d5cd 2019-10-21 stsp if (err) {
4073 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4074 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4075 f372d5cd 2019-10-21 stsp goto done;
4076 f372d5cd 2019-10-21 stsp }
4077 8e7bd50a 2019-08-22 stsp
4078 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4079 f372d5cd 2019-10-21 stsp if (err) {
4080 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4081 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4082 f372d5cd 2019-10-21 stsp goto done;
4083 8e7bd50a 2019-08-22 stsp }
4084 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4085 8e7bd50a 2019-08-22 stsp done:
4086 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4087 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4088 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4089 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4090 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4091 f372d5cd 2019-10-21 stsp free(tag_id_str);
4092 8e7bd50a 2019-08-22 stsp if (ref)
4093 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4094 8e7bd50a 2019-08-22 stsp free(commit_id);
4095 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4096 8e7bd50a 2019-08-22 stsp free(refname);
4097 8e7bd50a 2019-08-22 stsp free(tagmsg);
4098 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4099 aba9c984 2019-09-08 stsp free(tagger);
4100 8e7bd50a 2019-08-22 stsp return err;
4101 8e7bd50a 2019-08-22 stsp }
4102 8e7bd50a 2019-08-22 stsp
4103 8e7bd50a 2019-08-22 stsp static const struct got_error *
4104 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4105 8e7bd50a 2019-08-22 stsp {
4106 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4107 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4108 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4109 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4110 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4111 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4112 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4113 8e7bd50a 2019-08-22 stsp
4114 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4115 8e7bd50a 2019-08-22 stsp switch (ch) {
4116 8e7bd50a 2019-08-22 stsp case 'm':
4117 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4118 8e7bd50a 2019-08-22 stsp break;
4119 8e7bd50a 2019-08-22 stsp case 'r':
4120 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4121 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4122 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4123 9ba1d308 2019-10-21 stsp optarg);
4124 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4125 8e7bd50a 2019-08-22 stsp break;
4126 8e7bd50a 2019-08-22 stsp case 'l':
4127 8e7bd50a 2019-08-22 stsp do_list = 1;
4128 8e7bd50a 2019-08-22 stsp break;
4129 8e7bd50a 2019-08-22 stsp default:
4130 8e7bd50a 2019-08-22 stsp usage_tag();
4131 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4132 8e7bd50a 2019-08-22 stsp }
4133 8e7bd50a 2019-08-22 stsp }
4134 8e7bd50a 2019-08-22 stsp
4135 8e7bd50a 2019-08-22 stsp argc -= optind;
4136 8e7bd50a 2019-08-22 stsp argv += optind;
4137 8e7bd50a 2019-08-22 stsp
4138 8e7bd50a 2019-08-22 stsp if (do_list) {
4139 8e7bd50a 2019-08-22 stsp if (tagmsg)
4140 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
4141 8e7bd50a 2019-08-22 stsp if (argc > 0)
4142 8e7bd50a 2019-08-22 stsp usage_tag();
4143 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
4144 8e7bd50a 2019-08-22 stsp usage_tag();
4145 a2887370 2019-08-23 stsp else if (argc > 1)
4146 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
4147 a2887370 2019-08-23 stsp tag_name = argv[0];
4148 8e7bd50a 2019-08-22 stsp
4149 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4150 8e7bd50a 2019-08-22 stsp if (do_list) {
4151 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4152 8e7bd50a 2019-08-22 stsp NULL) == -1)
4153 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4154 8e7bd50a 2019-08-22 stsp } else {
4155 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4156 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4157 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4158 8e7bd50a 2019-08-22 stsp }
4159 8e7bd50a 2019-08-22 stsp #endif
4160 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4161 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4162 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4163 8e7bd50a 2019-08-22 stsp goto done;
4164 8e7bd50a 2019-08-22 stsp }
4165 8e7bd50a 2019-08-22 stsp
4166 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4167 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4168 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4169 8e7bd50a 2019-08-22 stsp goto done;
4170 8e7bd50a 2019-08-22 stsp else
4171 8e7bd50a 2019-08-22 stsp error = NULL;
4172 8e7bd50a 2019-08-22 stsp if (worktree) {
4173 8e7bd50a 2019-08-22 stsp repo_path =
4174 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4175 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4176 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4177 8e7bd50a 2019-08-22 stsp if (error)
4178 8e7bd50a 2019-08-22 stsp goto done;
4179 8e7bd50a 2019-08-22 stsp } else {
4180 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4181 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4182 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4183 8e7bd50a 2019-08-22 stsp goto done;
4184 8e7bd50a 2019-08-22 stsp }
4185 8e7bd50a 2019-08-22 stsp }
4186 8e7bd50a 2019-08-22 stsp }
4187 8e7bd50a 2019-08-22 stsp
4188 8e7bd50a 2019-08-22 stsp if (do_list) {
4189 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4190 c9956ddf 2019-09-08 stsp if (error != NULL)
4191 c9956ddf 2019-09-08 stsp goto done;
4192 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4193 8e7bd50a 2019-08-22 stsp if (error)
4194 8e7bd50a 2019-08-22 stsp goto done;
4195 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4196 8e7bd50a 2019-08-22 stsp } else {
4197 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4198 c9956ddf 2019-09-08 stsp if (error)
4199 c9956ddf 2019-09-08 stsp goto done;
4200 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4201 c9956ddf 2019-09-08 stsp if (error != NULL)
4202 c9956ddf 2019-09-08 stsp goto done;
4203 c9956ddf 2019-09-08 stsp
4204 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4205 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4206 8e7bd50a 2019-08-22 stsp if (error)
4207 8e7bd50a 2019-08-22 stsp goto done;
4208 8e7bd50a 2019-08-22 stsp }
4209 8e7bd50a 2019-08-22 stsp
4210 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4211 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4212 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4213 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4214 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4215 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4216 8e7bd50a 2019-08-22 stsp if (error)
4217 8e7bd50a 2019-08-22 stsp goto done;
4218 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4219 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4220 8e7bd50a 2019-08-22 stsp if (error)
4221 8e7bd50a 2019-08-22 stsp goto done;
4222 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4223 8e7bd50a 2019-08-22 stsp free(commit_id);
4224 8e7bd50a 2019-08-22 stsp if (error)
4225 8e7bd50a 2019-08-22 stsp goto done;
4226 8e7bd50a 2019-08-22 stsp }
4227 8e7bd50a 2019-08-22 stsp
4228 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4229 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4230 8e7bd50a 2019-08-22 stsp }
4231 8e7bd50a 2019-08-22 stsp done:
4232 8e7bd50a 2019-08-22 stsp if (repo)
4233 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4234 8e7bd50a 2019-08-22 stsp if (worktree)
4235 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4236 8e7bd50a 2019-08-22 stsp free(cwd);
4237 8e7bd50a 2019-08-22 stsp free(repo_path);
4238 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4239 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4240 8e7bd50a 2019-08-22 stsp return error;
4241 8e7bd50a 2019-08-22 stsp }
4242 8e7bd50a 2019-08-22 stsp
4243 8e7bd50a 2019-08-22 stsp __dead static void
4244 d00136be 2019-03-26 stsp usage_add(void)
4245 d00136be 2019-03-26 stsp {
4246 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4247 022fae89 2019-12-06 tracey getprogname());
4248 d00136be 2019-03-26 stsp exit(1);
4249 d00136be 2019-03-26 stsp }
4250 d00136be 2019-03-26 stsp
4251 d00136be 2019-03-26 stsp static const struct got_error *
4252 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4253 4e68cba3 2019-11-23 stsp {
4254 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4255 4e68cba3 2019-11-23 stsp path++;
4256 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4257 4e68cba3 2019-11-23 stsp return NULL;
4258 4e68cba3 2019-11-23 stsp }
4259 4e68cba3 2019-11-23 stsp
4260 4e68cba3 2019-11-23 stsp static const struct got_error *
4261 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4262 d00136be 2019-03-26 stsp {
4263 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4264 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4265 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4266 1dd54920 2019-05-11 stsp char *cwd = NULL;
4267 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4268 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4269 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4270 1dd54920 2019-05-11 stsp
4271 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4272 d00136be 2019-03-26 stsp
4273 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4274 d00136be 2019-03-26 stsp switch (ch) {
4275 022fae89 2019-12-06 tracey case 'I':
4276 022fae89 2019-12-06 tracey no_ignores = 1;
4277 022fae89 2019-12-06 tracey break;
4278 4e68cba3 2019-11-23 stsp case 'R':
4279 4e68cba3 2019-11-23 stsp can_recurse = 1;
4280 4e68cba3 2019-11-23 stsp break;
4281 d00136be 2019-03-26 stsp default:
4282 d00136be 2019-03-26 stsp usage_add();
4283 d00136be 2019-03-26 stsp /* NOTREACHED */
4284 d00136be 2019-03-26 stsp }
4285 d00136be 2019-03-26 stsp }
4286 d00136be 2019-03-26 stsp
4287 d00136be 2019-03-26 stsp argc -= optind;
4288 d00136be 2019-03-26 stsp argv += optind;
4289 d00136be 2019-03-26 stsp
4290 43012d58 2019-07-14 stsp #ifndef PROFILE
4291 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4292 43012d58 2019-07-14 stsp NULL) == -1)
4293 43012d58 2019-07-14 stsp err(1, "pledge");
4294 43012d58 2019-07-14 stsp #endif
4295 723c305c 2019-05-11 jcs if (argc < 1)
4296 d00136be 2019-03-26 stsp usage_add();
4297 d00136be 2019-03-26 stsp
4298 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4299 d00136be 2019-03-26 stsp if (cwd == NULL) {
4300 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4301 d00136be 2019-03-26 stsp goto done;
4302 d00136be 2019-03-26 stsp }
4303 723c305c 2019-05-11 jcs
4304 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4305 d00136be 2019-03-26 stsp if (error)
4306 d00136be 2019-03-26 stsp goto done;
4307 d00136be 2019-03-26 stsp
4308 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4309 c9956ddf 2019-09-08 stsp NULL);
4310 031a5338 2019-03-26 stsp if (error != NULL)
4311 031a5338 2019-03-26 stsp goto done;
4312 031a5338 2019-03-26 stsp
4313 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4314 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4315 d00136be 2019-03-26 stsp if (error)
4316 d00136be 2019-03-26 stsp goto done;
4317 d00136be 2019-03-26 stsp
4318 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4319 6d022e97 2019-08-04 stsp if (error)
4320 022fae89 2019-12-06 tracey goto done;
4321 022fae89 2019-12-06 tracey
4322 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4323 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4324 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4325 6d022e97 2019-08-04 stsp goto done;
4326 723c305c 2019-05-11 jcs
4327 022fae89 2019-12-06 tracey }
4328 022fae89 2019-12-06 tracey
4329 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4330 4e68cba3 2019-11-23 stsp char *ondisk_path;
4331 4e68cba3 2019-11-23 stsp struct stat sb;
4332 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4333 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4334 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4335 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4336 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4337 4e68cba3 2019-11-23 stsp goto done;
4338 4e68cba3 2019-11-23 stsp }
4339 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4340 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4341 4e68cba3 2019-11-23 stsp free(ondisk_path);
4342 4e68cba3 2019-11-23 stsp continue;
4343 4e68cba3 2019-11-23 stsp }
4344 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4345 4e68cba3 2019-11-23 stsp ondisk_path);
4346 4e68cba3 2019-11-23 stsp free(ondisk_path);
4347 4e68cba3 2019-11-23 stsp goto done;
4348 4e68cba3 2019-11-23 stsp }
4349 4e68cba3 2019-11-23 stsp free(ondisk_path);
4350 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4351 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4352 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4353 4e68cba3 2019-11-23 stsp goto done;
4354 4e68cba3 2019-11-23 stsp }
4355 4e68cba3 2019-11-23 stsp }
4356 4e68cba3 2019-11-23 stsp }
4357 022fae89 2019-12-06 tracey
4358 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4359 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4360 d00136be 2019-03-26 stsp done:
4361 031a5338 2019-03-26 stsp if (repo)
4362 031a5338 2019-03-26 stsp got_repo_close(repo);
4363 d00136be 2019-03-26 stsp if (worktree)
4364 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4365 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4366 1dd54920 2019-05-11 stsp free((char *)pe->path);
4367 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4368 2ec1f75b 2019-03-26 stsp free(cwd);
4369 2ec1f75b 2019-03-26 stsp return error;
4370 2ec1f75b 2019-03-26 stsp }
4371 2ec1f75b 2019-03-26 stsp
4372 2ec1f75b 2019-03-26 stsp __dead static void
4373 648e4ef7 2019-07-09 stsp usage_remove(void)
4374 2ec1f75b 2019-03-26 stsp {
4375 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4376 f2a9dc41 2019-12-13 tracey getprogname());
4377 2ec1f75b 2019-03-26 stsp exit(1);
4378 2a06fe5f 2019-08-24 stsp }
4379 2a06fe5f 2019-08-24 stsp
4380 2a06fe5f 2019-08-24 stsp static const struct got_error *
4381 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4382 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4383 2a06fe5f 2019-08-24 stsp {
4384 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4385 f2a9dc41 2019-12-13 tracey path++;
4386 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4387 2a06fe5f 2019-08-24 stsp return NULL;
4388 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4389 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4390 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4391 2a06fe5f 2019-08-24 stsp return NULL;
4392 2ec1f75b 2019-03-26 stsp }
4393 2ec1f75b 2019-03-26 stsp
4394 2ec1f75b 2019-03-26 stsp static const struct got_error *
4395 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4396 2ec1f75b 2019-03-26 stsp {
4397 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4398 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4399 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4400 17ed4618 2019-06-02 stsp char *cwd = NULL;
4401 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4402 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4403 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4404 2ec1f75b 2019-03-26 stsp
4405 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4406 17ed4618 2019-06-02 stsp
4407 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4408 2ec1f75b 2019-03-26 stsp switch (ch) {
4409 2ec1f75b 2019-03-26 stsp case 'f':
4410 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4411 2ec1f75b 2019-03-26 stsp break;
4412 70e3e7f5 2019-12-13 tracey case 'k':
4413 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4414 70e3e7f5 2019-12-13 tracey break;
4415 f2a9dc41 2019-12-13 tracey case 'R':
4416 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4417 f2a9dc41 2019-12-13 tracey break;
4418 2ec1f75b 2019-03-26 stsp default:
4419 f2a9dc41 2019-12-13 tracey usage_remove();
4420 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4421 2ec1f75b 2019-03-26 stsp }
4422 2ec1f75b 2019-03-26 stsp }
4423 2ec1f75b 2019-03-26 stsp
4424 2ec1f75b 2019-03-26 stsp argc -= optind;
4425 2ec1f75b 2019-03-26 stsp argv += optind;
4426 2ec1f75b 2019-03-26 stsp
4427 43012d58 2019-07-14 stsp #ifndef PROFILE
4428 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4429 43012d58 2019-07-14 stsp NULL) == -1)
4430 43012d58 2019-07-14 stsp err(1, "pledge");
4431 43012d58 2019-07-14 stsp #endif
4432 17ed4618 2019-06-02 stsp if (argc < 1)
4433 648e4ef7 2019-07-09 stsp usage_remove();
4434 2ec1f75b 2019-03-26 stsp
4435 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4436 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4437 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4438 2ec1f75b 2019-03-26 stsp goto done;
4439 2ec1f75b 2019-03-26 stsp }
4440 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4441 2ec1f75b 2019-03-26 stsp if (error)
4442 2ec1f75b 2019-03-26 stsp goto done;
4443 2ec1f75b 2019-03-26 stsp
4444 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4445 c9956ddf 2019-09-08 stsp NULL);
4446 2af4a041 2019-05-11 jcs if (error)
4447 2ec1f75b 2019-03-26 stsp goto done;
4448 2ec1f75b 2019-03-26 stsp
4449 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4450 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4451 2ec1f75b 2019-03-26 stsp if (error)
4452 2ec1f75b 2019-03-26 stsp goto done;
4453 2ec1f75b 2019-03-26 stsp
4454 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4455 6d022e97 2019-08-04 stsp if (error)
4456 6d022e97 2019-08-04 stsp goto done;
4457 17ed4618 2019-06-02 stsp
4458 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4459 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4460 f2a9dc41 2019-12-13 tracey struct stat sb;
4461 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4462 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4463 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4464 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4465 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4466 f2a9dc41 2019-12-13 tracey goto done;
4467 f2a9dc41 2019-12-13 tracey }
4468 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4469 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4470 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4471 f2a9dc41 2019-12-13 tracey continue;
4472 f2a9dc41 2019-12-13 tracey }
4473 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4474 f2a9dc41 2019-12-13 tracey ondisk_path);
4475 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4476 f2a9dc41 2019-12-13 tracey goto done;
4477 f2a9dc41 2019-12-13 tracey }
4478 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4479 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4480 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4481 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4482 f2a9dc41 2019-12-13 tracey goto done;
4483 f2a9dc41 2019-12-13 tracey }
4484 f2a9dc41 2019-12-13 tracey }
4485 f2a9dc41 2019-12-13 tracey }
4486 f2a9dc41 2019-12-13 tracey
4487 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4488 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4489 a129376b 2019-03-28 stsp if (error)
4490 a129376b 2019-03-28 stsp goto done;
4491 a129376b 2019-03-28 stsp done:
4492 a129376b 2019-03-28 stsp if (repo)
4493 a129376b 2019-03-28 stsp got_repo_close(repo);
4494 a129376b 2019-03-28 stsp if (worktree)
4495 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4496 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4497 17ed4618 2019-06-02 stsp free((char *)pe->path);
4498 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4499 a129376b 2019-03-28 stsp free(cwd);
4500 a129376b 2019-03-28 stsp return error;
4501 a129376b 2019-03-28 stsp }
4502 a129376b 2019-03-28 stsp
4503 a129376b 2019-03-28 stsp __dead static void
4504 a129376b 2019-03-28 stsp usage_revert(void)
4505 a129376b 2019-03-28 stsp {
4506 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4507 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4508 a129376b 2019-03-28 stsp exit(1);
4509 a129376b 2019-03-28 stsp }
4510 a129376b 2019-03-28 stsp
4511 1ee397ad 2019-07-12 stsp static const struct got_error *
4512 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4513 a129376b 2019-03-28 stsp {
4514 a129376b 2019-03-28 stsp while (path[0] == '/')
4515 a129376b 2019-03-28 stsp path++;
4516 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4517 33aa809d 2019-08-08 stsp return NULL;
4518 33aa809d 2019-08-08 stsp }
4519 33aa809d 2019-08-08 stsp
4520 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4521 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4522 33aa809d 2019-08-08 stsp const char *action;
4523 33aa809d 2019-08-08 stsp };
4524 33aa809d 2019-08-08 stsp
4525 33aa809d 2019-08-08 stsp static const struct got_error *
4526 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4527 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4528 33aa809d 2019-08-08 stsp {
4529 33aa809d 2019-08-08 stsp char *line = NULL;
4530 33aa809d 2019-08-08 stsp size_t linesize = 0;
4531 33aa809d 2019-08-08 stsp ssize_t linelen;
4532 33aa809d 2019-08-08 stsp
4533 33aa809d 2019-08-08 stsp switch (status) {
4534 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4535 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4536 33aa809d 2019-08-08 stsp break;
4537 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4538 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4539 33aa809d 2019-08-08 stsp break;
4540 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4541 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4542 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4543 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4544 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4545 33aa809d 2019-08-08 stsp printf("%s", line);
4546 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4547 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4548 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4549 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4550 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4551 33aa809d 2019-08-08 stsp break;
4552 33aa809d 2019-08-08 stsp default:
4553 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4554 33aa809d 2019-08-08 stsp }
4555 33aa809d 2019-08-08 stsp
4556 33aa809d 2019-08-08 stsp return NULL;
4557 33aa809d 2019-08-08 stsp }
4558 33aa809d 2019-08-08 stsp
4559 33aa809d 2019-08-08 stsp static const struct got_error *
4560 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4561 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4562 33aa809d 2019-08-08 stsp {
4563 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4564 33aa809d 2019-08-08 stsp char *line = NULL;
4565 33aa809d 2019-08-08 stsp size_t linesize = 0;
4566 33aa809d 2019-08-08 stsp ssize_t linelen;
4567 33aa809d 2019-08-08 stsp int resp = ' ';
4568 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4569 33aa809d 2019-08-08 stsp
4570 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4571 33aa809d 2019-08-08 stsp
4572 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4573 33aa809d 2019-08-08 stsp char *nl;
4574 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4575 33aa809d 2019-08-08 stsp a->action);
4576 33aa809d 2019-08-08 stsp if (err)
4577 33aa809d 2019-08-08 stsp return err;
4578 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4579 33aa809d 2019-08-08 stsp if (linelen == -1) {
4580 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4581 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4582 33aa809d 2019-08-08 stsp return NULL;
4583 33aa809d 2019-08-08 stsp }
4584 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4585 33aa809d 2019-08-08 stsp if (nl)
4586 33aa809d 2019-08-08 stsp *nl = '\0';
4587 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4588 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4589 33aa809d 2019-08-08 stsp printf("y\n");
4590 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4591 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4592 33aa809d 2019-08-08 stsp printf("n\n");
4593 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4594 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4595 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4596 33aa809d 2019-08-08 stsp printf("q\n");
4597 33aa809d 2019-08-08 stsp } else
4598 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4599 33aa809d 2019-08-08 stsp free(line);
4600 33aa809d 2019-08-08 stsp return NULL;
4601 33aa809d 2019-08-08 stsp }
4602 33aa809d 2019-08-08 stsp
4603 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4604 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4605 33aa809d 2019-08-08 stsp a->action);
4606 33aa809d 2019-08-08 stsp if (err)
4607 33aa809d 2019-08-08 stsp return err;
4608 33aa809d 2019-08-08 stsp resp = getchar();
4609 33aa809d 2019-08-08 stsp if (resp == '\n')
4610 33aa809d 2019-08-08 stsp resp = getchar();
4611 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4612 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4613 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4614 33aa809d 2019-08-08 stsp resp = ' ';
4615 33aa809d 2019-08-08 stsp }
4616 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4617 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4618 33aa809d 2019-08-08 stsp resp = ' ';
4619 33aa809d 2019-08-08 stsp }
4620 33aa809d 2019-08-08 stsp }
4621 33aa809d 2019-08-08 stsp
4622 33aa809d 2019-08-08 stsp if (resp == 'y')
4623 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4624 33aa809d 2019-08-08 stsp else if (resp == 'n')
4625 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4626 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4627 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4628 33aa809d 2019-08-08 stsp
4629 1ee397ad 2019-07-12 stsp return NULL;
4630 a129376b 2019-03-28 stsp }
4631 a129376b 2019-03-28 stsp
4632 33aa809d 2019-08-08 stsp
4633 a129376b 2019-03-28 stsp static const struct got_error *
4634 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4635 a129376b 2019-03-28 stsp {
4636 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4637 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4638 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4639 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4640 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4641 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4642 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4643 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4644 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4645 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4646 a129376b 2019-03-28 stsp
4647 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4648 e20a8b6f 2019-06-04 stsp
4649 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4650 a129376b 2019-03-28 stsp switch (ch) {
4651 33aa809d 2019-08-08 stsp case 'p':
4652 33aa809d 2019-08-08 stsp pflag = 1;
4653 33aa809d 2019-08-08 stsp break;
4654 33aa809d 2019-08-08 stsp case 'F':
4655 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4656 33aa809d 2019-08-08 stsp break;
4657 0f6d7415 2019-08-08 stsp case 'R':
4658 0f6d7415 2019-08-08 stsp can_recurse = 1;
4659 0f6d7415 2019-08-08 stsp break;
4660 a129376b 2019-03-28 stsp default:
4661 a129376b 2019-03-28 stsp usage_revert();
4662 a129376b 2019-03-28 stsp /* NOTREACHED */
4663 a129376b 2019-03-28 stsp }
4664 a129376b 2019-03-28 stsp }
4665 a129376b 2019-03-28 stsp
4666 a129376b 2019-03-28 stsp argc -= optind;
4667 a129376b 2019-03-28 stsp argv += optind;
4668 a129376b 2019-03-28 stsp
4669 43012d58 2019-07-14 stsp #ifndef PROFILE
4670 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4671 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4672 43012d58 2019-07-14 stsp err(1, "pledge");
4673 43012d58 2019-07-14 stsp #endif
4674 e20a8b6f 2019-06-04 stsp if (argc < 1)
4675 a129376b 2019-03-28 stsp usage_revert();
4676 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4677 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4678 a129376b 2019-03-28 stsp
4679 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4680 a129376b 2019-03-28 stsp if (cwd == NULL) {
4681 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4682 a129376b 2019-03-28 stsp goto done;
4683 a129376b 2019-03-28 stsp }
4684 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4685 2ec1f75b 2019-03-26 stsp if (error)
4686 2ec1f75b 2019-03-26 stsp goto done;
4687 a129376b 2019-03-28 stsp
4688 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4689 c9956ddf 2019-09-08 stsp NULL);
4690 a129376b 2019-03-28 stsp if (error != NULL)
4691 a129376b 2019-03-28 stsp goto done;
4692 a129376b 2019-03-28 stsp
4693 33aa809d 2019-08-08 stsp if (patch_script_path) {
4694 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4695 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4696 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4697 33aa809d 2019-08-08 stsp patch_script_path);
4698 33aa809d 2019-08-08 stsp goto done;
4699 33aa809d 2019-08-08 stsp }
4700 33aa809d 2019-08-08 stsp }
4701 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4702 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4703 a129376b 2019-03-28 stsp if (error)
4704 a129376b 2019-03-28 stsp goto done;
4705 a129376b 2019-03-28 stsp
4706 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4707 6d022e97 2019-08-04 stsp if (error)
4708 6d022e97 2019-08-04 stsp goto done;
4709 00db391e 2019-08-03 stsp
4710 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4711 0f6d7415 2019-08-08 stsp char *ondisk_path;
4712 0f6d7415 2019-08-08 stsp struct stat sb;
4713 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4714 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4715 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4716 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4717 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4718 0f6d7415 2019-08-08 stsp goto done;
4719 0f6d7415 2019-08-08 stsp }
4720 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4721 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4722 0f6d7415 2019-08-08 stsp free(ondisk_path);
4723 0f6d7415 2019-08-08 stsp continue;
4724 0f6d7415 2019-08-08 stsp }
4725 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4726 0f6d7415 2019-08-08 stsp ondisk_path);
4727 0f6d7415 2019-08-08 stsp free(ondisk_path);
4728 0f6d7415 2019-08-08 stsp goto done;
4729 0f6d7415 2019-08-08 stsp }
4730 0f6d7415 2019-08-08 stsp free(ondisk_path);
4731 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4732 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4733 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4734 0f6d7415 2019-08-08 stsp goto done;
4735 0f6d7415 2019-08-08 stsp }
4736 0f6d7415 2019-08-08 stsp }
4737 0f6d7415 2019-08-08 stsp }
4738 0f6d7415 2019-08-08 stsp
4739 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4740 33aa809d 2019-08-08 stsp cpa.action = "revert";
4741 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4742 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4743 a129376b 2019-03-28 stsp if (error)
4744 a129376b 2019-03-28 stsp goto done;
4745 2ec1f75b 2019-03-26 stsp done:
4746 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4747 33aa809d 2019-08-08 stsp error == NULL)
4748 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4749 2ec1f75b 2019-03-26 stsp if (repo)
4750 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4751 2ec1f75b 2019-03-26 stsp if (worktree)
4752 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4753 2ec1f75b 2019-03-26 stsp free(path);
4754 d00136be 2019-03-26 stsp free(cwd);
4755 6bad629b 2019-02-04 stsp return error;
4756 c4296144 2019-05-09 stsp }
4757 c4296144 2019-05-09 stsp
4758 c4296144 2019-05-09 stsp __dead static void
4759 c4296144 2019-05-09 stsp usage_commit(void)
4760 c4296144 2019-05-09 stsp {
4761 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4762 5c1e53bc 2019-07-28 stsp getprogname());
4763 c4296144 2019-05-09 stsp exit(1);
4764 33ad4cbe 2019-05-12 jcs }
4765 33ad4cbe 2019-05-12 jcs
4766 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4767 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4768 e2ba3d07 2019-05-13 stsp const char *editor;
4769 e0870e44 2019-05-13 stsp const char *worktree_path;
4770 76d98825 2019-06-03 stsp const char *branch_name;
4771 314a6357 2019-05-13 stsp const char *repo_path;
4772 e0870e44 2019-05-13 stsp char *logmsg_path;
4773 e2ba3d07 2019-05-13 stsp
4774 e2ba3d07 2019-05-13 stsp };
4775 e0870e44 2019-05-13 stsp
4776 33ad4cbe 2019-05-12 jcs static const struct got_error *
4777 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4778 33ad4cbe 2019-05-12 jcs void *arg)
4779 33ad4cbe 2019-05-12 jcs {
4780 76d98825 2019-06-03 stsp char *initial_content = NULL;
4781 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4782 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4783 e0870e44 2019-05-13 stsp char *template = NULL;
4784 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4785 3ce1b845 2019-07-15 stsp int fd;
4786 33ad4cbe 2019-05-12 jcs size_t len;
4787 33ad4cbe 2019-05-12 jcs
4788 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4789 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4790 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4791 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4792 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4793 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4794 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4795 33ad4cbe 2019-05-12 jcs return NULL;
4796 33ad4cbe 2019-05-12 jcs }
4797 33ad4cbe 2019-05-12 jcs
4798 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4799 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4800 76d98825 2019-06-03 stsp
4801 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4802 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4803 76d98825 2019-06-03 stsp a->branch_name) == -1)
4804 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4805 e0870e44 2019-05-13 stsp
4806 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4807 793c30b5 2019-05-13 stsp if (err)
4808 e0870e44 2019-05-13 stsp goto done;
4809 33ad4cbe 2019-05-12 jcs
4810 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4811 33ad4cbe 2019-05-12 jcs
4812 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4813 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4814 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4815 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4816 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4817 33ad4cbe 2019-05-12 jcs }
4818 33ad4cbe 2019-05-12 jcs close(fd);
4819 33ad4cbe 2019-05-12 jcs
4820 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4821 33ad4cbe 2019-05-12 jcs done:
4822 76d98825 2019-06-03 stsp free(initial_content);
4823 e0870e44 2019-05-13 stsp free(template);
4824 314a6357 2019-05-13 stsp
4825 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4826 3ce1b845 2019-07-15 stsp if (err == NULL) {
4827 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4828 3ce1b845 2019-07-15 stsp if (err) {
4829 3ce1b845 2019-07-15 stsp free(*logmsg);
4830 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4831 3ce1b845 2019-07-15 stsp }
4832 3ce1b845 2019-07-15 stsp }
4833 33ad4cbe 2019-05-12 jcs return err;
4834 6bad629b 2019-02-04 stsp }
4835 c4296144 2019-05-09 stsp
4836 c4296144 2019-05-09 stsp static const struct got_error *
4837 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4838 c4296144 2019-05-09 stsp {
4839 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4840 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4841 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4842 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4843 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4844 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4845 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4846 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4847 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4848 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4849 5c1e53bc 2019-07-28 stsp
4850 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4851 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4852 c4296144 2019-05-09 stsp
4853 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4854 c4296144 2019-05-09 stsp switch (ch) {
4855 c4296144 2019-05-09 stsp case 'm':
4856 c4296144 2019-05-09 stsp logmsg = optarg;
4857 c4296144 2019-05-09 stsp break;
4858 c4296144 2019-05-09 stsp default:
4859 c4296144 2019-05-09 stsp usage_commit();
4860 c4296144 2019-05-09 stsp /* NOTREACHED */
4861 c4296144 2019-05-09 stsp }
4862 c4296144 2019-05-09 stsp }
4863 c4296144 2019-05-09 stsp
4864 c4296144 2019-05-09 stsp argc -= optind;
4865 c4296144 2019-05-09 stsp argv += optind;
4866 c4296144 2019-05-09 stsp
4867 43012d58 2019-07-14 stsp #ifndef PROFILE
4868 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4869 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4870 43012d58 2019-07-14 stsp err(1, "pledge");
4871 43012d58 2019-07-14 stsp #endif
4872 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4873 c4296144 2019-05-09 stsp if (cwd == NULL) {
4874 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4875 c4296144 2019-05-09 stsp goto done;
4876 c4296144 2019-05-09 stsp }
4877 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4878 7d5807f4 2019-07-11 stsp if (error)
4879 7d5807f4 2019-07-11 stsp goto done;
4880 7d5807f4 2019-07-11 stsp
4881 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4882 c4296144 2019-05-09 stsp if (error)
4883 a698f62e 2019-07-25 stsp goto done;
4884 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4885 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4886 c4296144 2019-05-09 stsp goto done;
4887 a698f62e 2019-07-25 stsp }
4888 5c1e53bc 2019-07-28 stsp
4889 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4890 916f288c 2019-07-30 stsp worktree);
4891 5c1e53bc 2019-07-28 stsp if (error)
4892 5c1e53bc 2019-07-28 stsp goto done;
4893 c4296144 2019-05-09 stsp
4894 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4895 c9956ddf 2019-09-08 stsp if (error)
4896 c9956ddf 2019-09-08 stsp goto done;
4897 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4898 c9956ddf 2019-09-08 stsp gitconfig_path);
4899 c4296144 2019-05-09 stsp if (error != NULL)
4900 c4296144 2019-05-09 stsp goto done;
4901 c4296144 2019-05-09 stsp
4902 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4903 aba9c984 2019-09-08 stsp if (error)
4904 aba9c984 2019-09-08 stsp return error;
4905 aba9c984 2019-09-08 stsp
4906 314a6357 2019-05-13 stsp /*
4907 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4908 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4909 314a6357 2019-05-13 stsp */
4910 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4911 314a6357 2019-05-13 stsp error = get_editor(&editor);
4912 314a6357 2019-05-13 stsp else
4913 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4914 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4915 c4296144 2019-05-09 stsp if (error)
4916 c4296144 2019-05-09 stsp goto done;
4917 c4296144 2019-05-09 stsp
4918 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4919 087fb88c 2019-08-04 stsp if (error)
4920 087fb88c 2019-08-04 stsp goto done;
4921 087fb88c 2019-08-04 stsp
4922 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4923 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4924 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4925 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4926 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4927 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4928 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4929 916f288c 2019-07-30 stsp goto done;
4930 916f288c 2019-07-30 stsp }
4931 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4932 916f288c 2019-07-30 stsp }
4933 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4934 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4935 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4936 e0870e44 2019-05-13 stsp if (error) {
4937 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4938 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4939 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4940 c4296144 2019-05-09 stsp goto done;
4941 e0870e44 2019-05-13 stsp }
4942 c4296144 2019-05-09 stsp
4943 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4944 c4296144 2019-05-09 stsp if (error)
4945 c4296144 2019-05-09 stsp goto done;
4946 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4947 c4296144 2019-05-09 stsp done:
4948 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4949 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4950 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4951 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4952 7266f21f 2019-10-21 stsp error == NULL)
4953 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4954 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4955 c4296144 2019-05-09 stsp if (repo)
4956 c4296144 2019-05-09 stsp got_repo_close(repo);
4957 c4296144 2019-05-09 stsp if (worktree)
4958 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4959 c4296144 2019-05-09 stsp free(cwd);
4960 c4296144 2019-05-09 stsp free(id_str);
4961 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4962 e2ba3d07 2019-05-13 stsp free(editor);
4963 aba9c984 2019-09-08 stsp free(author);
4964 234035bc 2019-06-01 stsp return error;
4965 234035bc 2019-06-01 stsp }
4966 234035bc 2019-06-01 stsp
4967 234035bc 2019-06-01 stsp __dead static void
4968 234035bc 2019-06-01 stsp usage_cherrypick(void)
4969 234035bc 2019-06-01 stsp {
4970 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4971 234035bc 2019-06-01 stsp exit(1);
4972 234035bc 2019-06-01 stsp }
4973 234035bc 2019-06-01 stsp
4974 234035bc 2019-06-01 stsp static const struct got_error *
4975 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4976 234035bc 2019-06-01 stsp {
4977 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4978 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4979 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4980 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4981 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4982 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4983 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4984 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4985 234035bc 2019-06-01 stsp int ch, did_something = 0;
4986 234035bc 2019-06-01 stsp
4987 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4988 234035bc 2019-06-01 stsp switch (ch) {
4989 234035bc 2019-06-01 stsp default:
4990 234035bc 2019-06-01 stsp usage_cherrypick();
4991 234035bc 2019-06-01 stsp /* NOTREACHED */
4992 234035bc 2019-06-01 stsp }
4993 234035bc 2019-06-01 stsp }
4994 234035bc 2019-06-01 stsp
4995 234035bc 2019-06-01 stsp argc -= optind;
4996 234035bc 2019-06-01 stsp argv += optind;
4997 234035bc 2019-06-01 stsp
4998 43012d58 2019-07-14 stsp #ifndef PROFILE
4999 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5000 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5001 43012d58 2019-07-14 stsp err(1, "pledge");
5002 43012d58 2019-07-14 stsp #endif
5003 234035bc 2019-06-01 stsp if (argc != 1)
5004 234035bc 2019-06-01 stsp usage_cherrypick();
5005 234035bc 2019-06-01 stsp
5006 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5007 234035bc 2019-06-01 stsp if (cwd == NULL) {
5008 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5009 234035bc 2019-06-01 stsp goto done;
5010 234035bc 2019-06-01 stsp }
5011 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5012 234035bc 2019-06-01 stsp if (error)
5013 234035bc 2019-06-01 stsp goto done;
5014 234035bc 2019-06-01 stsp
5015 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5016 c9956ddf 2019-09-08 stsp NULL);
5017 234035bc 2019-06-01 stsp if (error != NULL)
5018 234035bc 2019-06-01 stsp goto done;
5019 234035bc 2019-06-01 stsp
5020 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5021 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5022 234035bc 2019-06-01 stsp if (error)
5023 234035bc 2019-06-01 stsp goto done;
5024 234035bc 2019-06-01 stsp
5025 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5026 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5027 234035bc 2019-06-01 stsp if (error != NULL) {
5028 234035bc 2019-06-01 stsp struct got_reference *ref;
5029 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5030 234035bc 2019-06-01 stsp goto done;
5031 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5032 234035bc 2019-06-01 stsp if (error != NULL)
5033 234035bc 2019-06-01 stsp goto done;
5034 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5035 234035bc 2019-06-01 stsp got_ref_close(ref);
5036 234035bc 2019-06-01 stsp if (error != NULL)
5037 234035bc 2019-06-01 stsp goto done;
5038 234035bc 2019-06-01 stsp }
5039 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5040 234035bc 2019-06-01 stsp if (error)
5041 234035bc 2019-06-01 stsp goto done;
5042 234035bc 2019-06-01 stsp
5043 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5044 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5045 234035bc 2019-06-01 stsp if (error != NULL)
5046 234035bc 2019-06-01 stsp goto done;
5047 234035bc 2019-06-01 stsp
5048 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5049 234035bc 2019-06-01 stsp if (error) {
5050 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5051 234035bc 2019-06-01 stsp goto done;
5052 234035bc 2019-06-01 stsp error = NULL;
5053 234035bc 2019-06-01 stsp } else {
5054 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5055 234035bc 2019-06-01 stsp goto done;
5056 234035bc 2019-06-01 stsp }
5057 234035bc 2019-06-01 stsp
5058 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5059 234035bc 2019-06-01 stsp if (error)
5060 234035bc 2019-06-01 stsp goto done;
5061 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5062 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5063 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5064 03415a1a 2019-06-02 stsp NULL);
5065 234035bc 2019-06-01 stsp if (error != NULL)
5066 234035bc 2019-06-01 stsp goto done;
5067 234035bc 2019-06-01 stsp
5068 234035bc 2019-06-01 stsp if (did_something)
5069 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5070 234035bc 2019-06-01 stsp done:
5071 234035bc 2019-06-01 stsp if (commit)
5072 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5073 234035bc 2019-06-01 stsp free(commit_id_str);
5074 234035bc 2019-06-01 stsp if (head_ref)
5075 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5076 234035bc 2019-06-01 stsp if (worktree)
5077 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5078 234035bc 2019-06-01 stsp if (repo)
5079 234035bc 2019-06-01 stsp got_repo_close(repo);
5080 c4296144 2019-05-09 stsp return error;
5081 c4296144 2019-05-09 stsp }
5082 5ef14e63 2019-06-02 stsp
5083 5ef14e63 2019-06-02 stsp __dead static void
5084 5ef14e63 2019-06-02 stsp usage_backout(void)
5085 5ef14e63 2019-06-02 stsp {
5086 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5087 5ef14e63 2019-06-02 stsp exit(1);
5088 5ef14e63 2019-06-02 stsp }
5089 5ef14e63 2019-06-02 stsp
5090 5ef14e63 2019-06-02 stsp static const struct got_error *
5091 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5092 5ef14e63 2019-06-02 stsp {
5093 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5094 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5095 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5096 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5097 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5098 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5099 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5100 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5101 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5102 5ef14e63 2019-06-02 stsp
5103 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5104 5ef14e63 2019-06-02 stsp switch (ch) {
5105 5ef14e63 2019-06-02 stsp default:
5106 5ef14e63 2019-06-02 stsp usage_backout();
5107 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5108 5ef14e63 2019-06-02 stsp }
5109 5ef14e63 2019-06-02 stsp }
5110 5ef14e63 2019-06-02 stsp
5111 5ef14e63 2019-06-02 stsp argc -= optind;
5112 5ef14e63 2019-06-02 stsp argv += optind;
5113 5ef14e63 2019-06-02 stsp
5114 43012d58 2019-07-14 stsp #ifndef PROFILE
5115 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5116 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5117 43012d58 2019-07-14 stsp err(1, "pledge");
5118 43012d58 2019-07-14 stsp #endif
5119 5ef14e63 2019-06-02 stsp if (argc != 1)
5120 5ef14e63 2019-06-02 stsp usage_backout();
5121 5ef14e63 2019-06-02 stsp
5122 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5123 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5124 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5125 5ef14e63 2019-06-02 stsp goto done;
5126 5ef14e63 2019-06-02 stsp }
5127 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5128 5ef14e63 2019-06-02 stsp if (error)
5129 5ef14e63 2019-06-02 stsp goto done;
5130 5ef14e63 2019-06-02 stsp
5131 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5132 c9956ddf 2019-09-08 stsp NULL);
5133 5ef14e63 2019-06-02 stsp if (error != NULL)
5134 5ef14e63 2019-06-02 stsp goto done;
5135 5ef14e63 2019-06-02 stsp
5136 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5137 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5138 5ef14e63 2019-06-02 stsp if (error)
5139 5ef14e63 2019-06-02 stsp goto done;
5140 5ef14e63 2019-06-02 stsp
5141 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5142 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5143 5ef14e63 2019-06-02 stsp if (error != NULL) {
5144 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5145 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5146 5ef14e63 2019-06-02 stsp goto done;
5147 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5148 5ef14e63 2019-06-02 stsp if (error != NULL)
5149 5ef14e63 2019-06-02 stsp goto done;
5150 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5151 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5152 5ef14e63 2019-06-02 stsp if (error != NULL)
5153 5ef14e63 2019-06-02 stsp goto done;
5154 5ef14e63 2019-06-02 stsp }
5155 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5156 5ef14e63 2019-06-02 stsp if (error)
5157 5ef14e63 2019-06-02 stsp goto done;
5158 5ef14e63 2019-06-02 stsp
5159 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5160 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5161 5ef14e63 2019-06-02 stsp if (error != NULL)
5162 5ef14e63 2019-06-02 stsp goto done;
5163 5ef14e63 2019-06-02 stsp
5164 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5165 5ef14e63 2019-06-02 stsp if (error)
5166 5ef14e63 2019-06-02 stsp goto done;
5167 5ef14e63 2019-06-02 stsp
5168 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5169 5ef14e63 2019-06-02 stsp if (error)
5170 5ef14e63 2019-06-02 stsp goto done;
5171 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5172 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5173 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5174 5ef14e63 2019-06-02 stsp goto done;
5175 5ef14e63 2019-06-02 stsp }
5176 5ef14e63 2019-06-02 stsp
5177 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5178 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5179 5ef14e63 2019-06-02 stsp if (error != NULL)
5180 5ef14e63 2019-06-02 stsp goto done;
5181 5ef14e63 2019-06-02 stsp
5182 5ef14e63 2019-06-02 stsp if (did_something)
5183 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5184 5ef14e63 2019-06-02 stsp done:
5185 5ef14e63 2019-06-02 stsp if (commit)
5186 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5187 5ef14e63 2019-06-02 stsp free(commit_id_str);
5188 5ef14e63 2019-06-02 stsp if (head_ref)
5189 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5190 818c7501 2019-07-11 stsp if (worktree)
5191 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5192 818c7501 2019-07-11 stsp if (repo)
5193 818c7501 2019-07-11 stsp got_repo_close(repo);
5194 818c7501 2019-07-11 stsp return error;
5195 818c7501 2019-07-11 stsp }
5196 818c7501 2019-07-11 stsp
5197 818c7501 2019-07-11 stsp __dead static void
5198 818c7501 2019-07-11 stsp usage_rebase(void)
5199 818c7501 2019-07-11 stsp {
5200 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5201 af54c8f8 2019-07-11 stsp getprogname());
5202 818c7501 2019-07-11 stsp exit(1);
5203 0ebf8283 2019-07-24 stsp }
5204 0ebf8283 2019-07-24 stsp
5205 0ebf8283 2019-07-24 stsp void
5206 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5207 0ebf8283 2019-07-24 stsp {
5208 0ebf8283 2019-07-24 stsp char *nl;
5209 0ebf8283 2019-07-24 stsp size_t len;
5210 0ebf8283 2019-07-24 stsp
5211 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5212 0ebf8283 2019-07-24 stsp if (len > limit)
5213 0ebf8283 2019-07-24 stsp len = limit;
5214 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5215 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5216 0ebf8283 2019-07-24 stsp if (nl)
5217 0ebf8283 2019-07-24 stsp *nl = '\0';
5218 0ebf8283 2019-07-24 stsp }
5219 0ebf8283 2019-07-24 stsp
5220 0ebf8283 2019-07-24 stsp static const struct got_error *
5221 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5222 0ebf8283 2019-07-24 stsp {
5223 5943eee2 2019-08-13 stsp const struct got_error *err;
5224 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5225 5943eee2 2019-08-13 stsp const char *s;
5226 0ebf8283 2019-07-24 stsp
5227 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5228 5943eee2 2019-08-13 stsp if (err)
5229 5943eee2 2019-08-13 stsp return err;
5230 0ebf8283 2019-07-24 stsp
5231 5943eee2 2019-08-13 stsp s = logmsg0;
5232 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5233 5943eee2 2019-08-13 stsp s++;
5234 5943eee2 2019-08-13 stsp
5235 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5236 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5237 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5238 5943eee2 2019-08-13 stsp goto done;
5239 5943eee2 2019-08-13 stsp }
5240 0ebf8283 2019-07-24 stsp
5241 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5242 5943eee2 2019-08-13 stsp done:
5243 5943eee2 2019-08-13 stsp free(logmsg0);
5244 5943eee2 2019-08-13 stsp return err;
5245 818c7501 2019-07-11 stsp }
5246 818c7501 2019-07-11 stsp
5247 818c7501 2019-07-11 stsp static const struct got_error *
5248 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5249 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5250 818c7501 2019-07-11 stsp {
5251 818c7501 2019-07-11 stsp const struct got_error *err;
5252 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5253 818c7501 2019-07-11 stsp
5254 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5255 818c7501 2019-07-11 stsp if (err)
5256 818c7501 2019-07-11 stsp goto done;
5257 818c7501 2019-07-11 stsp
5258 ff0d2220 2019-07-11 stsp if (new_id) {
5259 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5260 ff0d2220 2019-07-11 stsp if (err)
5261 ff0d2220 2019-07-11 stsp goto done;
5262 ff0d2220 2019-07-11 stsp }
5263 818c7501 2019-07-11 stsp
5264 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5265 ff0d2220 2019-07-11 stsp if (new_id_str)
5266 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5267 0ebf8283 2019-07-24 stsp
5268 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5269 0ebf8283 2019-07-24 stsp if (err)
5270 0ebf8283 2019-07-24 stsp goto done;
5271 0ebf8283 2019-07-24 stsp
5272 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5273 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5274 818c7501 2019-07-11 stsp done:
5275 818c7501 2019-07-11 stsp free(old_id_str);
5276 818c7501 2019-07-11 stsp free(new_id_str);
5277 818c7501 2019-07-11 stsp return err;
5278 818c7501 2019-07-11 stsp }
5279 818c7501 2019-07-11 stsp
5280 1ee397ad 2019-07-12 stsp static const struct got_error *
5281 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5282 818c7501 2019-07-11 stsp {
5283 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5284 818c7501 2019-07-11 stsp
5285 818c7501 2019-07-11 stsp while (path[0] == '/')
5286 818c7501 2019-07-11 stsp path++;
5287 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5288 818c7501 2019-07-11 stsp
5289 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5290 1ee397ad 2019-07-12 stsp return NULL;
5291 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5292 818c7501 2019-07-11 stsp *rebase_status = status;
5293 1ee397ad 2019-07-12 stsp return NULL;
5294 818c7501 2019-07-11 stsp }
5295 818c7501 2019-07-11 stsp
5296 818c7501 2019-07-11 stsp static const struct got_error *
5297 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5298 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5299 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5300 818c7501 2019-07-11 stsp {
5301 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5302 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5303 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5304 818c7501 2019-07-11 stsp }
5305 818c7501 2019-07-11 stsp
5306 818c7501 2019-07-11 stsp static const struct got_error *
5307 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5308 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5309 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5310 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5311 ff0d2220 2019-07-11 stsp {
5312 ff0d2220 2019-07-11 stsp const struct got_error *error;
5313 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5314 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5315 ff0d2220 2019-07-11 stsp
5316 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5317 ff0d2220 2019-07-11 stsp if (error)
5318 ff0d2220 2019-07-11 stsp return error;
5319 ff0d2220 2019-07-11 stsp
5320 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5321 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5322 ff0d2220 2019-07-11 stsp if (error) {
5323 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5324 ff0d2220 2019-07-11 stsp goto done;
5325 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5326 ff0d2220 2019-07-11 stsp } else {
5327 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5328 ff0d2220 2019-07-11 stsp free(new_commit_id);
5329 ff0d2220 2019-07-11 stsp }
5330 ff0d2220 2019-07-11 stsp done:
5331 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5332 ff0d2220 2019-07-11 stsp return error;
5333 64c6d990 2019-07-11 stsp }
5334 64c6d990 2019-07-11 stsp
5335 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5336 64c6d990 2019-07-11 stsp const char *path_prefix;
5337 64c6d990 2019-07-11 stsp size_t len;
5338 8ca9bd68 2019-07-25 stsp int errcode;
5339 64c6d990 2019-07-11 stsp };
5340 64c6d990 2019-07-11 stsp
5341 64c6d990 2019-07-11 stsp static const struct got_error *
5342 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5343 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5344 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5345 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5346 64c6d990 2019-07-11 stsp {
5347 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5348 64c6d990 2019-07-11 stsp
5349 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5350 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5351 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5352 64c6d990 2019-07-11 stsp
5353 64c6d990 2019-07-11 stsp return NULL;
5354 64c6d990 2019-07-11 stsp }
5355 64c6d990 2019-07-11 stsp
5356 64c6d990 2019-07-11 stsp static const struct got_error *
5357 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5358 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5359 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5360 64c6d990 2019-07-11 stsp {
5361 64c6d990 2019-07-11 stsp const struct got_error *err;
5362 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5363 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5364 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5365 64c6d990 2019-07-11 stsp
5366 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5367 64c6d990 2019-07-11 stsp return NULL;
5368 64c6d990 2019-07-11 stsp
5369 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5370 64c6d990 2019-07-11 stsp if (err)
5371 64c6d990 2019-07-11 stsp goto done;
5372 64c6d990 2019-07-11 stsp
5373 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5374 64c6d990 2019-07-11 stsp if (err)
5375 64c6d990 2019-07-11 stsp goto done;
5376 64c6d990 2019-07-11 stsp
5377 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5378 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5379 64c6d990 2019-07-11 stsp if (err)
5380 64c6d990 2019-07-11 stsp goto done;
5381 64c6d990 2019-07-11 stsp
5382 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5383 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5384 64c6d990 2019-07-11 stsp if (err)
5385 64c6d990 2019-07-11 stsp goto done;
5386 64c6d990 2019-07-11 stsp
5387 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5388 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5389 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5390 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5391 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5392 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5393 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5394 64c6d990 2019-07-11 stsp done:
5395 64c6d990 2019-07-11 stsp if (tree1)
5396 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5397 64c6d990 2019-07-11 stsp if (tree2)
5398 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5399 64c6d990 2019-07-11 stsp if (commit)
5400 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5401 64c6d990 2019-07-11 stsp if (parent_commit)
5402 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5403 64c6d990 2019-07-11 stsp return err;
5404 ff0d2220 2019-07-11 stsp }
5405 ff0d2220 2019-07-11 stsp
5406 ff0d2220 2019-07-11 stsp static const struct got_error *
5407 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5408 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5409 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5410 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5411 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5412 af61c510 2019-07-19 stsp {
5413 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5414 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5415 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5416 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5417 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5418 af61c510 2019-07-19 stsp
5419 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5420 af61c510 2019-07-19 stsp if (err)
5421 af61c510 2019-07-19 stsp return err;
5422 af61c510 2019-07-19 stsp
5423 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5424 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5425 af61c510 2019-07-19 stsp if (err)
5426 af61c510 2019-07-19 stsp goto done;
5427 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5428 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5429 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5430 af61c510 2019-07-19 stsp if (err) {
5431 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5432 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5433 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5434 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5435 af61c510 2019-07-19 stsp "been reached?!?");
5436 ee780d5c 2020-01-04 stsp }
5437 ee780d5c 2020-01-04 stsp goto done;
5438 af61c510 2019-07-19 stsp } else {
5439 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5440 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5441 af61c510 2019-07-19 stsp if (err)
5442 af61c510 2019-07-19 stsp goto done;
5443 af61c510 2019-07-19 stsp
5444 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5445 af61c510 2019-07-19 stsp if (err)
5446 af61c510 2019-07-19 stsp goto done;
5447 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5448 af61c510 2019-07-19 stsp commit_id = parent_id;
5449 af61c510 2019-07-19 stsp }
5450 af61c510 2019-07-19 stsp }
5451 af61c510 2019-07-19 stsp done:
5452 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5453 af61c510 2019-07-19 stsp return err;
5454 af61c510 2019-07-19 stsp }
5455 af61c510 2019-07-19 stsp
5456 af61c510 2019-07-19 stsp static const struct got_error *
5457 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5458 818c7501 2019-07-11 stsp {
5459 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5460 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5461 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5462 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5463 818c7501 2019-07-11 stsp char *cwd = NULL;
5464 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5465 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5466 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5467 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5468 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5469 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5470 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5471 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5472 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5473 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5474 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5475 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5476 818c7501 2019-07-11 stsp
5477 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5478 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5479 818c7501 2019-07-11 stsp
5480 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5481 818c7501 2019-07-11 stsp switch (ch) {
5482 818c7501 2019-07-11 stsp case 'a':
5483 818c7501 2019-07-11 stsp abort_rebase = 1;
5484 818c7501 2019-07-11 stsp break;
5485 818c7501 2019-07-11 stsp case 'c':
5486 818c7501 2019-07-11 stsp continue_rebase = 1;
5487 818c7501 2019-07-11 stsp break;
5488 818c7501 2019-07-11 stsp default:
5489 818c7501 2019-07-11 stsp usage_rebase();
5490 818c7501 2019-07-11 stsp /* NOTREACHED */
5491 818c7501 2019-07-11 stsp }
5492 818c7501 2019-07-11 stsp }
5493 818c7501 2019-07-11 stsp
5494 818c7501 2019-07-11 stsp argc -= optind;
5495 818c7501 2019-07-11 stsp argv += optind;
5496 818c7501 2019-07-11 stsp
5497 43012d58 2019-07-14 stsp #ifndef PROFILE
5498 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5499 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5500 43012d58 2019-07-14 stsp err(1, "pledge");
5501 43012d58 2019-07-14 stsp #endif
5502 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5503 818c7501 2019-07-11 stsp usage_rebase();
5504 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5505 818c7501 2019-07-11 stsp if (argc != 0)
5506 818c7501 2019-07-11 stsp usage_rebase();
5507 818c7501 2019-07-11 stsp } else if (argc != 1)
5508 818c7501 2019-07-11 stsp usage_rebase();
5509 818c7501 2019-07-11 stsp
5510 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5511 818c7501 2019-07-11 stsp if (cwd == NULL) {
5512 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5513 818c7501 2019-07-11 stsp goto done;
5514 818c7501 2019-07-11 stsp }
5515 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5516 818c7501 2019-07-11 stsp if (error)
5517 818c7501 2019-07-11 stsp goto done;
5518 818c7501 2019-07-11 stsp
5519 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5520 c9956ddf 2019-09-08 stsp NULL);
5521 818c7501 2019-07-11 stsp if (error != NULL)
5522 818c7501 2019-07-11 stsp goto done;
5523 818c7501 2019-07-11 stsp
5524 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5525 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5526 818c7501 2019-07-11 stsp if (error)
5527 818c7501 2019-07-11 stsp goto done;
5528 818c7501 2019-07-11 stsp
5529 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5530 818c7501 2019-07-11 stsp if (error)
5531 818c7501 2019-07-11 stsp goto done;
5532 818c7501 2019-07-11 stsp
5533 f6794adc 2019-07-23 stsp if (abort_rebase) {
5534 818c7501 2019-07-11 stsp int did_something;
5535 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5536 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5537 f6794adc 2019-07-23 stsp goto done;
5538 f6794adc 2019-07-23 stsp }
5539 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5540 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5541 3e3a69f1 2019-07-25 stsp worktree, repo);
5542 818c7501 2019-07-11 stsp if (error)
5543 818c7501 2019-07-11 stsp goto done;
5544 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5545 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5546 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5547 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5548 818c7501 2019-07-11 stsp if (error)
5549 818c7501 2019-07-11 stsp goto done;
5550 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5551 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5552 818c7501 2019-07-11 stsp }
5553 818c7501 2019-07-11 stsp
5554 818c7501 2019-07-11 stsp if (continue_rebase) {
5555 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5556 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5557 f6794adc 2019-07-23 stsp goto done;
5558 f6794adc 2019-07-23 stsp }
5559 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5560 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5561 3e3a69f1 2019-07-25 stsp worktree, repo);
5562 818c7501 2019-07-11 stsp if (error)
5563 818c7501 2019-07-11 stsp goto done;
5564 818c7501 2019-07-11 stsp
5565 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5566 01757395 2019-07-12 stsp resume_commit_id, repo);
5567 818c7501 2019-07-11 stsp if (error)
5568 818c7501 2019-07-11 stsp goto done;
5569 818c7501 2019-07-11 stsp
5570 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5571 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5572 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5573 818c7501 2019-07-11 stsp goto done;
5574 818c7501 2019-07-11 stsp }
5575 818c7501 2019-07-11 stsp } else {
5576 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5577 818c7501 2019-07-11 stsp if (error != NULL)
5578 818c7501 2019-07-11 stsp goto done;
5579 ff0d2220 2019-07-11 stsp }
5580 818c7501 2019-07-11 stsp
5581 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5582 ff0d2220 2019-07-11 stsp if (error)
5583 ff0d2220 2019-07-11 stsp goto done;
5584 ff0d2220 2019-07-11 stsp
5585 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5586 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5587 a51a74b3 2019-07-27 stsp
5588 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5589 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5590 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5591 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5592 818c7501 2019-07-11 stsp if (error)
5593 818c7501 2019-07-11 stsp goto done;
5594 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5595 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5596 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5597 818c7501 2019-07-11 stsp "with work tree's branch");
5598 818c7501 2019-07-11 stsp goto done;
5599 818c7501 2019-07-11 stsp }
5600 818c7501 2019-07-11 stsp
5601 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5602 a51a74b3 2019-07-27 stsp if (error) {
5603 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5604 a51a74b3 2019-07-27 stsp goto done;
5605 a51a74b3 2019-07-27 stsp error = NULL;
5606 a51a74b3 2019-07-27 stsp } else {
5607 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5608 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5609 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5610 a51a74b3 2019-07-27 stsp goto done;
5611 a51a74b3 2019-07-27 stsp }
5612 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5613 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5614 818c7501 2019-07-11 stsp if (error)
5615 818c7501 2019-07-11 stsp goto done;
5616 818c7501 2019-07-11 stsp }
5617 818c7501 2019-07-11 stsp
5618 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5619 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5620 818c7501 2019-07-11 stsp if (error)
5621 818c7501 2019-07-11 stsp goto done;
5622 818c7501 2019-07-11 stsp
5623 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5624 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5625 fc66b545 2019-08-12 stsp if (pid == NULL) {
5626 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5627 fc66b545 2019-08-12 stsp int did_something;
5628 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5629 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5630 fc66b545 2019-08-12 stsp &did_something);
5631 fc66b545 2019-08-12 stsp if (error)
5632 fc66b545 2019-08-12 stsp goto done;
5633 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5634 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5635 fc66b545 2019-08-12 stsp }
5636 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5637 fc66b545 2019-08-12 stsp goto done;
5638 fc66b545 2019-08-12 stsp }
5639 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5640 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5641 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5642 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5643 818c7501 2019-07-11 stsp commit = NULL;
5644 818c7501 2019-07-11 stsp if (error)
5645 818c7501 2019-07-11 stsp goto done;
5646 64c6d990 2019-07-11 stsp
5647 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5648 38b0338b 2019-11-29 stsp if (continue_rebase) {
5649 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5650 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5651 38b0338b 2019-11-29 stsp goto done;
5652 38b0338b 2019-11-29 stsp } else {
5653 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5654 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5655 38b0338b 2019-11-29 stsp char *id_str;
5656 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5657 38b0338b 2019-11-29 stsp new_base_branch);
5658 38b0338b 2019-11-29 stsp if (error)
5659 38b0338b 2019-11-29 stsp goto done;
5660 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5661 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5662 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5663 38b0338b 2019-11-29 stsp free(id_str);
5664 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5665 38b0338b 2019-11-29 stsp new_head_commit_id);
5666 38b0338b 2019-11-29 stsp if (error)
5667 38b0338b 2019-11-29 stsp goto done;
5668 38b0338b 2019-11-29 stsp }
5669 818c7501 2019-07-11 stsp }
5670 818c7501 2019-07-11 stsp
5671 818c7501 2019-07-11 stsp pid = NULL;
5672 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5673 818c7501 2019-07-11 stsp commit_id = qid->id;
5674 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5675 818c7501 2019-07-11 stsp pid = qid;
5676 818c7501 2019-07-11 stsp
5677 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5678 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5679 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5680 818c7501 2019-07-11 stsp if (error)
5681 818c7501 2019-07-11 stsp goto done;
5682 818c7501 2019-07-11 stsp
5683 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5684 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5685 818c7501 2019-07-11 stsp break;
5686 01757395 2019-07-12 stsp }
5687 818c7501 2019-07-11 stsp
5688 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5689 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5690 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5691 818c7501 2019-07-11 stsp if (error)
5692 818c7501 2019-07-11 stsp goto done;
5693 818c7501 2019-07-11 stsp }
5694 818c7501 2019-07-11 stsp
5695 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5696 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5697 818c7501 2019-07-11 stsp if (error)
5698 818c7501 2019-07-11 stsp goto done;
5699 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5700 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5701 818c7501 2019-07-11 stsp } else
5702 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5703 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5704 818c7501 2019-07-11 stsp done:
5705 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5706 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5707 818c7501 2019-07-11 stsp free(resume_commit_id);
5708 818c7501 2019-07-11 stsp free(yca_id);
5709 818c7501 2019-07-11 stsp if (commit)
5710 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5711 818c7501 2019-07-11 stsp if (branch)
5712 818c7501 2019-07-11 stsp got_ref_close(branch);
5713 818c7501 2019-07-11 stsp if (new_base_branch)
5714 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5715 818c7501 2019-07-11 stsp if (tmp_branch)
5716 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5717 5ef14e63 2019-06-02 stsp if (worktree)
5718 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5719 5ef14e63 2019-06-02 stsp if (repo)
5720 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5721 5ef14e63 2019-06-02 stsp return error;
5722 0ebf8283 2019-07-24 stsp }
5723 0ebf8283 2019-07-24 stsp
5724 0ebf8283 2019-07-24 stsp __dead static void
5725 0ebf8283 2019-07-24 stsp usage_histedit(void)
5726 0ebf8283 2019-07-24 stsp {
5727 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5728 0ebf8283 2019-07-24 stsp getprogname());
5729 0ebf8283 2019-07-24 stsp exit(1);
5730 0ebf8283 2019-07-24 stsp }
5731 0ebf8283 2019-07-24 stsp
5732 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5733 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5734 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5735 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5736 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5737 0ebf8283 2019-07-24 stsp
5738 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5739 0ebf8283 2019-07-24 stsp unsigned char code;
5740 0ebf8283 2019-07-24 stsp const char *name;
5741 0ebf8283 2019-07-24 stsp const char *desc;
5742 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5743 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5744 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5745 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5746 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5747 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5748 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5749 0ebf8283 2019-07-24 stsp };
5750 0ebf8283 2019-07-24 stsp
5751 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5752 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5753 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5754 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5755 0ebf8283 2019-07-24 stsp char *logmsg;
5756 0ebf8283 2019-07-24 stsp };
5757 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5758 0ebf8283 2019-07-24 stsp
5759 0ebf8283 2019-07-24 stsp static const struct got_error *
5760 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5761 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5762 0ebf8283 2019-07-24 stsp {
5763 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5764 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5765 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5766 8138f3e1 2019-08-11 stsp int n;
5767 0ebf8283 2019-07-24 stsp
5768 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5769 0ebf8283 2019-07-24 stsp if (err)
5770 0ebf8283 2019-07-24 stsp goto done;
5771 0ebf8283 2019-07-24 stsp
5772 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5773 0ebf8283 2019-07-24 stsp if (err)
5774 0ebf8283 2019-07-24 stsp goto done;
5775 0ebf8283 2019-07-24 stsp
5776 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5777 0ebf8283 2019-07-24 stsp if (err)
5778 0ebf8283 2019-07-24 stsp goto done;
5779 0ebf8283 2019-07-24 stsp
5780 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5781 0ebf8283 2019-07-24 stsp if (n < 0)
5782 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5783 0ebf8283 2019-07-24 stsp done:
5784 0ebf8283 2019-07-24 stsp if (commit)
5785 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5786 0ebf8283 2019-07-24 stsp free(id_str);
5787 0ebf8283 2019-07-24 stsp free(logmsg);
5788 0ebf8283 2019-07-24 stsp return err;
5789 0ebf8283 2019-07-24 stsp }
5790 0ebf8283 2019-07-24 stsp
5791 0ebf8283 2019-07-24 stsp static const struct got_error *
5792 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5793 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5794 0ebf8283 2019-07-24 stsp {
5795 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5796 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5797 0ebf8283 2019-07-24 stsp
5798 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5799 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5800 0ebf8283 2019-07-24 stsp
5801 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5802 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5803 0ebf8283 2019-07-24 stsp f, repo);
5804 0ebf8283 2019-07-24 stsp if (err)
5805 0ebf8283 2019-07-24 stsp break;
5806 0ebf8283 2019-07-24 stsp }
5807 0ebf8283 2019-07-24 stsp
5808 0ebf8283 2019-07-24 stsp return err;
5809 0ebf8283 2019-07-24 stsp }
5810 0ebf8283 2019-07-24 stsp
5811 0ebf8283 2019-07-24 stsp static const struct got_error *
5812 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5813 0ebf8283 2019-07-24 stsp {
5814 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5815 0ebf8283 2019-07-24 stsp int n, i;
5816 0ebf8283 2019-07-24 stsp
5817 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5818 0ebf8283 2019-07-24 stsp if (n < 0)
5819 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5820 0ebf8283 2019-07-24 stsp
5821 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5822 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5823 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5824 0ebf8283 2019-07-24 stsp cmd->desc);
5825 0ebf8283 2019-07-24 stsp if (n < 0) {
5826 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5827 0ebf8283 2019-07-24 stsp break;
5828 0ebf8283 2019-07-24 stsp }
5829 0ebf8283 2019-07-24 stsp }
5830 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5831 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5832 0ebf8283 2019-07-24 stsp if (n < 0)
5833 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5834 0ebf8283 2019-07-24 stsp return err;
5835 0ebf8283 2019-07-24 stsp }
5836 0ebf8283 2019-07-24 stsp
5837 0ebf8283 2019-07-24 stsp static const struct got_error *
5838 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5839 0ebf8283 2019-07-24 stsp {
5840 0ebf8283 2019-07-24 stsp static char msg[42];
5841 0ebf8283 2019-07-24 stsp int ret;
5842 0ebf8283 2019-07-24 stsp
5843 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5844 0ebf8283 2019-07-24 stsp lineno);
5845 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5846 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5847 0ebf8283 2019-07-24 stsp
5848 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5849 0ebf8283 2019-07-24 stsp }
5850 0ebf8283 2019-07-24 stsp
5851 0ebf8283 2019-07-24 stsp static const struct got_error *
5852 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5853 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5854 0ebf8283 2019-07-24 stsp {
5855 0ebf8283 2019-07-24 stsp const struct got_error *err;
5856 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5857 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5858 0ebf8283 2019-07-24 stsp
5859 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5860 0ebf8283 2019-07-24 stsp if (err)
5861 0ebf8283 2019-07-24 stsp return err;
5862 0ebf8283 2019-07-24 stsp
5863 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5864 0ebf8283 2019-07-24 stsp if (err)
5865 0ebf8283 2019-07-24 stsp goto done;
5866 0ebf8283 2019-07-24 stsp
5867 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5868 5943eee2 2019-08-13 stsp if (err)
5869 5943eee2 2019-08-13 stsp goto done;
5870 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5871 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5872 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5873 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5874 0ebf8283 2019-07-24 stsp goto done;
5875 0ebf8283 2019-07-24 stsp }
5876 0ebf8283 2019-07-24 stsp done:
5877 0ebf8283 2019-07-24 stsp if (folded_commit)
5878 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5879 0ebf8283 2019-07-24 stsp free(id_str);
5880 5943eee2 2019-08-13 stsp free(folded_logmsg);
5881 0ebf8283 2019-07-24 stsp return err;
5882 0ebf8283 2019-07-24 stsp }
5883 0ebf8283 2019-07-24 stsp
5884 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5885 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5886 0ebf8283 2019-07-24 stsp {
5887 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5888 0ebf8283 2019-07-24 stsp
5889 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5890 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5891 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5892 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5893 3f9de99f 2019-07-24 stsp folded = prev;
5894 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5895 0ebf8283 2019-07-24 stsp }
5896 0ebf8283 2019-07-24 stsp
5897 0ebf8283 2019-07-24 stsp return folded;
5898 0ebf8283 2019-07-24 stsp }
5899 0ebf8283 2019-07-24 stsp
5900 0ebf8283 2019-07-24 stsp static const struct got_error *
5901 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5902 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5903 0ebf8283 2019-07-24 stsp {
5904 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5905 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5906 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5907 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5908 0ebf8283 2019-07-24 stsp int fd;
5909 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5910 0ebf8283 2019-07-24 stsp
5911 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5912 0ebf8283 2019-07-24 stsp if (err)
5913 0ebf8283 2019-07-24 stsp return err;
5914 0ebf8283 2019-07-24 stsp
5915 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5916 0ebf8283 2019-07-24 stsp if (folded) {
5917 0ebf8283 2019-07-24 stsp while (folded != hle) {
5918 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5919 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5920 3f9de99f 2019-07-24 stsp continue;
5921 3f9de99f 2019-07-24 stsp }
5922 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5923 0ebf8283 2019-07-24 stsp logmsg, repo);
5924 0ebf8283 2019-07-24 stsp if (err)
5925 0ebf8283 2019-07-24 stsp goto done;
5926 0ebf8283 2019-07-24 stsp free(logmsg);
5927 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5928 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5929 0ebf8283 2019-07-24 stsp }
5930 0ebf8283 2019-07-24 stsp }
5931 0ebf8283 2019-07-24 stsp
5932 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5933 0ebf8283 2019-07-24 stsp if (err)
5934 0ebf8283 2019-07-24 stsp goto done;
5935 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5936 5943eee2 2019-08-13 stsp if (err)
5937 5943eee2 2019-08-13 stsp goto done;
5938 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5939 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5940 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5941 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5942 0ebf8283 2019-07-24 stsp goto done;
5943 0ebf8283 2019-07-24 stsp }
5944 0ebf8283 2019-07-24 stsp free(logmsg);
5945 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5946 0ebf8283 2019-07-24 stsp
5947 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5948 0ebf8283 2019-07-24 stsp if (err)
5949 0ebf8283 2019-07-24 stsp goto done;
5950 0ebf8283 2019-07-24 stsp
5951 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5952 0ebf8283 2019-07-24 stsp if (err)
5953 0ebf8283 2019-07-24 stsp goto done;
5954 0ebf8283 2019-07-24 stsp
5955 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5956 0ebf8283 2019-07-24 stsp close(fd);
5957 0ebf8283 2019-07-24 stsp
5958 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5959 0ebf8283 2019-07-24 stsp if (err)
5960 0ebf8283 2019-07-24 stsp goto done;
5961 0ebf8283 2019-07-24 stsp
5962 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5963 0ebf8283 2019-07-24 stsp if (err) {
5964 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5965 0ebf8283 2019-07-24 stsp goto done;
5966 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5967 0ebf8283 2019-07-24 stsp }
5968 0ebf8283 2019-07-24 stsp done:
5969 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5970 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5971 0ebf8283 2019-07-24 stsp free(logmsg_path);
5972 0ebf8283 2019-07-24 stsp free(logmsg);
5973 5943eee2 2019-08-13 stsp free(orig_logmsg);
5974 0ebf8283 2019-07-24 stsp free(editor);
5975 0ebf8283 2019-07-24 stsp if (commit)
5976 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5977 0ebf8283 2019-07-24 stsp return err;
5978 5ef14e63 2019-06-02 stsp }
5979 0ebf8283 2019-07-24 stsp
5980 0ebf8283 2019-07-24 stsp static const struct got_error *
5981 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5982 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5983 0ebf8283 2019-07-24 stsp {
5984 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5985 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5986 0ebf8283 2019-07-24 stsp size_t size;
5987 0ebf8283 2019-07-24 stsp ssize_t len;
5988 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5989 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5990 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5991 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5992 0ebf8283 2019-07-24 stsp
5993 0ebf8283 2019-07-24 stsp for (;;) {
5994 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5995 0ebf8283 2019-07-24 stsp if (len == -1) {
5996 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5997 0ebf8283 2019-07-24 stsp if (feof(f))
5998 0ebf8283 2019-07-24 stsp break;
5999 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6000 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6001 0ebf8283 2019-07-24 stsp break;
6002 0ebf8283 2019-07-24 stsp }
6003 0ebf8283 2019-07-24 stsp lineno++;
6004 0ebf8283 2019-07-24 stsp p = line;
6005 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6006 0ebf8283 2019-07-24 stsp p++;
6007 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6008 0ebf8283 2019-07-24 stsp free(line);
6009 0ebf8283 2019-07-24 stsp line = NULL;
6010 0ebf8283 2019-07-24 stsp continue;
6011 0ebf8283 2019-07-24 stsp }
6012 0ebf8283 2019-07-24 stsp cmd = NULL;
6013 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6014 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6015 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6016 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6017 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6018 0ebf8283 2019-07-24 stsp break;
6019 0ebf8283 2019-07-24 stsp }
6020 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6021 0ebf8283 2019-07-24 stsp p++;
6022 0ebf8283 2019-07-24 stsp break;
6023 0ebf8283 2019-07-24 stsp }
6024 0ebf8283 2019-07-24 stsp }
6025 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6026 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6027 0ebf8283 2019-07-24 stsp break;
6028 0ebf8283 2019-07-24 stsp }
6029 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6030 0ebf8283 2019-07-24 stsp p++;
6031 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6032 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6033 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6034 0ebf8283 2019-07-24 stsp break;
6035 0ebf8283 2019-07-24 stsp }
6036 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6037 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6038 0ebf8283 2019-07-24 stsp if (err)
6039 0ebf8283 2019-07-24 stsp break;
6040 0ebf8283 2019-07-24 stsp } else {
6041 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6042 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6043 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6044 0ebf8283 2019-07-24 stsp break;
6045 0ebf8283 2019-07-24 stsp }
6046 0ebf8283 2019-07-24 stsp }
6047 0ebf8283 2019-07-24 stsp free(line);
6048 0ebf8283 2019-07-24 stsp line = NULL;
6049 0ebf8283 2019-07-24 stsp continue;
6050 0ebf8283 2019-07-24 stsp } else {
6051 0ebf8283 2019-07-24 stsp end = p;
6052 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6053 0ebf8283 2019-07-24 stsp end++;
6054 0ebf8283 2019-07-24 stsp *end = '\0';
6055 0ebf8283 2019-07-24 stsp
6056 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6057 0ebf8283 2019-07-24 stsp if (err) {
6058 0ebf8283 2019-07-24 stsp /* override error code */
6059 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6060 0ebf8283 2019-07-24 stsp break;
6061 0ebf8283 2019-07-24 stsp }
6062 0ebf8283 2019-07-24 stsp }
6063 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6064 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6065 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6066 0ebf8283 2019-07-24 stsp break;
6067 0ebf8283 2019-07-24 stsp }
6068 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6069 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6070 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6071 0ebf8283 2019-07-24 stsp commit_id = NULL;
6072 0ebf8283 2019-07-24 stsp free(line);
6073 0ebf8283 2019-07-24 stsp line = NULL;
6074 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6075 0ebf8283 2019-07-24 stsp }
6076 0ebf8283 2019-07-24 stsp
6077 0ebf8283 2019-07-24 stsp free(line);
6078 0ebf8283 2019-07-24 stsp free(commit_id);
6079 0ebf8283 2019-07-24 stsp return err;
6080 0ebf8283 2019-07-24 stsp }
6081 0ebf8283 2019-07-24 stsp
6082 0ebf8283 2019-07-24 stsp static const struct got_error *
6083 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6084 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6085 bfce7f83 2019-07-27 stsp {
6086 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6087 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6088 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6089 bfce7f83 2019-07-27 stsp static char msg[80];
6090 bfce7f83 2019-07-27 stsp char *id_str;
6091 bfce7f83 2019-07-27 stsp
6092 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6093 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6094 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6095 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6096 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6097 bfce7f83 2019-07-27 stsp
6098 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6099 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6100 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6101 bfce7f83 2019-07-27 stsp break;
6102 bfce7f83 2019-07-27 stsp }
6103 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6104 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6105 bfce7f83 2019-07-27 stsp if (err)
6106 bfce7f83 2019-07-27 stsp return err;
6107 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6108 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6109 bfce7f83 2019-07-27 stsp free(id_str);
6110 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6111 bfce7f83 2019-07-27 stsp }
6112 bfce7f83 2019-07-27 stsp }
6113 bfce7f83 2019-07-27 stsp
6114 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6115 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6116 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6117 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6118 bfce7f83 2019-07-27 stsp
6119 bfce7f83 2019-07-27 stsp return NULL;
6120 bfce7f83 2019-07-27 stsp }
6121 bfce7f83 2019-07-27 stsp
6122 bfce7f83 2019-07-27 stsp static const struct got_error *
6123 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6124 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6125 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6126 0ebf8283 2019-07-24 stsp {
6127 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6128 0ebf8283 2019-07-24 stsp char *editor;
6129 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6130 0ebf8283 2019-07-24 stsp
6131 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6132 0ebf8283 2019-07-24 stsp if (err)
6133 0ebf8283 2019-07-24 stsp return err;
6134 0ebf8283 2019-07-24 stsp
6135 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6136 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6137 0ebf8283 2019-07-24 stsp goto done;
6138 0ebf8283 2019-07-24 stsp }
6139 0ebf8283 2019-07-24 stsp
6140 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6141 0ebf8283 2019-07-24 stsp if (f == NULL) {
6142 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6143 0ebf8283 2019-07-24 stsp goto done;
6144 0ebf8283 2019-07-24 stsp }
6145 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6146 bfce7f83 2019-07-27 stsp if (err)
6147 bfce7f83 2019-07-27 stsp goto done;
6148 bfce7f83 2019-07-27 stsp
6149 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6150 0ebf8283 2019-07-24 stsp done:
6151 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6152 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6153 0ebf8283 2019-07-24 stsp free(editor);
6154 0ebf8283 2019-07-24 stsp return err;
6155 0ebf8283 2019-07-24 stsp }
6156 0ebf8283 2019-07-24 stsp
6157 0ebf8283 2019-07-24 stsp static const struct got_error *
6158 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6159 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
6160 0ebf8283 2019-07-24 stsp
6161 0ebf8283 2019-07-24 stsp static const struct got_error *
6162 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6163 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6164 0ebf8283 2019-07-24 stsp {
6165 0ebf8283 2019-07-24 stsp const struct got_error *err;
6166 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6167 0ebf8283 2019-07-24 stsp char *path = NULL;
6168 0ebf8283 2019-07-24 stsp
6169 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6170 0ebf8283 2019-07-24 stsp if (err)
6171 0ebf8283 2019-07-24 stsp return err;
6172 0ebf8283 2019-07-24 stsp
6173 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
6174 0ebf8283 2019-07-24 stsp if (err)
6175 0ebf8283 2019-07-24 stsp goto done;
6176 0ebf8283 2019-07-24 stsp
6177 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
6178 0ebf8283 2019-07-24 stsp if (err)
6179 0ebf8283 2019-07-24 stsp goto done;
6180 0ebf8283 2019-07-24 stsp
6181 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
6182 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6183 0ebf8283 2019-07-24 stsp goto done;
6184 0ebf8283 2019-07-24 stsp }
6185 0ebf8283 2019-07-24 stsp f = NULL;
6186 0ebf8283 2019-07-24 stsp
6187 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6188 0ebf8283 2019-07-24 stsp if (err) {
6189 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6190 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6191 0ebf8283 2019-07-24 stsp goto done;
6192 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6193 0ebf8283 2019-07-24 stsp commits, path, repo);
6194 0ebf8283 2019-07-24 stsp }
6195 0ebf8283 2019-07-24 stsp done:
6196 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6197 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6198 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6199 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6200 0ebf8283 2019-07-24 stsp free(path);
6201 0ebf8283 2019-07-24 stsp return err;
6202 0ebf8283 2019-07-24 stsp }
6203 0ebf8283 2019-07-24 stsp
6204 0ebf8283 2019-07-24 stsp static const struct got_error *
6205 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6206 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6207 0ebf8283 2019-07-24 stsp {
6208 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6209 0ebf8283 2019-07-24 stsp char *path = NULL;
6210 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6211 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6212 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6213 0ebf8283 2019-07-24 stsp
6214 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6215 0ebf8283 2019-07-24 stsp if (err)
6216 0ebf8283 2019-07-24 stsp return err;
6217 0ebf8283 2019-07-24 stsp
6218 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6219 0ebf8283 2019-07-24 stsp if (f == NULL) {
6220 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6221 0ebf8283 2019-07-24 stsp goto done;
6222 0ebf8283 2019-07-24 stsp }
6223 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6224 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6225 0ebf8283 2019-07-24 stsp repo);
6226 0ebf8283 2019-07-24 stsp if (err)
6227 0ebf8283 2019-07-24 stsp break;
6228 0ebf8283 2019-07-24 stsp
6229 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6230 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6231 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6232 0ebf8283 2019-07-24 stsp if (n < 0) {
6233 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6234 0ebf8283 2019-07-24 stsp break;
6235 0ebf8283 2019-07-24 stsp }
6236 0ebf8283 2019-07-24 stsp }
6237 0ebf8283 2019-07-24 stsp }
6238 0ebf8283 2019-07-24 stsp done:
6239 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6240 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6241 0ebf8283 2019-07-24 stsp free(path);
6242 0ebf8283 2019-07-24 stsp if (commit)
6243 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6244 0ebf8283 2019-07-24 stsp return err;
6245 0ebf8283 2019-07-24 stsp }
6246 0ebf8283 2019-07-24 stsp
6247 bfce7f83 2019-07-27 stsp void
6248 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6249 bfce7f83 2019-07-27 stsp {
6250 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6251 bfce7f83 2019-07-27 stsp
6252 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6253 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6254 bfce7f83 2019-07-27 stsp free(hle);
6255 bfce7f83 2019-07-27 stsp }
6256 bfce7f83 2019-07-27 stsp }
6257 bfce7f83 2019-07-27 stsp
6258 0ebf8283 2019-07-24 stsp static const struct got_error *
6259 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6260 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6261 0ebf8283 2019-07-24 stsp {
6262 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6263 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6264 0ebf8283 2019-07-24 stsp
6265 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6266 0ebf8283 2019-07-24 stsp if (f == NULL) {
6267 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6268 0ebf8283 2019-07-24 stsp goto done;
6269 0ebf8283 2019-07-24 stsp }
6270 0ebf8283 2019-07-24 stsp
6271 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6272 0ebf8283 2019-07-24 stsp done:
6273 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6274 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6275 0ebf8283 2019-07-24 stsp return err;
6276 0ebf8283 2019-07-24 stsp }
6277 0ebf8283 2019-07-24 stsp
6278 0ebf8283 2019-07-24 stsp static const struct got_error *
6279 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6280 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6281 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6282 0ebf8283 2019-07-24 stsp {
6283 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6284 0ebf8283 2019-07-24 stsp int resp = ' ';
6285 0ebf8283 2019-07-24 stsp
6286 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6287 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6288 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6289 0ebf8283 2019-07-24 stsp resp = getchar();
6290 a61a4414 2019-08-07 stsp if (resp == '\n')
6291 a61a4414 2019-08-07 stsp resp = getchar();
6292 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6293 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6294 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6295 bfce7f83 2019-07-27 stsp repo);
6296 426ebf2e 2019-07-25 stsp if (err) {
6297 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6298 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6299 426ebf2e 2019-07-25 stsp break;
6300 bfce7f83 2019-07-27 stsp prev_err = err;
6301 426ebf2e 2019-07-25 stsp resp = ' ';
6302 426ebf2e 2019-07-25 stsp continue;
6303 426ebf2e 2019-07-25 stsp }
6304 bfce7f83 2019-07-27 stsp break;
6305 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6306 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6307 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6308 0ebf8283 2019-07-24 stsp commits, repo);
6309 426ebf2e 2019-07-25 stsp if (err) {
6310 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6311 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6312 426ebf2e 2019-07-25 stsp break;
6313 bfce7f83 2019-07-27 stsp prev_err = err;
6314 426ebf2e 2019-07-25 stsp resp = ' ';
6315 426ebf2e 2019-07-25 stsp continue;
6316 426ebf2e 2019-07-25 stsp }
6317 bfce7f83 2019-07-27 stsp break;
6318 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6319 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6320 0ebf8283 2019-07-24 stsp break;
6321 426ebf2e 2019-07-25 stsp } else
6322 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6323 0ebf8283 2019-07-24 stsp }
6324 0ebf8283 2019-07-24 stsp
6325 0ebf8283 2019-07-24 stsp return err;
6326 0ebf8283 2019-07-24 stsp }
6327 0ebf8283 2019-07-24 stsp
6328 0ebf8283 2019-07-24 stsp static const struct got_error *
6329 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6330 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6331 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6332 0ebf8283 2019-07-24 stsp {
6333 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6334 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6335 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6336 3e3a69f1 2019-07-25 stsp branch, repo);
6337 0ebf8283 2019-07-24 stsp }
6338 0ebf8283 2019-07-24 stsp
6339 0ebf8283 2019-07-24 stsp static const struct got_error *
6340 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6341 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6342 0ebf8283 2019-07-24 stsp {
6343 0ebf8283 2019-07-24 stsp const struct got_error *err;
6344 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6345 0ebf8283 2019-07-24 stsp
6346 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6347 0ebf8283 2019-07-24 stsp if (err)
6348 0ebf8283 2019-07-24 stsp goto done;
6349 0ebf8283 2019-07-24 stsp
6350 0ebf8283 2019-07-24 stsp if (new_id) {
6351 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6352 0ebf8283 2019-07-24 stsp if (err)
6353 0ebf8283 2019-07-24 stsp goto done;
6354 0ebf8283 2019-07-24 stsp }
6355 0ebf8283 2019-07-24 stsp
6356 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6357 0ebf8283 2019-07-24 stsp if (new_id_str)
6358 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6359 0ebf8283 2019-07-24 stsp
6360 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6361 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6362 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6363 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6364 0ebf8283 2019-07-24 stsp goto done;
6365 0ebf8283 2019-07-24 stsp }
6366 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6367 0ebf8283 2019-07-24 stsp } else {
6368 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6369 0ebf8283 2019-07-24 stsp if (err)
6370 0ebf8283 2019-07-24 stsp goto done;
6371 0ebf8283 2019-07-24 stsp }
6372 0ebf8283 2019-07-24 stsp
6373 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6374 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6375 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6376 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6377 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6378 0ebf8283 2019-07-24 stsp break;
6379 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6380 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6381 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6382 0ebf8283 2019-07-24 stsp logmsg);
6383 0ebf8283 2019-07-24 stsp break;
6384 0ebf8283 2019-07-24 stsp default:
6385 0ebf8283 2019-07-24 stsp break;
6386 0ebf8283 2019-07-24 stsp }
6387 0ebf8283 2019-07-24 stsp
6388 0ebf8283 2019-07-24 stsp done:
6389 0ebf8283 2019-07-24 stsp free(old_id_str);
6390 0ebf8283 2019-07-24 stsp free(new_id_str);
6391 0ebf8283 2019-07-24 stsp return err;
6392 0ebf8283 2019-07-24 stsp }
6393 0ebf8283 2019-07-24 stsp
6394 0ebf8283 2019-07-24 stsp static const struct got_error *
6395 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6396 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6397 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6398 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6399 0ebf8283 2019-07-24 stsp {
6400 0ebf8283 2019-07-24 stsp const struct got_error *err;
6401 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6402 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6403 0ebf8283 2019-07-24 stsp
6404 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6405 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6406 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6407 0ebf8283 2019-07-24 stsp if (err)
6408 0ebf8283 2019-07-24 stsp return err;
6409 0ebf8283 2019-07-24 stsp }
6410 0ebf8283 2019-07-24 stsp
6411 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6412 0ebf8283 2019-07-24 stsp if (err)
6413 0ebf8283 2019-07-24 stsp return err;
6414 0ebf8283 2019-07-24 stsp
6415 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6416 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6417 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6418 0ebf8283 2019-07-24 stsp if (err) {
6419 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6420 0ebf8283 2019-07-24 stsp goto done;
6421 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6422 0ebf8283 2019-07-24 stsp } else {
6423 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6424 0ebf8283 2019-07-24 stsp free(new_commit_id);
6425 0ebf8283 2019-07-24 stsp }
6426 0ebf8283 2019-07-24 stsp done:
6427 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6428 0ebf8283 2019-07-24 stsp return err;
6429 0ebf8283 2019-07-24 stsp }
6430 0ebf8283 2019-07-24 stsp
6431 0ebf8283 2019-07-24 stsp static const struct got_error *
6432 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6433 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6434 0ebf8283 2019-07-24 stsp {
6435 0ebf8283 2019-07-24 stsp const struct got_error *error;
6436 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6437 0ebf8283 2019-07-24 stsp
6438 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6439 0ebf8283 2019-07-24 stsp repo);
6440 0ebf8283 2019-07-24 stsp if (error)
6441 0ebf8283 2019-07-24 stsp return error;
6442 0ebf8283 2019-07-24 stsp
6443 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6444 0ebf8283 2019-07-24 stsp if (error)
6445 0ebf8283 2019-07-24 stsp return error;
6446 0ebf8283 2019-07-24 stsp
6447 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6448 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6449 0ebf8283 2019-07-24 stsp return error;
6450 0ebf8283 2019-07-24 stsp }
6451 0ebf8283 2019-07-24 stsp
6452 0ebf8283 2019-07-24 stsp static const struct got_error *
6453 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6454 0ebf8283 2019-07-24 stsp {
6455 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6456 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6457 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6458 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6459 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6460 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6461 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6462 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6463 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6464 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6465 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6466 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6467 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6468 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6469 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6470 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6471 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6472 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6473 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6474 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6475 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6476 0ebf8283 2019-07-24 stsp
6477 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6478 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6479 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6480 0ebf8283 2019-07-24 stsp
6481 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
6482 0ebf8283 2019-07-24 stsp switch (ch) {
6483 0ebf8283 2019-07-24 stsp case 'a':
6484 0ebf8283 2019-07-24 stsp abort_edit = 1;
6485 0ebf8283 2019-07-24 stsp break;
6486 0ebf8283 2019-07-24 stsp case 'c':
6487 0ebf8283 2019-07-24 stsp continue_edit = 1;
6488 0ebf8283 2019-07-24 stsp break;
6489 0ebf8283 2019-07-24 stsp case 'F':
6490 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6491 0ebf8283 2019-07-24 stsp break;
6492 0ebf8283 2019-07-24 stsp default:
6493 0ebf8283 2019-07-24 stsp usage_histedit();
6494 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6495 0ebf8283 2019-07-24 stsp }
6496 0ebf8283 2019-07-24 stsp }
6497 0ebf8283 2019-07-24 stsp
6498 0ebf8283 2019-07-24 stsp argc -= optind;
6499 0ebf8283 2019-07-24 stsp argv += optind;
6500 0ebf8283 2019-07-24 stsp
6501 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6502 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6503 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6504 0ebf8283 2019-07-24 stsp err(1, "pledge");
6505 0ebf8283 2019-07-24 stsp #endif
6506 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6507 0ebf8283 2019-07-24 stsp usage_histedit();
6508 0ebf8283 2019-07-24 stsp if (argc != 0)
6509 0ebf8283 2019-07-24 stsp usage_histedit();
6510 0ebf8283 2019-07-24 stsp
6511 0ebf8283 2019-07-24 stsp /*
6512 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6513 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6514 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6515 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6516 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6517 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6518 0ebf8283 2019-07-24 stsp */
6519 0ebf8283 2019-07-24 stsp
6520 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6521 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6522 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6523 0ebf8283 2019-07-24 stsp goto done;
6524 0ebf8283 2019-07-24 stsp }
6525 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6526 0ebf8283 2019-07-24 stsp if (error)
6527 0ebf8283 2019-07-24 stsp goto done;
6528 0ebf8283 2019-07-24 stsp
6529 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6530 c9956ddf 2019-09-08 stsp NULL);
6531 0ebf8283 2019-07-24 stsp if (error != NULL)
6532 0ebf8283 2019-07-24 stsp goto done;
6533 0ebf8283 2019-07-24 stsp
6534 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6535 0ebf8283 2019-07-24 stsp if (error)
6536 0ebf8283 2019-07-24 stsp goto done;
6537 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6538 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6539 0ebf8283 2019-07-24 stsp goto done;
6540 0ebf8283 2019-07-24 stsp }
6541 0ebf8283 2019-07-24 stsp
6542 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6543 0ebf8283 2019-07-24 stsp if (error)
6544 0ebf8283 2019-07-24 stsp goto done;
6545 0ebf8283 2019-07-24 stsp
6546 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6547 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6548 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6549 3e3a69f1 2019-07-25 stsp worktree, repo);
6550 0ebf8283 2019-07-24 stsp if (error)
6551 0ebf8283 2019-07-24 stsp goto done;
6552 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6553 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6554 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6555 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6556 0ebf8283 2019-07-24 stsp if (error)
6557 0ebf8283 2019-07-24 stsp goto done;
6558 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6559 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6560 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6561 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6562 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6563 0ebf8283 2019-07-24 stsp goto done;
6564 0ebf8283 2019-07-24 stsp }
6565 0ebf8283 2019-07-24 stsp
6566 0ebf8283 2019-07-24 stsp if (continue_edit) {
6567 0ebf8283 2019-07-24 stsp char *path;
6568 0ebf8283 2019-07-24 stsp
6569 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6570 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6571 0ebf8283 2019-07-24 stsp goto done;
6572 0ebf8283 2019-07-24 stsp }
6573 0ebf8283 2019-07-24 stsp
6574 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6575 0ebf8283 2019-07-24 stsp if (error)
6576 0ebf8283 2019-07-24 stsp goto done;
6577 0ebf8283 2019-07-24 stsp
6578 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6579 0ebf8283 2019-07-24 stsp free(path);
6580 0ebf8283 2019-07-24 stsp if (error)
6581 0ebf8283 2019-07-24 stsp goto done;
6582 0ebf8283 2019-07-24 stsp
6583 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6584 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6585 3e3a69f1 2019-07-25 stsp worktree, repo);
6586 0ebf8283 2019-07-24 stsp if (error)
6587 0ebf8283 2019-07-24 stsp goto done;
6588 0ebf8283 2019-07-24 stsp
6589 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6590 0ebf8283 2019-07-24 stsp if (error)
6591 0ebf8283 2019-07-24 stsp goto done;
6592 0ebf8283 2019-07-24 stsp
6593 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6594 0ebf8283 2019-07-24 stsp head_commit_id);
6595 0ebf8283 2019-07-24 stsp if (error)
6596 0ebf8283 2019-07-24 stsp goto done;
6597 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6598 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6599 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6600 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6601 3aac7cf7 2019-07-25 stsp goto done;
6602 3aac7cf7 2019-07-25 stsp }
6603 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6604 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6605 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6606 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6607 0ebf8283 2019-07-24 stsp commit = NULL;
6608 0ebf8283 2019-07-24 stsp if (error)
6609 0ebf8283 2019-07-24 stsp goto done;
6610 0ebf8283 2019-07-24 stsp } else {
6611 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6612 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6613 0ebf8283 2019-07-24 stsp goto done;
6614 0ebf8283 2019-07-24 stsp }
6615 0ebf8283 2019-07-24 stsp
6616 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6617 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6618 0ebf8283 2019-07-24 stsp if (error != NULL)
6619 0ebf8283 2019-07-24 stsp goto done;
6620 0ebf8283 2019-07-24 stsp
6621 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6622 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6623 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6624 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6625 c7d20a3f 2019-07-30 stsp goto done;
6626 c7d20a3f 2019-07-30 stsp }
6627 c7d20a3f 2019-07-30 stsp
6628 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6629 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6630 bfce7f83 2019-07-27 stsp branch = NULL;
6631 0ebf8283 2019-07-24 stsp if (error)
6632 0ebf8283 2019-07-24 stsp goto done;
6633 0ebf8283 2019-07-24 stsp
6634 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6635 0ebf8283 2019-07-24 stsp head_commit_id);
6636 0ebf8283 2019-07-24 stsp if (error)
6637 0ebf8283 2019-07-24 stsp goto done;
6638 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6639 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6640 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6641 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6642 3aac7cf7 2019-07-25 stsp goto done;
6643 3aac7cf7 2019-07-25 stsp }
6644 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6645 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6646 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6647 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6648 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6649 0ebf8283 2019-07-24 stsp commit = NULL;
6650 0ebf8283 2019-07-24 stsp if (error)
6651 0ebf8283 2019-07-24 stsp goto done;
6652 0ebf8283 2019-07-24 stsp
6653 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6654 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6655 bfce7f83 2019-07-27 stsp if (error)
6656 bfce7f83 2019-07-27 stsp goto done;
6657 bfce7f83 2019-07-27 stsp
6658 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6659 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6660 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6661 6ba2bea2 2019-07-27 stsp if (error) {
6662 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6663 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6664 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6665 0ebf8283 2019-07-24 stsp goto done;
6666 6ba2bea2 2019-07-27 stsp }
6667 0ebf8283 2019-07-24 stsp } else {
6668 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6669 0ebf8283 2019-07-24 stsp repo);
6670 6ba2bea2 2019-07-27 stsp if (error) {
6671 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6672 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6673 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6674 0ebf8283 2019-07-24 stsp goto done;
6675 6ba2bea2 2019-07-27 stsp }
6676 0ebf8283 2019-07-24 stsp
6677 0ebf8283 2019-07-24 stsp }
6678 0ebf8283 2019-07-24 stsp
6679 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6680 0ebf8283 2019-07-24 stsp repo);
6681 6ba2bea2 2019-07-27 stsp if (error) {
6682 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6683 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6684 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6685 0ebf8283 2019-07-24 stsp goto done;
6686 6ba2bea2 2019-07-27 stsp }
6687 0ebf8283 2019-07-24 stsp
6688 0ebf8283 2019-07-24 stsp }
6689 0ebf8283 2019-07-24 stsp
6690 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6691 4ec14e60 2019-08-28 hiltjo if (error)
6692 0ebf8283 2019-07-24 stsp goto done;
6693 0ebf8283 2019-07-24 stsp
6694 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6695 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6696 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6697 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6698 0ebf8283 2019-07-24 stsp continue;
6699 0ebf8283 2019-07-24 stsp
6700 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6701 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6702 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6703 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6704 0ebf8283 2019-07-24 stsp repo);
6705 0ebf8283 2019-07-24 stsp } else {
6706 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6707 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6708 0ebf8283 2019-07-24 stsp }
6709 0ebf8283 2019-07-24 stsp if (error)
6710 0ebf8283 2019-07-24 stsp goto done;
6711 0ebf8283 2019-07-24 stsp continue;
6712 0ebf8283 2019-07-24 stsp }
6713 0ebf8283 2019-07-24 stsp
6714 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6715 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6716 0ebf8283 2019-07-24 stsp if (error)
6717 0ebf8283 2019-07-24 stsp goto done;
6718 0ebf8283 2019-07-24 stsp continue;
6719 0ebf8283 2019-07-24 stsp }
6720 0ebf8283 2019-07-24 stsp
6721 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6722 0ebf8283 2019-07-24 stsp hle->commit_id);
6723 0ebf8283 2019-07-24 stsp if (error)
6724 0ebf8283 2019-07-24 stsp goto done;
6725 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6726 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6727 0ebf8283 2019-07-24 stsp
6728 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6729 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6730 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6731 0ebf8283 2019-07-24 stsp if (error)
6732 0ebf8283 2019-07-24 stsp goto done;
6733 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6734 0ebf8283 2019-07-24 stsp commit = NULL;
6735 0ebf8283 2019-07-24 stsp
6736 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6737 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6738 0ebf8283 2019-07-24 stsp break;
6739 0ebf8283 2019-07-24 stsp }
6740 0ebf8283 2019-07-24 stsp
6741 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6742 0ebf8283 2019-07-24 stsp char *id_str;
6743 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6744 0ebf8283 2019-07-24 stsp if (error)
6745 0ebf8283 2019-07-24 stsp goto done;
6746 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6747 0ebf8283 2019-07-24 stsp id_str);
6748 0ebf8283 2019-07-24 stsp free(id_str);
6749 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6750 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6751 3e3a69f1 2019-07-25 stsp fileindex);
6752 0ebf8283 2019-07-24 stsp goto done;
6753 0ebf8283 2019-07-24 stsp }
6754 0ebf8283 2019-07-24 stsp
6755 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6756 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6757 0ebf8283 2019-07-24 stsp if (error)
6758 0ebf8283 2019-07-24 stsp goto done;
6759 0ebf8283 2019-07-24 stsp continue;
6760 0ebf8283 2019-07-24 stsp }
6761 0ebf8283 2019-07-24 stsp
6762 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6763 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6764 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6765 0ebf8283 2019-07-24 stsp if (error)
6766 0ebf8283 2019-07-24 stsp goto done;
6767 0ebf8283 2019-07-24 stsp }
6768 0ebf8283 2019-07-24 stsp
6769 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6770 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6771 0ebf8283 2019-07-24 stsp if (error)
6772 0ebf8283 2019-07-24 stsp goto done;
6773 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6774 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6775 0ebf8283 2019-07-24 stsp } else
6776 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6777 3e3a69f1 2019-07-25 stsp branch, repo);
6778 0ebf8283 2019-07-24 stsp done:
6779 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6780 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6781 0ebf8283 2019-07-24 stsp free(head_commit_id);
6782 0ebf8283 2019-07-24 stsp free(base_commit_id);
6783 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6784 0ebf8283 2019-07-24 stsp if (commit)
6785 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6786 0ebf8283 2019-07-24 stsp if (branch)
6787 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6788 0ebf8283 2019-07-24 stsp if (tmp_branch)
6789 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6790 0ebf8283 2019-07-24 stsp if (worktree)
6791 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6792 2822a352 2019-10-15 stsp if (repo)
6793 2822a352 2019-10-15 stsp got_repo_close(repo);
6794 2822a352 2019-10-15 stsp return error;
6795 2822a352 2019-10-15 stsp }
6796 2822a352 2019-10-15 stsp
6797 2822a352 2019-10-15 stsp __dead static void
6798 2822a352 2019-10-15 stsp usage_integrate(void)
6799 2822a352 2019-10-15 stsp {
6800 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6801 2822a352 2019-10-15 stsp exit(1);
6802 2822a352 2019-10-15 stsp }
6803 2822a352 2019-10-15 stsp
6804 2822a352 2019-10-15 stsp static const struct got_error *
6805 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6806 2822a352 2019-10-15 stsp {
6807 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6808 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6809 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6810 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6811 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6812 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6813 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6814 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6815 2822a352 2019-10-15 stsp int ch, did_something = 0;
6816 2822a352 2019-10-15 stsp
6817 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6818 2822a352 2019-10-15 stsp switch (ch) {
6819 2822a352 2019-10-15 stsp default:
6820 2822a352 2019-10-15 stsp usage_integrate();
6821 2822a352 2019-10-15 stsp /* NOTREACHED */
6822 2822a352 2019-10-15 stsp }
6823 2822a352 2019-10-15 stsp }
6824 2822a352 2019-10-15 stsp
6825 2822a352 2019-10-15 stsp argc -= optind;
6826 2822a352 2019-10-15 stsp argv += optind;
6827 2822a352 2019-10-15 stsp
6828 2822a352 2019-10-15 stsp if (argc != 1)
6829 2822a352 2019-10-15 stsp usage_integrate();
6830 2822a352 2019-10-15 stsp branch_arg = argv[0];
6831 2822a352 2019-10-15 stsp
6832 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6833 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6834 2822a352 2019-10-15 stsp err(1, "pledge");
6835 2822a352 2019-10-15 stsp
6836 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6837 2822a352 2019-10-15 stsp if (cwd == NULL) {
6838 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6839 2822a352 2019-10-15 stsp goto done;
6840 2822a352 2019-10-15 stsp }
6841 2822a352 2019-10-15 stsp
6842 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6843 2822a352 2019-10-15 stsp if (error)
6844 2822a352 2019-10-15 stsp goto done;
6845 2822a352 2019-10-15 stsp
6846 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6847 2822a352 2019-10-15 stsp if (error)
6848 2822a352 2019-10-15 stsp goto done;
6849 2822a352 2019-10-15 stsp
6850 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6851 2822a352 2019-10-15 stsp NULL);
6852 2822a352 2019-10-15 stsp if (error != NULL)
6853 2822a352 2019-10-15 stsp goto done;
6854 2822a352 2019-10-15 stsp
6855 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6856 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6857 2822a352 2019-10-15 stsp if (error)
6858 2822a352 2019-10-15 stsp goto done;
6859 2822a352 2019-10-15 stsp
6860 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6861 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6862 2822a352 2019-10-15 stsp goto done;
6863 2822a352 2019-10-15 stsp }
6864 2822a352 2019-10-15 stsp
6865 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6866 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6867 2822a352 2019-10-15 stsp if (error)
6868 2822a352 2019-10-15 stsp goto done;
6869 2822a352 2019-10-15 stsp
6870 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6871 2822a352 2019-10-15 stsp if (refname == NULL) {
6872 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6873 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6874 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6875 2822a352 2019-10-15 stsp goto done;
6876 2822a352 2019-10-15 stsp }
6877 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6878 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6879 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6880 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6881 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6882 2822a352 2019-10-15 stsp goto done;
6883 2822a352 2019-10-15 stsp }
6884 2822a352 2019-10-15 stsp
6885 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6886 2822a352 2019-10-15 stsp if (error)
6887 2822a352 2019-10-15 stsp goto done;
6888 2822a352 2019-10-15 stsp
6889 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6890 2822a352 2019-10-15 stsp if (error)
6891 2822a352 2019-10-15 stsp goto done;
6892 2822a352 2019-10-15 stsp
6893 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6894 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6895 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6896 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6897 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6898 2822a352 2019-10-15 stsp goto done;
6899 2822a352 2019-10-15 stsp }
6900 2822a352 2019-10-15 stsp
6901 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6902 2822a352 2019-10-15 stsp if (error) {
6903 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
6904 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
6905 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6906 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6907 2822a352 2019-10-15 stsp goto done;
6908 2822a352 2019-10-15 stsp }
6909 2822a352 2019-10-15 stsp
6910 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
6911 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
6912 2822a352 2019-10-15 stsp check_cancelled, NULL);
6913 2822a352 2019-10-15 stsp if (error)
6914 2822a352 2019-10-15 stsp goto done;
6915 2822a352 2019-10-15 stsp
6916 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
6917 2822a352 2019-10-15 stsp done:
6918 715dc77e 2019-08-03 stsp if (repo)
6919 715dc77e 2019-08-03 stsp got_repo_close(repo);
6920 2822a352 2019-10-15 stsp if (worktree)
6921 2822a352 2019-10-15 stsp got_worktree_close(worktree);
6922 2822a352 2019-10-15 stsp free(cwd);
6923 2822a352 2019-10-15 stsp free(base_commit_id);
6924 2822a352 2019-10-15 stsp free(commit_id);
6925 2822a352 2019-10-15 stsp free(refname);
6926 2822a352 2019-10-15 stsp free(base_refname);
6927 715dc77e 2019-08-03 stsp return error;
6928 715dc77e 2019-08-03 stsp }
6929 715dc77e 2019-08-03 stsp
6930 715dc77e 2019-08-03 stsp __dead static void
6931 715dc77e 2019-08-03 stsp usage_stage(void)
6932 715dc77e 2019-08-03 stsp {
6933 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6934 f3055044 2019-08-07 stsp "[file-path ...]\n",
6935 715dc77e 2019-08-03 stsp getprogname());
6936 715dc77e 2019-08-03 stsp exit(1);
6937 715dc77e 2019-08-03 stsp }
6938 715dc77e 2019-08-03 stsp
6939 715dc77e 2019-08-03 stsp static const struct got_error *
6940 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6941 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6942 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6943 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6944 408b4ebc 2019-08-03 stsp {
6945 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6946 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6947 408b4ebc 2019-08-03 stsp
6948 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6949 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6950 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6951 408b4ebc 2019-08-03 stsp return NULL;
6952 408b4ebc 2019-08-03 stsp
6953 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6954 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6955 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6956 408b4ebc 2019-08-03 stsp else
6957 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6958 408b4ebc 2019-08-03 stsp if (err)
6959 408b4ebc 2019-08-03 stsp return err;
6960 408b4ebc 2019-08-03 stsp
6961 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6962 408b4ebc 2019-08-03 stsp free(id_str);
6963 dc424a06 2019-08-07 stsp return NULL;
6964 dc424a06 2019-08-07 stsp }
6965 2e1f37b0 2019-08-08 stsp
6966 dc424a06 2019-08-07 stsp static const struct got_error *
6967 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6968 715dc77e 2019-08-03 stsp {
6969 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6970 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6971 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6972 715dc77e 2019-08-03 stsp char *cwd = NULL;
6973 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6974 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6975 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6976 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6977 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6978 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6979 715dc77e 2019-08-03 stsp
6980 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6981 715dc77e 2019-08-03 stsp
6982 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6983 715dc77e 2019-08-03 stsp switch (ch) {
6984 408b4ebc 2019-08-03 stsp case 'l':
6985 408b4ebc 2019-08-03 stsp list_stage = 1;
6986 408b4ebc 2019-08-03 stsp break;
6987 dc424a06 2019-08-07 stsp case 'p':
6988 dc424a06 2019-08-07 stsp pflag = 1;
6989 dc424a06 2019-08-07 stsp break;
6990 dc424a06 2019-08-07 stsp case 'F':
6991 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6992 dc424a06 2019-08-07 stsp break;
6993 715dc77e 2019-08-03 stsp default:
6994 715dc77e 2019-08-03 stsp usage_stage();
6995 715dc77e 2019-08-03 stsp /* NOTREACHED */
6996 715dc77e 2019-08-03 stsp }
6997 715dc77e 2019-08-03 stsp }
6998 715dc77e 2019-08-03 stsp
6999 715dc77e 2019-08-03 stsp argc -= optind;
7000 715dc77e 2019-08-03 stsp argv += optind;
7001 715dc77e 2019-08-03 stsp
7002 715dc77e 2019-08-03 stsp #ifndef PROFILE
7003 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7004 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7005 715dc77e 2019-08-03 stsp err(1, "pledge");
7006 715dc77e 2019-08-03 stsp #endif
7007 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7008 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7009 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7010 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7011 715dc77e 2019-08-03 stsp
7012 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7013 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7014 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7015 715dc77e 2019-08-03 stsp goto done;
7016 715dc77e 2019-08-03 stsp }
7017 715dc77e 2019-08-03 stsp
7018 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7019 715dc77e 2019-08-03 stsp if (error)
7020 715dc77e 2019-08-03 stsp goto done;
7021 715dc77e 2019-08-03 stsp
7022 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7023 c9956ddf 2019-09-08 stsp NULL);
7024 715dc77e 2019-08-03 stsp if (error != NULL)
7025 715dc77e 2019-08-03 stsp goto done;
7026 715dc77e 2019-08-03 stsp
7027 dc424a06 2019-08-07 stsp if (patch_script_path) {
7028 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7029 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7030 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7031 dc424a06 2019-08-07 stsp patch_script_path);
7032 dc424a06 2019-08-07 stsp goto done;
7033 dc424a06 2019-08-07 stsp }
7034 dc424a06 2019-08-07 stsp }
7035 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7036 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7037 715dc77e 2019-08-03 stsp if (error)
7038 715dc77e 2019-08-03 stsp goto done;
7039 715dc77e 2019-08-03 stsp
7040 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7041 6d022e97 2019-08-04 stsp if (error)
7042 6d022e97 2019-08-04 stsp goto done;
7043 715dc77e 2019-08-03 stsp
7044 6d022e97 2019-08-04 stsp if (list_stage)
7045 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7046 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7047 2e1f37b0 2019-08-08 stsp else {
7048 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7049 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7050 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7051 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7052 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7053 2e1f37b0 2019-08-08 stsp }
7054 ad493afc 2019-08-03 stsp done:
7055 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7056 2e1f37b0 2019-08-08 stsp error == NULL)
7057 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7058 ad493afc 2019-08-03 stsp if (repo)
7059 ad493afc 2019-08-03 stsp got_repo_close(repo);
7060 ad493afc 2019-08-03 stsp if (worktree)
7061 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7062 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7063 ad493afc 2019-08-03 stsp free((char *)pe->path);
7064 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7065 ad493afc 2019-08-03 stsp free(cwd);
7066 ad493afc 2019-08-03 stsp return error;
7067 ad493afc 2019-08-03 stsp }
7068 ad493afc 2019-08-03 stsp
7069 ad493afc 2019-08-03 stsp __dead static void
7070 ad493afc 2019-08-03 stsp usage_unstage(void)
7071 ad493afc 2019-08-03 stsp {
7072 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7073 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7074 ad493afc 2019-08-03 stsp getprogname());
7075 ad493afc 2019-08-03 stsp exit(1);
7076 ad493afc 2019-08-03 stsp }
7077 ad493afc 2019-08-03 stsp
7078 ad493afc 2019-08-03 stsp
7079 ad493afc 2019-08-03 stsp static const struct got_error *
7080 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7081 ad493afc 2019-08-03 stsp {
7082 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7083 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7084 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7085 ad493afc 2019-08-03 stsp char *cwd = NULL;
7086 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7087 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7088 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7089 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7090 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7091 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7092 ad493afc 2019-08-03 stsp
7093 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7094 ad493afc 2019-08-03 stsp
7095 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7096 ad493afc 2019-08-03 stsp switch (ch) {
7097 2e1f37b0 2019-08-08 stsp case 'p':
7098 2e1f37b0 2019-08-08 stsp pflag = 1;
7099 2e1f37b0 2019-08-08 stsp break;
7100 2e1f37b0 2019-08-08 stsp case 'F':
7101 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7102 2e1f37b0 2019-08-08 stsp break;
7103 ad493afc 2019-08-03 stsp default:
7104 ad493afc 2019-08-03 stsp usage_unstage();
7105 ad493afc 2019-08-03 stsp /* NOTREACHED */
7106 ad493afc 2019-08-03 stsp }
7107 ad493afc 2019-08-03 stsp }
7108 ad493afc 2019-08-03 stsp
7109 ad493afc 2019-08-03 stsp argc -= optind;
7110 ad493afc 2019-08-03 stsp argv += optind;
7111 ad493afc 2019-08-03 stsp
7112 ad493afc 2019-08-03 stsp #ifndef PROFILE
7113 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7114 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7115 ad493afc 2019-08-03 stsp err(1, "pledge");
7116 ad493afc 2019-08-03 stsp #endif
7117 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7118 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7119 2e1f37b0 2019-08-08 stsp
7120 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7121 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7122 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7123 ad493afc 2019-08-03 stsp goto done;
7124 ad493afc 2019-08-03 stsp }
7125 ad493afc 2019-08-03 stsp
7126 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7127 ad493afc 2019-08-03 stsp if (error)
7128 ad493afc 2019-08-03 stsp goto done;
7129 ad493afc 2019-08-03 stsp
7130 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7131 c9956ddf 2019-09-08 stsp NULL);
7132 ad493afc 2019-08-03 stsp if (error != NULL)
7133 ad493afc 2019-08-03 stsp goto done;
7134 ad493afc 2019-08-03 stsp
7135 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7136 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7137 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7138 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7139 2e1f37b0 2019-08-08 stsp patch_script_path);
7140 2e1f37b0 2019-08-08 stsp goto done;
7141 2e1f37b0 2019-08-08 stsp }
7142 2e1f37b0 2019-08-08 stsp }
7143 2e1f37b0 2019-08-08 stsp
7144 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7145 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7146 ad493afc 2019-08-03 stsp if (error)
7147 ad493afc 2019-08-03 stsp goto done;
7148 ad493afc 2019-08-03 stsp
7149 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7150 ad493afc 2019-08-03 stsp if (error)
7151 ad493afc 2019-08-03 stsp goto done;
7152 ad493afc 2019-08-03 stsp
7153 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7154 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7155 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7156 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7157 715dc77e 2019-08-03 stsp done:
7158 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7159 2e1f37b0 2019-08-08 stsp error == NULL)
7160 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7161 0ebf8283 2019-07-24 stsp if (repo)
7162 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7163 715dc77e 2019-08-03 stsp if (worktree)
7164 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7165 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7166 715dc77e 2019-08-03 stsp free((char *)pe->path);
7167 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7168 715dc77e 2019-08-03 stsp free(cwd);
7169 01073a5d 2019-08-22 stsp return error;
7170 01073a5d 2019-08-22 stsp }
7171 01073a5d 2019-08-22 stsp
7172 01073a5d 2019-08-22 stsp __dead static void
7173 01073a5d 2019-08-22 stsp usage_cat(void)
7174 01073a5d 2019-08-22 stsp {
7175 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7176 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7177 01073a5d 2019-08-22 stsp exit(1);
7178 01073a5d 2019-08-22 stsp }
7179 01073a5d 2019-08-22 stsp
7180 01073a5d 2019-08-22 stsp static const struct got_error *
7181 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7182 01073a5d 2019-08-22 stsp {
7183 01073a5d 2019-08-22 stsp const struct got_error *err;
7184 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7185 01073a5d 2019-08-22 stsp
7186 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7187 01073a5d 2019-08-22 stsp if (err)
7188 01073a5d 2019-08-22 stsp return err;
7189 01073a5d 2019-08-22 stsp
7190 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7191 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7192 01073a5d 2019-08-22 stsp return err;
7193 01073a5d 2019-08-22 stsp }
7194 01073a5d 2019-08-22 stsp
7195 01073a5d 2019-08-22 stsp static const struct got_error *
7196 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7197 01073a5d 2019-08-22 stsp {
7198 01073a5d 2019-08-22 stsp const struct got_error *err;
7199 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7200 56e0773d 2019-11-28 stsp int nentries, i;
7201 01073a5d 2019-08-22 stsp
7202 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7203 01073a5d 2019-08-22 stsp if (err)
7204 01073a5d 2019-08-22 stsp return err;
7205 01073a5d 2019-08-22 stsp
7206 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7207 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7208 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7209 01073a5d 2019-08-22 stsp char *id_str;
7210 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7211 01073a5d 2019-08-22 stsp break;
7212 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7213 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7214 01073a5d 2019-08-22 stsp if (err)
7215 01073a5d 2019-08-22 stsp break;
7216 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7217 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7218 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7219 01073a5d 2019-08-22 stsp free(id_str);
7220 01073a5d 2019-08-22 stsp }
7221 01073a5d 2019-08-22 stsp
7222 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7223 01073a5d 2019-08-22 stsp return err;
7224 01073a5d 2019-08-22 stsp }
7225 01073a5d 2019-08-22 stsp
7226 01073a5d 2019-08-22 stsp static const struct got_error *
7227 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7228 01073a5d 2019-08-22 stsp {
7229 01073a5d 2019-08-22 stsp const struct got_error *err;
7230 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7231 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7232 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7233 01073a5d 2019-08-22 stsp char *id_str = NULL;
7234 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7235 01073a5d 2019-08-22 stsp
7236 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7237 01073a5d 2019-08-22 stsp if (err)
7238 01073a5d 2019-08-22 stsp return err;
7239 01073a5d 2019-08-22 stsp
7240 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7241 01073a5d 2019-08-22 stsp if (err)
7242 01073a5d 2019-08-22 stsp goto done;
7243 01073a5d 2019-08-22 stsp
7244 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7245 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7246 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7247 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7248 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7249 01073a5d 2019-08-22 stsp char *pid_str;
7250 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7251 01073a5d 2019-08-22 stsp if (err)
7252 01073a5d 2019-08-22 stsp goto done;
7253 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7254 01073a5d 2019-08-22 stsp free(pid_str);
7255 01073a5d 2019-08-22 stsp }
7256 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7257 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7258 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7259 01073a5d 2019-08-22 stsp
7260 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7261 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7262 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7263 01073a5d 2019-08-22 stsp
7264 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7265 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7266 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7267 01073a5d 2019-08-22 stsp done:
7268 01073a5d 2019-08-22 stsp free(id_str);
7269 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7270 01073a5d 2019-08-22 stsp return err;
7271 01073a5d 2019-08-22 stsp }
7272 01073a5d 2019-08-22 stsp
7273 01073a5d 2019-08-22 stsp static const struct got_error *
7274 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7275 01073a5d 2019-08-22 stsp {
7276 01073a5d 2019-08-22 stsp const struct got_error *err;
7277 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7278 01073a5d 2019-08-22 stsp char *id_str = NULL;
7279 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7280 01073a5d 2019-08-22 stsp
7281 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7282 01073a5d 2019-08-22 stsp if (err)
7283 01073a5d 2019-08-22 stsp return err;
7284 01073a5d 2019-08-22 stsp
7285 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7286 01073a5d 2019-08-22 stsp if (err)
7287 01073a5d 2019-08-22 stsp goto done;
7288 01073a5d 2019-08-22 stsp
7289 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7290 e15d5241 2019-08-22 stsp
7291 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7292 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7293 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7294 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7295 01073a5d 2019-08-22 stsp break;
7296 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7297 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7298 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7299 01073a5d 2019-08-22 stsp break;
7300 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7301 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7302 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7303 01073a5d 2019-08-22 stsp break;
7304 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7305 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7306 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7307 01073a5d 2019-08-22 stsp break;
7308 01073a5d 2019-08-22 stsp default:
7309 01073a5d 2019-08-22 stsp break;
7310 01073a5d 2019-08-22 stsp }
7311 01073a5d 2019-08-22 stsp
7312 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7313 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7314 e15d5241 2019-08-22 stsp
7315 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7316 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7317 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7318 01073a5d 2019-08-22 stsp
7319 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7320 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7321 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7322 01073a5d 2019-08-22 stsp done:
7323 01073a5d 2019-08-22 stsp free(id_str);
7324 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7325 01073a5d 2019-08-22 stsp return err;
7326 01073a5d 2019-08-22 stsp }
7327 01073a5d 2019-08-22 stsp
7328 01073a5d 2019-08-22 stsp static const struct got_error *
7329 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7330 01073a5d 2019-08-22 stsp {
7331 01073a5d 2019-08-22 stsp const struct got_error *error;
7332 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7333 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7334 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7335 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7336 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7337 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7338 01073a5d 2019-08-22 stsp
7339 01073a5d 2019-08-22 stsp #ifndef PROFILE
7340 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7341 01073a5d 2019-08-22 stsp NULL) == -1)
7342 01073a5d 2019-08-22 stsp err(1, "pledge");
7343 01073a5d 2019-08-22 stsp #endif
7344 01073a5d 2019-08-22 stsp
7345 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7346 01073a5d 2019-08-22 stsp switch (ch) {
7347 896e9b6f 2019-08-26 stsp case 'c':
7348 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7349 896e9b6f 2019-08-26 stsp break;
7350 01073a5d 2019-08-22 stsp case 'r':
7351 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7352 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7353 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7354 9ba1d308 2019-10-21 stsp optarg);
7355 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7356 01073a5d 2019-08-22 stsp break;
7357 896e9b6f 2019-08-26 stsp case 'P':
7358 896e9b6f 2019-08-26 stsp force_path = 1;
7359 896e9b6f 2019-08-26 stsp break;
7360 01073a5d 2019-08-22 stsp default:
7361 01073a5d 2019-08-22 stsp usage_cat();
7362 01073a5d 2019-08-22 stsp /* NOTREACHED */
7363 01073a5d 2019-08-22 stsp }
7364 01073a5d 2019-08-22 stsp }
7365 01073a5d 2019-08-22 stsp
7366 01073a5d 2019-08-22 stsp argc -= optind;
7367 01073a5d 2019-08-22 stsp argv += optind;
7368 01073a5d 2019-08-22 stsp
7369 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7370 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7371 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7372 01073a5d 2019-08-22 stsp goto done;
7373 01073a5d 2019-08-22 stsp }
7374 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7375 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7376 01073a5d 2019-08-22 stsp goto done;
7377 01073a5d 2019-08-22 stsp if (worktree) {
7378 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7379 01073a5d 2019-08-22 stsp repo_path = strdup(
7380 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7381 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7382 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7383 01073a5d 2019-08-22 stsp goto done;
7384 01073a5d 2019-08-22 stsp }
7385 01073a5d 2019-08-22 stsp }
7386 01073a5d 2019-08-22 stsp }
7387 01073a5d 2019-08-22 stsp
7388 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7389 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7390 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7391 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7392 01073a5d 2019-08-22 stsp }
7393 01073a5d 2019-08-22 stsp
7394 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7395 01073a5d 2019-08-22 stsp free(repo_path);
7396 01073a5d 2019-08-22 stsp if (error != NULL)
7397 01073a5d 2019-08-22 stsp goto done;
7398 01073a5d 2019-08-22 stsp
7399 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7400 896e9b6f 2019-08-26 stsp if (error)
7401 896e9b6f 2019-08-26 stsp goto done;
7402 896e9b6f 2019-08-26 stsp
7403 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7404 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7405 896e9b6f 2019-08-26 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7406 01073a5d 2019-08-22 stsp if (error)
7407 01073a5d 2019-08-22 stsp goto done;
7408 01073a5d 2019-08-22 stsp
7409 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7410 896e9b6f 2019-08-26 stsp if (force_path) {
7411 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7412 896e9b6f 2019-08-26 stsp argv[i]);
7413 896e9b6f 2019-08-26 stsp if (error)
7414 896e9b6f 2019-08-26 stsp break;
7415 896e9b6f 2019-08-26 stsp } else {
7416 896e9b6f 2019-08-26 stsp error = match_object_id(&id, &label, argv[i],
7417 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7418 896e9b6f 2019-08-26 stsp if (error) {
7419 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7420 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7421 896e9b6f 2019-08-26 stsp break;
7422 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7423 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7424 896e9b6f 2019-08-26 stsp if (error)
7425 896e9b6f 2019-08-26 stsp break;
7426 896e9b6f 2019-08-26 stsp }
7427 896e9b6f 2019-08-26 stsp }
7428 01073a5d 2019-08-22 stsp
7429 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7430 01073a5d 2019-08-22 stsp if (error)
7431 01073a5d 2019-08-22 stsp break;
7432 01073a5d 2019-08-22 stsp
7433 01073a5d 2019-08-22 stsp switch (obj_type) {
7434 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7435 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7436 01073a5d 2019-08-22 stsp break;
7437 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7438 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7439 01073a5d 2019-08-22 stsp break;
7440 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7441 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7442 01073a5d 2019-08-22 stsp break;
7443 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7444 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7445 01073a5d 2019-08-22 stsp break;
7446 01073a5d 2019-08-22 stsp default:
7447 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7448 01073a5d 2019-08-22 stsp break;
7449 01073a5d 2019-08-22 stsp }
7450 01073a5d 2019-08-22 stsp if (error)
7451 01073a5d 2019-08-22 stsp break;
7452 01073a5d 2019-08-22 stsp free(label);
7453 01073a5d 2019-08-22 stsp label = NULL;
7454 01073a5d 2019-08-22 stsp free(id);
7455 01073a5d 2019-08-22 stsp id = NULL;
7456 01073a5d 2019-08-22 stsp }
7457 01073a5d 2019-08-22 stsp
7458 01073a5d 2019-08-22 stsp done:
7459 01073a5d 2019-08-22 stsp free(label);
7460 01073a5d 2019-08-22 stsp free(id);
7461 896e9b6f 2019-08-26 stsp free(commit_id);
7462 01073a5d 2019-08-22 stsp if (worktree)
7463 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7464 01073a5d 2019-08-22 stsp if (repo) {
7465 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7466 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7467 01073a5d 2019-08-22 stsp if (error == NULL)
7468 01073a5d 2019-08-22 stsp error = repo_error;
7469 01073a5d 2019-08-22 stsp }
7470 0ebf8283 2019-07-24 stsp return error;
7471 0ebf8283 2019-07-24 stsp }