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 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
301 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
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 3ce1b845 2019-07-15 stsp done:
362 3ce1b845 2019-07-15 stsp free(repo_path);
363 3ce1b845 2019-07-15 stsp return error;
364 3ce1b845 2019-07-15 stsp }
365 3ce1b845 2019-07-15 stsp
366 3ce1b845 2019-07-15 stsp __dead static void
367 3ce1b845 2019-07-15 stsp usage_import(void)
368 3ce1b845 2019-07-15 stsp {
369 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
370 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
371 3ce1b845 2019-07-15 stsp exit(1);
372 3ce1b845 2019-07-15 stsp }
373 3ce1b845 2019-07-15 stsp
374 3ce1b845 2019-07-15 stsp int
375 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
376 3ce1b845 2019-07-15 stsp {
377 3ce1b845 2019-07-15 stsp pid_t pid;
378 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
379 3ce1b845 2019-07-15 stsp int st = -1;
380 3ce1b845 2019-07-15 stsp
381 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
382 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
383 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
384 3ce1b845 2019-07-15 stsp
385 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
386 3ce1b845 2019-07-15 stsp case -1:
387 3ce1b845 2019-07-15 stsp goto doneediting;
388 3ce1b845 2019-07-15 stsp case 0:
389 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
390 3ce1b845 2019-07-15 stsp _exit(127);
391 3ce1b845 2019-07-15 stsp }
392 3ce1b845 2019-07-15 stsp
393 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
394 3ce1b845 2019-07-15 stsp if (errno != EINTR)
395 3ce1b845 2019-07-15 stsp break;
396 3ce1b845 2019-07-15 stsp
397 3ce1b845 2019-07-15 stsp doneediting:
398 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
399 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
400 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
403 3ce1b845 2019-07-15 stsp errno = EINTR;
404 3ce1b845 2019-07-15 stsp return -1;
405 3ce1b845 2019-07-15 stsp }
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
408 3ce1b845 2019-07-15 stsp }
409 3ce1b845 2019-07-15 stsp
410 3ce1b845 2019-07-15 stsp static const struct got_error *
411 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
412 3ce1b845 2019-07-15 stsp const char *initial_content)
413 3ce1b845 2019-07-15 stsp {
414 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
415 3ce1b845 2019-07-15 stsp char buf[1024];
416 3ce1b845 2019-07-15 stsp struct stat st, st2;
417 3ce1b845 2019-07-15 stsp FILE *fp;
418 3ce1b845 2019-07-15 stsp int content_changed = 0;
419 3ce1b845 2019-07-15 stsp size_t len;
420 3ce1b845 2019-07-15 stsp
421 3ce1b845 2019-07-15 stsp *logmsg = NULL;
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
424 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
427 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
428 3ce1b845 2019-07-15 stsp
429 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
430 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
433 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
434 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
435 3ce1b845 2019-07-15 stsp
436 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
437 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
438 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
439 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
440 3ce1b845 2019-07-15 stsp len = 0;
441 3ce1b845 2019-07-15 stsp
442 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
443 3ce1b845 2019-07-15 stsp if (fp == NULL) {
444 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
445 3ce1b845 2019-07-15 stsp goto done;
446 3ce1b845 2019-07-15 stsp }
447 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
448 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
449 3ce1b845 2019-07-15 stsp content_changed = 1;
450 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
451 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
452 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
453 3ce1b845 2019-07-15 stsp }
454 3ce1b845 2019-07-15 stsp fclose(fp);
455 3ce1b845 2019-07-15 stsp
456 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
457 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
458 3ce1b845 2019-07-15 stsp len--;
459 3ce1b845 2019-07-15 stsp }
460 3ce1b845 2019-07-15 stsp
461 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
462 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
464 3ce1b845 2019-07-15 stsp done:
465 3ce1b845 2019-07-15 stsp if (err) {
466 3ce1b845 2019-07-15 stsp free(*logmsg);
467 3ce1b845 2019-07-15 stsp *logmsg = NULL;
468 3ce1b845 2019-07-15 stsp }
469 3ce1b845 2019-07-15 stsp return err;
470 3ce1b845 2019-07-15 stsp }
471 3ce1b845 2019-07-15 stsp
472 3ce1b845 2019-07-15 stsp static const struct got_error *
473 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
475 3ce1b845 2019-07-15 stsp {
476 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
477 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
478 3ce1b845 2019-07-15 stsp int fd;
479 3ce1b845 2019-07-15 stsp
480 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
481 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
482 3ce1b845 2019-07-15 stsp branch_name) == -1)
483 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
484 3ce1b845 2019-07-15 stsp
485 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
486 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
487 3ce1b845 2019-07-15 stsp if (err)
488 3ce1b845 2019-07-15 stsp goto done;
489 3ce1b845 2019-07-15 stsp
490 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
491 3ce1b845 2019-07-15 stsp close(fd);
492 3ce1b845 2019-07-15 stsp
493 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
494 3ce1b845 2019-07-15 stsp done:
495 3ce1b845 2019-07-15 stsp free(initial_content);
496 3ce1b845 2019-07-15 stsp return err;
497 3ce1b845 2019-07-15 stsp }
498 3ce1b845 2019-07-15 stsp
499 3ce1b845 2019-07-15 stsp static const struct got_error *
500 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
501 3ce1b845 2019-07-15 stsp {
502 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
503 3ce1b845 2019-07-15 stsp return NULL;
504 3ce1b845 2019-07-15 stsp }
505 3ce1b845 2019-07-15 stsp
506 3ce1b845 2019-07-15 stsp static const struct got_error *
507 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
508 84792843 2019-08-09 stsp {
509 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
510 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
511 84792843 2019-08-09 stsp
512 84792843 2019-08-09 stsp *author = NULL;
513 aba9c984 2019-09-08 stsp
514 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
515 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
516 c9956ddf 2019-09-08 stsp if (name && email) {
517 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
518 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
519 aba9c984 2019-09-08 stsp return NULL;
520 aba9c984 2019-09-08 stsp }
521 84792843 2019-08-09 stsp
522 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
523 84792843 2019-08-09 stsp if (got_author == NULL) {
524 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
525 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
526 c9956ddf 2019-09-08 stsp if (name && email) {
527 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
528 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
529 c9956ddf 2019-09-08 stsp return NULL;
530 c9956ddf 2019-09-08 stsp }
531 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
532 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
533 84792843 2019-08-09 stsp }
534 84792843 2019-08-09 stsp
535 aba9c984 2019-09-08 stsp *author = strdup(got_author);
536 aba9c984 2019-09-08 stsp if (*author == NULL)
537 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
538 84792843 2019-08-09 stsp
539 84792843 2019-08-09 stsp /*
540 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
541 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
542 84792843 2019-08-09 stsp */
543 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
544 84792843 2019-08-09 stsp got_author++;
545 aba9c984 2019-09-08 stsp if (*got_author != '<') {
546 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
547 aba9c984 2019-09-08 stsp goto done;
548 aba9c984 2019-09-08 stsp }
549 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
550 84792843 2019-08-09 stsp got_author++;
551 aba9c984 2019-09-08 stsp if (*got_author != '@') {
552 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 aba9c984 2019-09-08 stsp goto done;
554 aba9c984 2019-09-08 stsp }
555 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
556 84792843 2019-08-09 stsp got_author++;
557 84792843 2019-08-09 stsp if (*got_author != '>')
558 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 aba9c984 2019-09-08 stsp done:
560 aba9c984 2019-09-08 stsp if (err) {
561 aba9c984 2019-09-08 stsp free(*author);
562 aba9c984 2019-09-08 stsp *author = NULL;
563 aba9c984 2019-09-08 stsp }
564 aba9c984 2019-09-08 stsp return err;
565 c9956ddf 2019-09-08 stsp }
566 c9956ddf 2019-09-08 stsp
567 c9956ddf 2019-09-08 stsp static const struct got_error *
568 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
569 c9956ddf 2019-09-08 stsp {
570 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
571 c9956ddf 2019-09-08 stsp
572 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
573 c9956ddf 2019-09-08 stsp if (homedir) {
574 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
575 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
576 c9956ddf 2019-09-08 stsp
577 c9956ddf 2019-09-08 stsp }
578 c9956ddf 2019-09-08 stsp return NULL;
579 84792843 2019-08-09 stsp }
580 84792843 2019-08-09 stsp
581 84792843 2019-08-09 stsp static const struct got_error *
582 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
583 3ce1b845 2019-07-15 stsp {
584 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
585 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
586 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
587 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
588 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
589 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
590 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
591 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
592 3ce1b845 2019-07-15 stsp int ch;
593 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
594 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
595 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
596 3ce1b845 2019-07-15 stsp
597 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
598 3ce1b845 2019-07-15 stsp
599 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
600 3ce1b845 2019-07-15 stsp switch (ch) {
601 3ce1b845 2019-07-15 stsp case 'b':
602 3ce1b845 2019-07-15 stsp branch_name = optarg;
603 3ce1b845 2019-07-15 stsp break;
604 3ce1b845 2019-07-15 stsp case 'm':
605 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
606 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
607 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
608 3ce1b845 2019-07-15 stsp goto done;
609 3ce1b845 2019-07-15 stsp }
610 3ce1b845 2019-07-15 stsp break;
611 3ce1b845 2019-07-15 stsp case 'r':
612 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
613 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
614 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
615 9ba1d308 2019-10-21 stsp optarg);
616 3ce1b845 2019-07-15 stsp goto done;
617 3ce1b845 2019-07-15 stsp }
618 3ce1b845 2019-07-15 stsp break;
619 3ce1b845 2019-07-15 stsp case 'I':
620 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
621 3ce1b845 2019-07-15 stsp break;
622 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
623 3ce1b845 2019-07-15 stsp NULL);
624 3ce1b845 2019-07-15 stsp if (error)
625 3ce1b845 2019-07-15 stsp goto done;
626 3ce1b845 2019-07-15 stsp break;
627 3ce1b845 2019-07-15 stsp default:
628 b2b65d18 2019-08-22 stsp usage_import();
629 3ce1b845 2019-07-15 stsp /* NOTREACHED */
630 3ce1b845 2019-07-15 stsp }
631 3ce1b845 2019-07-15 stsp }
632 3ce1b845 2019-07-15 stsp
633 3ce1b845 2019-07-15 stsp argc -= optind;
634 3ce1b845 2019-07-15 stsp argv += optind;
635 3ce1b845 2019-07-15 stsp
636 3ce1b845 2019-07-15 stsp #ifndef PROFILE
637 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
638 aba9c984 2019-09-08 stsp "unveil",
639 3ce1b845 2019-07-15 stsp NULL) == -1)
640 3ce1b845 2019-07-15 stsp err(1, "pledge");
641 3ce1b845 2019-07-15 stsp #endif
642 3ce1b845 2019-07-15 stsp if (argc != 1)
643 3ce1b845 2019-07-15 stsp usage_import();
644 2c7829a4 2019-06-17 stsp
645 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
646 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
647 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
648 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
649 3ce1b845 2019-07-15 stsp }
650 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
651 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
652 c9956ddf 2019-09-08 stsp if (error)
653 c9956ddf 2019-09-08 stsp goto done;
654 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
655 3ce1b845 2019-07-15 stsp if (error)
656 3ce1b845 2019-07-15 stsp goto done;
657 aba9c984 2019-09-08 stsp
658 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
659 aba9c984 2019-09-08 stsp if (error)
660 aba9c984 2019-09-08 stsp return error;
661 e560b7e0 2019-11-28 stsp
662 e560b7e0 2019-11-28 stsp /*
663 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
664 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
665 e560b7e0 2019-11-28 stsp * an unintended typo.
666 e560b7e0 2019-11-28 stsp */
667 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
668 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
669 3ce1b845 2019-07-15 stsp
670 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
671 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
672 3ce1b845 2019-07-15 stsp goto done;
673 3ce1b845 2019-07-15 stsp }
674 3ce1b845 2019-07-15 stsp
675 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
676 3ce1b845 2019-07-15 stsp if (error) {
677 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
678 3ce1b845 2019-07-15 stsp goto done;
679 3ce1b845 2019-07-15 stsp } else {
680 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
681 3ce1b845 2019-07-15 stsp "import target branch already exists");
682 3ce1b845 2019-07-15 stsp goto done;
683 3ce1b845 2019-07-15 stsp }
684 3ce1b845 2019-07-15 stsp
685 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
686 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
687 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
688 3ce1b845 2019-07-15 stsp goto done;
689 3ce1b845 2019-07-15 stsp }
690 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
691 3ce1b845 2019-07-15 stsp
692 3ce1b845 2019-07-15 stsp /*
693 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
694 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
695 3ce1b845 2019-07-15 stsp */
696 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
697 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
698 3ce1b845 2019-07-15 stsp if (error)
699 3ce1b845 2019-07-15 stsp goto done;
700 8e158b01 2019-09-22 stsp free(logmsg);
701 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
702 ef293bdd 2019-10-21 stsp path_dir, refname);
703 ef293bdd 2019-10-21 stsp if (error) {
704 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
705 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
706 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
707 3ce1b845 2019-07-15 stsp goto done;
708 ef293bdd 2019-10-21 stsp }
709 3ce1b845 2019-07-15 stsp }
710 3ce1b845 2019-07-15 stsp
711 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
712 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
713 ef293bdd 2019-10-21 stsp if (logmsg_path)
714 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
715 3ce1b845 2019-07-15 stsp goto done;
716 ef293bdd 2019-10-21 stsp }
717 3ce1b845 2019-07-15 stsp
718 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
719 ef293bdd 2019-10-21 stsp if (error) {
720 ef293bdd 2019-10-21 stsp if (logmsg_path)
721 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
722 ef293bdd 2019-10-21 stsp goto done;
723 ef293bdd 2019-10-21 stsp }
724 ef293bdd 2019-10-21 stsp
725 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
726 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
727 ef293bdd 2019-10-21 stsp if (error) {
728 ef293bdd 2019-10-21 stsp if (logmsg_path)
729 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
730 3ce1b845 2019-07-15 stsp goto done;
731 ef293bdd 2019-10-21 stsp }
732 3ce1b845 2019-07-15 stsp
733 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
734 ef293bdd 2019-10-21 stsp if (error) {
735 ef293bdd 2019-10-21 stsp if (logmsg_path)
736 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
737 3ce1b845 2019-07-15 stsp goto done;
738 ef293bdd 2019-10-21 stsp }
739 3ce1b845 2019-07-15 stsp
740 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
741 ef293bdd 2019-10-21 stsp if (error) {
742 ef293bdd 2019-10-21 stsp if (logmsg_path)
743 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
744 3ce1b845 2019-07-15 stsp goto done;
745 ef293bdd 2019-10-21 stsp }
746 3ce1b845 2019-07-15 stsp
747 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
748 ef293bdd 2019-10-21 stsp if (error) {
749 ef293bdd 2019-10-21 stsp if (logmsg_path)
750 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
751 3ce1b845 2019-07-15 stsp goto done;
752 ef293bdd 2019-10-21 stsp }
753 3ce1b845 2019-07-15 stsp
754 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
755 3ce1b845 2019-07-15 stsp if (error) {
756 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
757 ef293bdd 2019-10-21 stsp if (logmsg_path)
758 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
759 3ce1b845 2019-07-15 stsp goto done;
760 ef293bdd 2019-10-21 stsp }
761 3ce1b845 2019-07-15 stsp
762 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
763 3ce1b845 2019-07-15 stsp branch_ref);
764 ef293bdd 2019-10-21 stsp if (error) {
765 ef293bdd 2019-10-21 stsp if (logmsg_path)
766 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
767 3ce1b845 2019-07-15 stsp goto done;
768 ef293bdd 2019-10-21 stsp }
769 3ce1b845 2019-07-15 stsp
770 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
771 ef293bdd 2019-10-21 stsp if (error) {
772 ef293bdd 2019-10-21 stsp if (logmsg_path)
773 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
774 3ce1b845 2019-07-15 stsp goto done;
775 ef293bdd 2019-10-21 stsp }
776 3ce1b845 2019-07-15 stsp }
777 3ce1b845 2019-07-15 stsp
778 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
779 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
780 2c7829a4 2019-06-17 stsp done:
781 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
782 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
783 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
784 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
785 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
786 8e158b01 2019-09-22 stsp free(logmsg);
787 ef293bdd 2019-10-21 stsp free(logmsg_path);
788 2c7829a4 2019-06-17 stsp free(repo_path);
789 3ce1b845 2019-07-15 stsp free(editor);
790 3ce1b845 2019-07-15 stsp free(refname);
791 3ce1b845 2019-07-15 stsp free(new_commit_id);
792 3ce1b845 2019-07-15 stsp free(id_str);
793 aba9c984 2019-09-08 stsp free(author);
794 c9956ddf 2019-09-08 stsp free(gitconfig_path);
795 3ce1b845 2019-07-15 stsp if (branch_ref)
796 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
797 3ce1b845 2019-07-15 stsp if (head_ref)
798 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
799 2c7829a4 2019-06-17 stsp return error;
800 0266afb7 2019-01-04 stsp }
801 0266afb7 2019-01-04 stsp
802 4ed7e80c 2018-05-20 stsp __dead static void
803 c09a553d 2018-03-12 stsp usage_checkout(void)
804 c09a553d 2018-03-12 stsp {
805 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
806 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
807 c09a553d 2018-03-12 stsp exit(1);
808 92a684f4 2018-03-12 stsp }
809 92a684f4 2018-03-12 stsp
810 7f47418f 2019-12-20 stsp static void
811 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
812 7f47418f 2019-12-20 stsp {
813 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
814 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
815 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
816 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
817 7f47418f 2019-12-20 stsp getprogname());
818 7f47418f 2019-12-20 stsp }
819 7f47418f 2019-12-20 stsp
820 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
821 7f47418f 2019-12-20 stsp const char *worktree_path;
822 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
823 7f47418f 2019-12-20 stsp };
824 7f47418f 2019-12-20 stsp
825 1ee397ad 2019-07-12 stsp static const struct got_error *
826 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
827 92a684f4 2018-03-12 stsp {
828 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
829 a484d721 2019-06-10 stsp
830 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
831 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
832 7f47418f 2019-12-20 stsp return NULL;
833 7f47418f 2019-12-20 stsp
834 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
835 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
836 1ee397ad 2019-07-12 stsp return NULL;
837 7f47418f 2019-12-20 stsp }
838 92a684f4 2018-03-12 stsp
839 92a684f4 2018-03-12 stsp while (path[0] == '/')
840 92a684f4 2018-03-12 stsp path++;
841 92a684f4 2018-03-12 stsp
842 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
843 1ee397ad 2019-07-12 stsp return NULL;
844 99437157 2018-11-11 stsp }
845 99437157 2018-11-11 stsp
846 99437157 2018-11-11 stsp static const struct got_error *
847 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
848 99437157 2018-11-11 stsp {
849 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
850 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
851 99437157 2018-11-11 stsp return NULL;
852 8069f636 2019-01-12 stsp }
853 8069f636 2019-01-12 stsp
854 8069f636 2019-01-12 stsp static const struct got_error *
855 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
856 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
857 3aef623b 2019-10-15 stsp struct got_repository *repo)
858 8069f636 2019-01-12 stsp {
859 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
860 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
861 8069f636 2019-01-12 stsp
862 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
863 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
864 36a38700 2019-05-10 stsp if (err)
865 36a38700 2019-05-10 stsp return err;
866 8069f636 2019-01-12 stsp
867 d5bea539 2019-05-13 stsp if (yca_id == NULL)
868 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
869 8069f636 2019-01-12 stsp
870 d5bea539 2019-05-13 stsp /*
871 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
872 d5bea539 2019-05-13 stsp * and the work tree's base commit.
873 d5bea539 2019-05-13 stsp *
874 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
875 d5bea539 2019-05-13 stsp *
876 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
877 d5bea539 2019-05-13 stsp * \ /
878 d5bea539 2019-05-13 stsp * C E
879 d5bea539 2019-05-13 stsp * \ /
880 d5bea539 2019-05-13 stsp * B (yca)
881 d5bea539 2019-05-13 stsp * |
882 d5bea539 2019-05-13 stsp * A
883 d5bea539 2019-05-13 stsp *
884 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
885 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
886 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
887 d5bea539 2019-05-13 stsp */
888 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
889 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
890 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
891 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
892 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
893 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
894 8069f636 2019-01-12 stsp
895 d5bea539 2019-05-13 stsp free(yca_id);
896 d5bea539 2019-05-13 stsp return NULL;
897 c09a553d 2018-03-12 stsp }
898 a367ff0f 2019-05-14 stsp
899 a367ff0f 2019-05-14 stsp static const struct got_error *
900 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
901 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
902 a51a74b3 2019-07-27 stsp struct got_repository *repo)
903 a367ff0f 2019-05-14 stsp {
904 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
905 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
906 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
907 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
908 a367ff0f 2019-05-14 stsp
909 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
910 a367ff0f 2019-05-14 stsp if (err)
911 a51a74b3 2019-07-27 stsp goto done;
912 a51a74b3 2019-07-27 stsp
913 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
914 a51a74b3 2019-07-27 stsp is_same_branch = 1;
915 a51a74b3 2019-07-27 stsp goto done;
916 a51a74b3 2019-07-27 stsp }
917 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
918 a51a74b3 2019-07-27 stsp is_same_branch = 1;
919 a367ff0f 2019-05-14 stsp goto done;
920 a51a74b3 2019-07-27 stsp }
921 a367ff0f 2019-05-14 stsp
922 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
923 a367ff0f 2019-05-14 stsp if (err)
924 a367ff0f 2019-05-14 stsp goto done;
925 a367ff0f 2019-05-14 stsp
926 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
927 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
928 a367ff0f 2019-05-14 stsp if (err)
929 a367ff0f 2019-05-14 stsp goto done;
930 a367ff0f 2019-05-14 stsp
931 a367ff0f 2019-05-14 stsp for (;;) {
932 a367ff0f 2019-05-14 stsp struct got_object_id *id;
933 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
934 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
935 a367ff0f 2019-05-14 stsp if (err) {
936 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
937 a367ff0f 2019-05-14 stsp err = NULL;
938 ee780d5c 2020-01-04 stsp break;
939 a367ff0f 2019-05-14 stsp }
940 c09a553d 2018-03-12 stsp
941 a367ff0f 2019-05-14 stsp if (id) {
942 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
943 a51a74b3 2019-07-27 stsp break;
944 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
945 a367ff0f 2019-05-14 stsp is_same_branch = 1;
946 a367ff0f 2019-05-14 stsp break;
947 a367ff0f 2019-05-14 stsp }
948 a367ff0f 2019-05-14 stsp }
949 a367ff0f 2019-05-14 stsp }
950 a367ff0f 2019-05-14 stsp done:
951 a367ff0f 2019-05-14 stsp if (graph)
952 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
953 a367ff0f 2019-05-14 stsp free(head_commit_id);
954 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
955 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
956 a367ff0f 2019-05-14 stsp return err;
957 a367ff0f 2019-05-14 stsp }
958 8069f636 2019-01-12 stsp
959 4ed7e80c 2018-05-20 stsp static const struct got_error *
960 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
961 c09a553d 2018-03-12 stsp {
962 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
963 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
964 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
965 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
966 c09a553d 2018-03-12 stsp char *repo_path = NULL;
967 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
968 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
969 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
970 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
971 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
972 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
973 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
974 f2ea84fa 2019-07-27 stsp
975 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
976 c09a553d 2018-03-12 stsp
977 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
978 0bb8a95e 2018-03-12 stsp switch (ch) {
979 08573d5b 2019-05-14 stsp case 'b':
980 08573d5b 2019-05-14 stsp branch_name = optarg;
981 08573d5b 2019-05-14 stsp break;
982 8069f636 2019-01-12 stsp case 'c':
983 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
984 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
985 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
986 bb51a5b4 2020-01-13 stsp break;
987 bb51a5b4 2020-01-13 stsp case 'E':
988 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
989 8069f636 2019-01-12 stsp break;
990 0bb8a95e 2018-03-12 stsp case 'p':
991 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
992 0bb8a95e 2018-03-12 stsp break;
993 0bb8a95e 2018-03-12 stsp default:
994 2deda0b9 2019-03-07 stsp usage_checkout();
995 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
996 0bb8a95e 2018-03-12 stsp }
997 0bb8a95e 2018-03-12 stsp }
998 0bb8a95e 2018-03-12 stsp
999 0bb8a95e 2018-03-12 stsp argc -= optind;
1000 0bb8a95e 2018-03-12 stsp argv += optind;
1001 0bb8a95e 2018-03-12 stsp
1002 6715a751 2018-03-16 stsp #ifndef PROFILE
1003 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1004 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1005 c09a553d 2018-03-12 stsp err(1, "pledge");
1006 6715a751 2018-03-16 stsp #endif
1007 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1008 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1009 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1010 76089277 2018-04-01 stsp if (repo_path == NULL)
1011 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1012 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1013 76089277 2018-04-01 stsp if (cwd == NULL) {
1014 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1015 76089277 2018-04-01 stsp goto done;
1016 76089277 2018-04-01 stsp }
1017 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1018 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1019 230a42bd 2019-05-11 jcs if (base == NULL) {
1020 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1021 230a42bd 2019-05-11 jcs path_prefix);
1022 230a42bd 2019-05-11 jcs goto done;
1023 230a42bd 2019-05-11 jcs }
1024 230a42bd 2019-05-11 jcs } else {
1025 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1026 230a42bd 2019-05-11 jcs if (base == NULL) {
1027 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1028 230a42bd 2019-05-11 jcs repo_path);
1029 230a42bd 2019-05-11 jcs goto done;
1030 230a42bd 2019-05-11 jcs }
1031 76089277 2018-04-01 stsp }
1032 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1033 c09a553d 2018-03-12 stsp if (dotgit)
1034 c09a553d 2018-03-12 stsp *dotgit = '\0';
1035 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1036 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1037 c09a553d 2018-03-12 stsp free(cwd);
1038 76089277 2018-04-01 stsp goto done;
1039 c09a553d 2018-03-12 stsp }
1040 c09a553d 2018-03-12 stsp free(cwd);
1041 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1042 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1043 76089277 2018-04-01 stsp if (repo_path == NULL) {
1044 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1045 76089277 2018-04-01 stsp goto done;
1046 76089277 2018-04-01 stsp }
1047 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1048 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1049 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1050 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1051 b4b3a7dd 2019-07-22 stsp argv[1]);
1052 b4b3a7dd 2019-07-22 stsp goto done;
1053 b4b3a7dd 2019-07-22 stsp }
1054 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1055 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1056 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1057 b4b3a7dd 2019-07-22 stsp goto done;
1058 b4b3a7dd 2019-07-22 stsp }
1059 76089277 2018-04-01 stsp }
1060 c09a553d 2018-03-12 stsp } else
1061 c09a553d 2018-03-12 stsp usage_checkout();
1062 c09a553d 2018-03-12 stsp
1063 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1064 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1065 13bfb272 2019-05-10 jcs
1066 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1067 c09a553d 2018-03-12 stsp if (error != NULL)
1068 c02c541e 2019-03-29 stsp goto done;
1069 c02c541e 2019-03-29 stsp
1070 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1071 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1072 c530dc23 2019-07-23 stsp if (error) {
1073 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1074 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1075 c530dc23 2019-07-23 stsp goto done;
1076 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1077 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1078 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1079 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1080 c530dc23 2019-07-23 stsp goto done;
1081 c530dc23 2019-07-23 stsp }
1082 c530dc23 2019-07-23 stsp }
1083 c530dc23 2019-07-23 stsp
1084 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1085 c02c541e 2019-03-29 stsp if (error)
1086 c09a553d 2018-03-12 stsp goto done;
1087 8069f636 2019-01-12 stsp
1088 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1089 c09a553d 2018-03-12 stsp if (error != NULL)
1090 c09a553d 2018-03-12 stsp goto done;
1091 c09a553d 2018-03-12 stsp
1092 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1093 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1094 c09a553d 2018-03-12 stsp goto done;
1095 c09a553d 2018-03-12 stsp
1096 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1097 c09a553d 2018-03-12 stsp if (error != NULL)
1098 c09a553d 2018-03-12 stsp goto done;
1099 c09a553d 2018-03-12 stsp
1100 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1101 e5dc7198 2018-12-29 stsp path_prefix);
1102 e5dc7198 2018-12-29 stsp if (error != NULL)
1103 e5dc7198 2018-12-29 stsp goto done;
1104 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1105 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1106 49520a32 2018-12-29 stsp goto done;
1107 49520a32 2018-12-29 stsp }
1108 49520a32 2018-12-29 stsp
1109 8069f636 2019-01-12 stsp if (commit_id_str) {
1110 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1111 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1112 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1113 30837e32 2019-07-25 stsp if (error)
1114 8069f636 2019-01-12 stsp goto done;
1115 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1116 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1117 8069f636 2019-01-12 stsp if (error != NULL) {
1118 8069f636 2019-01-12 stsp free(commit_id);
1119 8069f636 2019-01-12 stsp goto done;
1120 8069f636 2019-01-12 stsp }
1121 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1122 45d344f6 2019-05-14 stsp if (error)
1123 45d344f6 2019-05-14 stsp goto done;
1124 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1125 8069f636 2019-01-12 stsp commit_id);
1126 8069f636 2019-01-12 stsp free(commit_id);
1127 8069f636 2019-01-12 stsp if (error)
1128 8069f636 2019-01-12 stsp goto done;
1129 8069f636 2019-01-12 stsp }
1130 8069f636 2019-01-12 stsp
1131 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1132 f2ea84fa 2019-07-27 stsp if (error)
1133 f2ea84fa 2019-07-27 stsp goto done;
1134 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1135 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1136 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1137 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1138 c09a553d 2018-03-12 stsp if (error != NULL)
1139 c09a553d 2018-03-12 stsp goto done;
1140 c09a553d 2018-03-12 stsp
1141 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1142 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1143 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1144 c09a553d 2018-03-12 stsp done:
1145 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1146 8069f636 2019-01-12 stsp free(commit_id_str);
1147 76089277 2018-04-01 stsp free(repo_path);
1148 507dc3bb 2018-12-29 stsp free(worktree_path);
1149 507dc3bb 2018-12-29 stsp return error;
1150 507dc3bb 2018-12-29 stsp }
1151 507dc3bb 2018-12-29 stsp
1152 507dc3bb 2018-12-29 stsp __dead static void
1153 507dc3bb 2018-12-29 stsp usage_update(void)
1154 507dc3bb 2018-12-29 stsp {
1155 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1156 507dc3bb 2018-12-29 stsp getprogname());
1157 507dc3bb 2018-12-29 stsp exit(1);
1158 507dc3bb 2018-12-29 stsp }
1159 507dc3bb 2018-12-29 stsp
1160 1ee397ad 2019-07-12 stsp static const struct got_error *
1161 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1162 507dc3bb 2018-12-29 stsp {
1163 784955db 2019-01-12 stsp int *did_something = arg;
1164 784955db 2019-01-12 stsp
1165 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1166 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1167 1ee397ad 2019-07-12 stsp return NULL;
1168 507dc3bb 2018-12-29 stsp
1169 1545c615 2019-02-10 stsp *did_something = 1;
1170 a484d721 2019-06-10 stsp
1171 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1172 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1173 1ee397ad 2019-07-12 stsp return NULL;
1174 a484d721 2019-06-10 stsp
1175 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1176 507dc3bb 2018-12-29 stsp path++;
1177 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1178 1ee397ad 2019-07-12 stsp return NULL;
1179 be7061eb 2018-12-30 stsp }
1180 be7061eb 2018-12-30 stsp
1181 be7061eb 2018-12-30 stsp static const struct got_error *
1182 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1183 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1184 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1185 a1fb16d8 2019-05-24 stsp {
1186 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1187 a1fb16d8 2019-05-24 stsp char *base_id_str;
1188 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1189 a1fb16d8 2019-05-24 stsp
1190 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1191 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1192 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1193 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1194 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1195 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1196 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1197 a1fb16d8 2019-05-24 stsp }
1198 a1fb16d8 2019-05-24 stsp
1199 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1200 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1201 a1fb16d8 2019-05-24 stsp if (err) {
1202 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1203 a1fb16d8 2019-05-24 stsp return err;
1204 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1205 a1fb16d8 2019-05-24 stsp }
1206 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1207 a1fb16d8 2019-05-24 stsp return NULL;
1208 a1fb16d8 2019-05-24 stsp
1209 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1210 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1211 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1212 a1fb16d8 2019-05-24 stsp if (err)
1213 a1fb16d8 2019-05-24 stsp return err;
1214 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1215 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1216 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1217 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1218 0ebf8283 2019-07-24 stsp return NULL;
1219 0ebf8283 2019-07-24 stsp }
1220 0ebf8283 2019-07-24 stsp
1221 0ebf8283 2019-07-24 stsp static const struct got_error *
1222 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1223 0ebf8283 2019-07-24 stsp {
1224 0ebf8283 2019-07-24 stsp const struct got_error *err;
1225 0ebf8283 2019-07-24 stsp int in_progress;
1226 0ebf8283 2019-07-24 stsp
1227 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1228 0ebf8283 2019-07-24 stsp if (err)
1229 0ebf8283 2019-07-24 stsp return err;
1230 0ebf8283 2019-07-24 stsp if (in_progress)
1231 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1232 0ebf8283 2019-07-24 stsp
1233 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1234 0ebf8283 2019-07-24 stsp if (err)
1235 0ebf8283 2019-07-24 stsp return err;
1236 0ebf8283 2019-07-24 stsp if (in_progress)
1237 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1238 0ebf8283 2019-07-24 stsp
1239 a1fb16d8 2019-05-24 stsp return NULL;
1240 a5edda0a 2019-07-27 stsp }
1241 a5edda0a 2019-07-27 stsp
1242 a5edda0a 2019-07-27 stsp static const struct got_error *
1243 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1244 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1245 a5edda0a 2019-07-27 stsp {
1246 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1247 a5edda0a 2019-07-27 stsp char *path;
1248 a5edda0a 2019-07-27 stsp int i;
1249 a5edda0a 2019-07-27 stsp
1250 a5edda0a 2019-07-27 stsp if (argc == 0) {
1251 a5edda0a 2019-07-27 stsp path = strdup("");
1252 a5edda0a 2019-07-27 stsp if (path == NULL)
1253 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1254 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1255 a5edda0a 2019-07-27 stsp }
1256 a5edda0a 2019-07-27 stsp
1257 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1258 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1259 a5edda0a 2019-07-27 stsp if (err)
1260 a5edda0a 2019-07-27 stsp break;
1261 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1262 a5edda0a 2019-07-27 stsp if (err) {
1263 a5edda0a 2019-07-27 stsp free(path);
1264 a5edda0a 2019-07-27 stsp break;
1265 a5edda0a 2019-07-27 stsp }
1266 a5edda0a 2019-07-27 stsp }
1267 a5edda0a 2019-07-27 stsp
1268 a5edda0a 2019-07-27 stsp return err;
1269 a1fb16d8 2019-05-24 stsp }
1270 a1fb16d8 2019-05-24 stsp
1271 a1fb16d8 2019-05-24 stsp static const struct got_error *
1272 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1273 507dc3bb 2018-12-29 stsp {
1274 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1275 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1276 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1277 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1278 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1279 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1280 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1281 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1282 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1283 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1284 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1285 507dc3bb 2018-12-29 stsp
1286 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1287 f2ea84fa 2019-07-27 stsp
1288 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1289 507dc3bb 2018-12-29 stsp switch (ch) {
1290 024e9686 2019-05-14 stsp case 'b':
1291 024e9686 2019-05-14 stsp branch_name = optarg;
1292 024e9686 2019-05-14 stsp break;
1293 507dc3bb 2018-12-29 stsp case 'c':
1294 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1295 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1296 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1297 507dc3bb 2018-12-29 stsp break;
1298 507dc3bb 2018-12-29 stsp default:
1299 2deda0b9 2019-03-07 stsp usage_update();
1300 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1301 507dc3bb 2018-12-29 stsp }
1302 507dc3bb 2018-12-29 stsp }
1303 507dc3bb 2018-12-29 stsp
1304 507dc3bb 2018-12-29 stsp argc -= optind;
1305 507dc3bb 2018-12-29 stsp argv += optind;
1306 507dc3bb 2018-12-29 stsp
1307 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1308 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1309 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1310 507dc3bb 2018-12-29 stsp err(1, "pledge");
1311 507dc3bb 2018-12-29 stsp #endif
1312 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1313 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1314 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1315 c4cdcb68 2019-04-03 stsp goto done;
1316 c4cdcb68 2019-04-03 stsp }
1317 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1318 7d5807f4 2019-07-11 stsp if (error)
1319 7d5807f4 2019-07-11 stsp goto done;
1320 7d5807f4 2019-07-11 stsp
1321 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1322 f2ea84fa 2019-07-27 stsp if (error)
1323 f2ea84fa 2019-07-27 stsp goto done;
1324 507dc3bb 2018-12-29 stsp
1325 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1326 c9956ddf 2019-09-08 stsp NULL);
1327 507dc3bb 2018-12-29 stsp if (error != NULL)
1328 507dc3bb 2018-12-29 stsp goto done;
1329 507dc3bb 2018-12-29 stsp
1330 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1331 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1332 087fb88c 2019-08-04 stsp if (error)
1333 087fb88c 2019-08-04 stsp goto done;
1334 087fb88c 2019-08-04 stsp
1335 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1336 0266afb7 2019-01-04 stsp if (error)
1337 0266afb7 2019-01-04 stsp goto done;
1338 0266afb7 2019-01-04 stsp
1339 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1340 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1341 024e9686 2019-05-14 stsp if (error != NULL)
1342 024e9686 2019-05-14 stsp goto done;
1343 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1344 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1345 9c4b8182 2019-01-02 stsp if (error != NULL)
1346 9c4b8182 2019-01-02 stsp goto done;
1347 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1348 507dc3bb 2018-12-29 stsp if (error != NULL)
1349 507dc3bb 2018-12-29 stsp goto done;
1350 507dc3bb 2018-12-29 stsp } else {
1351 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1352 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1353 04f57cb3 2019-07-25 stsp free(commit_id_str);
1354 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1355 30837e32 2019-07-25 stsp if (error)
1356 a297e751 2019-07-11 stsp goto done;
1357 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1358 a297e751 2019-07-11 stsp if (error)
1359 507dc3bb 2018-12-29 stsp goto done;
1360 507dc3bb 2018-12-29 stsp }
1361 35c965b2 2018-12-29 stsp
1362 a1fb16d8 2019-05-24 stsp if (branch_name) {
1363 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1364 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1365 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1366 f2ea84fa 2019-07-27 stsp continue;
1367 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1368 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1369 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1370 024e9686 2019-05-14 stsp goto done;
1371 024e9686 2019-05-14 stsp }
1372 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1373 024e9686 2019-05-14 stsp if (error)
1374 024e9686 2019-05-14 stsp goto done;
1375 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1376 3aef623b 2019-10-15 stsp repo);
1377 a367ff0f 2019-05-14 stsp free(head_commit_id);
1378 024e9686 2019-05-14 stsp if (error != NULL)
1379 024e9686 2019-05-14 stsp goto done;
1380 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1381 a367ff0f 2019-05-14 stsp if (error)
1382 a367ff0f 2019-05-14 stsp goto done;
1383 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1384 024e9686 2019-05-14 stsp if (error)
1385 024e9686 2019-05-14 stsp goto done;
1386 024e9686 2019-05-14 stsp } else {
1387 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1388 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1389 a367ff0f 2019-05-14 stsp if (error != NULL) {
1390 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1391 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1392 024e9686 2019-05-14 stsp goto done;
1393 a367ff0f 2019-05-14 stsp }
1394 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1395 a367ff0f 2019-05-14 stsp if (error)
1396 a367ff0f 2019-05-14 stsp goto done;
1397 024e9686 2019-05-14 stsp }
1398 507dc3bb 2018-12-29 stsp
1399 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1400 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1401 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1402 507dc3bb 2018-12-29 stsp commit_id);
1403 507dc3bb 2018-12-29 stsp if (error)
1404 507dc3bb 2018-12-29 stsp goto done;
1405 507dc3bb 2018-12-29 stsp }
1406 507dc3bb 2018-12-29 stsp
1407 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1408 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1409 507dc3bb 2018-12-29 stsp if (error != NULL)
1410 507dc3bb 2018-12-29 stsp goto done;
1411 9c4b8182 2019-01-02 stsp
1412 784955db 2019-01-12 stsp if (did_something)
1413 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1414 784955db 2019-01-12 stsp else
1415 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1416 507dc3bb 2018-12-29 stsp done:
1417 c09a553d 2018-03-12 stsp free(worktree_path);
1418 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1419 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1420 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1421 507dc3bb 2018-12-29 stsp free(commit_id);
1422 9c4b8182 2019-01-02 stsp free(commit_id_str);
1423 c09a553d 2018-03-12 stsp return error;
1424 c09a553d 2018-03-12 stsp }
1425 c09a553d 2018-03-12 stsp
1426 f42b1b34 2018-03-12 stsp static const struct got_error *
1427 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1428 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1429 63035f9f 2019-10-06 stsp struct got_repository *repo)
1430 5c860e29 2018-03-12 stsp {
1431 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1432 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1433 79109fed 2018-03-27 stsp
1434 44392932 2019-08-25 stsp if (blob_id1) {
1435 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1436 44392932 2019-08-25 stsp if (err)
1437 44392932 2019-08-25 stsp goto done;
1438 44392932 2019-08-25 stsp }
1439 79109fed 2018-03-27 stsp
1440 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1441 44392932 2019-08-25 stsp if (err)
1442 44392932 2019-08-25 stsp goto done;
1443 79109fed 2018-03-27 stsp
1444 44392932 2019-08-25 stsp while (path[0] == '/')
1445 44392932 2019-08-25 stsp path++;
1446 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1447 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1448 44392932 2019-08-25 stsp done:
1449 44392932 2019-08-25 stsp if (blob1)
1450 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1451 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1452 44392932 2019-08-25 stsp return err;
1453 44392932 2019-08-25 stsp }
1454 44392932 2019-08-25 stsp
1455 44392932 2019-08-25 stsp static const struct got_error *
1456 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1457 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1458 63035f9f 2019-10-06 stsp struct got_repository *repo)
1459 44392932 2019-08-25 stsp {
1460 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1461 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1462 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1463 44392932 2019-08-25 stsp
1464 44392932 2019-08-25 stsp if (tree_id1) {
1465 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1466 79109fed 2018-03-27 stsp if (err)
1467 44392932 2019-08-25 stsp goto done;
1468 79109fed 2018-03-27 stsp }
1469 79109fed 2018-03-27 stsp
1470 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1471 0f2b3dca 2018-12-22 stsp if (err)
1472 0f2b3dca 2018-12-22 stsp goto done;
1473 0f2b3dca 2018-12-22 stsp
1474 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1475 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1476 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1477 44392932 2019-08-25 stsp while (path[0] == '/')
1478 44392932 2019-08-25 stsp path++;
1479 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1480 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1481 0f2b3dca 2018-12-22 stsp done:
1482 79109fed 2018-03-27 stsp if (tree1)
1483 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1484 366e0a5f 2019-10-10 stsp if (tree2)
1485 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
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 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1491 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1492 44392932 2019-08-25 stsp {
1493 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1494 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1495 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1496 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1497 44392932 2019-08-25 stsp struct got_object_qid *qid;
1498 44392932 2019-08-25 stsp
1499 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1500 44392932 2019-08-25 stsp if (qid != NULL) {
1501 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1502 44392932 2019-08-25 stsp qid->id);
1503 44392932 2019-08-25 stsp if (err)
1504 44392932 2019-08-25 stsp return err;
1505 44392932 2019-08-25 stsp }
1506 44392932 2019-08-25 stsp
1507 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1508 44392932 2019-08-25 stsp int obj_type;
1509 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1510 44392932 2019-08-25 stsp if (err)
1511 44392932 2019-08-25 stsp goto done;
1512 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1513 44392932 2019-08-25 stsp if (err) {
1514 44392932 2019-08-25 stsp free(obj_id2);
1515 44392932 2019-08-25 stsp goto done;
1516 44392932 2019-08-25 stsp }
1517 44392932 2019-08-25 stsp if (pcommit) {
1518 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1519 44392932 2019-08-25 stsp qid->id, path);
1520 44392932 2019-08-25 stsp if (err) {
1521 44392932 2019-08-25 stsp free(obj_id2);
1522 44392932 2019-08-25 stsp goto done;
1523 44392932 2019-08-25 stsp }
1524 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1525 44392932 2019-08-25 stsp if (err) {
1526 44392932 2019-08-25 stsp free(obj_id2);
1527 44392932 2019-08-25 stsp goto done;
1528 44392932 2019-08-25 stsp }
1529 44392932 2019-08-25 stsp }
1530 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1531 44392932 2019-08-25 stsp if (err) {
1532 44392932 2019-08-25 stsp free(obj_id2);
1533 44392932 2019-08-25 stsp goto done;
1534 44392932 2019-08-25 stsp }
1535 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1536 44392932 2019-08-25 stsp switch (obj_type) {
1537 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1538 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1539 63035f9f 2019-10-06 stsp 0, repo);
1540 44392932 2019-08-25 stsp break;
1541 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1542 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1543 63035f9f 2019-10-06 stsp 0, repo);
1544 44392932 2019-08-25 stsp break;
1545 44392932 2019-08-25 stsp default:
1546 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1547 44392932 2019-08-25 stsp break;
1548 44392932 2019-08-25 stsp }
1549 44392932 2019-08-25 stsp free(obj_id1);
1550 44392932 2019-08-25 stsp free(obj_id2);
1551 44392932 2019-08-25 stsp } else {
1552 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1553 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1554 44392932 2019-08-25 stsp if (err)
1555 44392932 2019-08-25 stsp goto done;
1556 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1557 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1558 44392932 2019-08-25 stsp if (err)
1559 44392932 2019-08-25 stsp goto done;
1560 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1561 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1562 44392932 2019-08-25 stsp }
1563 44392932 2019-08-25 stsp done:
1564 0f2b3dca 2018-12-22 stsp free(id_str1);
1565 0f2b3dca 2018-12-22 stsp free(id_str2);
1566 44392932 2019-08-25 stsp if (pcommit)
1567 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1568 79109fed 2018-03-27 stsp return err;
1569 79109fed 2018-03-27 stsp }
1570 79109fed 2018-03-27 stsp
1571 4bb494d5 2018-06-16 stsp static char *
1572 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1573 6c281f94 2018-06-11 stsp {
1574 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1575 09867e48 2019-08-13 stsp char *p, *s;
1576 09867e48 2019-08-13 stsp
1577 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1578 09867e48 2019-08-13 stsp if (tm == NULL)
1579 09867e48 2019-08-13 stsp return NULL;
1580 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1581 09867e48 2019-08-13 stsp if (s == NULL)
1582 09867e48 2019-08-13 stsp return NULL;
1583 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1584 6c281f94 2018-06-11 stsp if (p)
1585 6c281f94 2018-06-11 stsp *p = '\0';
1586 6c281f94 2018-06-11 stsp return s;
1587 6c281f94 2018-06-11 stsp }
1588 dc424a06 2019-08-07 stsp
1589 6841bf13 2019-11-29 kn static const struct got_error *
1590 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1591 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1592 6841bf13 2019-11-29 kn {
1593 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1594 6841bf13 2019-11-29 kn regmatch_t regmatch;
1595 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1596 6841bf13 2019-11-29 kn
1597 6841bf13 2019-11-29 kn *have_match = 0;
1598 6841bf13 2019-11-29 kn
1599 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1600 6841bf13 2019-11-29 kn if (err)
1601 6841bf13 2019-11-29 kn return err;
1602 6841bf13 2019-11-29 kn
1603 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1604 6841bf13 2019-11-29 kn if (err)
1605 6841bf13 2019-11-29 kn goto done;
1606 6841bf13 2019-11-29 kn
1607 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1608 6841bf13 2019-11-29 kn *have_match = 1;
1609 6841bf13 2019-11-29 kn done:
1610 6841bf13 2019-11-29 kn free(id_str);
1611 6841bf13 2019-11-29 kn free(logmsg);
1612 6841bf13 2019-11-29 kn return err;
1613 6841bf13 2019-11-29 kn }
1614 6841bf13 2019-11-29 kn
1615 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1616 6c281f94 2018-06-11 stsp
1617 79109fed 2018-03-27 stsp static const struct got_error *
1618 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1619 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1620 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1621 79109fed 2018-03-27 stsp {
1622 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1623 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1624 6c281f94 2018-06-11 stsp char datebuf[26];
1625 45d799e2 2018-12-23 stsp time_t committer_time;
1626 45d799e2 2018-12-23 stsp const char *author, *committer;
1627 199a4027 2019-02-02 stsp char *refs_str = NULL;
1628 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1629 5c860e29 2018-03-12 stsp
1630 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1631 199a4027 2019-02-02 stsp char *s;
1632 199a4027 2019-02-02 stsp const char *name;
1633 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1634 a436ad14 2019-08-13 stsp int cmp;
1635 a436ad14 2019-08-13 stsp
1636 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1637 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1638 d9498b20 2019-02-05 stsp continue;
1639 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1640 199a4027 2019-02-02 stsp name += 5;
1641 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1642 7143d404 2019-03-12 stsp continue;
1643 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1644 e34f9ed6 2019-02-02 stsp name += 6;
1645 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1646 141c2bff 2019-02-04 stsp name += 8;
1647 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1648 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1649 5d844a1e 2019-08-13 stsp if (err) {
1650 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1651 5d844a1e 2019-08-13 stsp return err;
1652 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1653 5d844a1e 2019-08-13 stsp err = NULL;
1654 5d844a1e 2019-08-13 stsp tag = NULL;
1655 5d844a1e 2019-08-13 stsp }
1656 a436ad14 2019-08-13 stsp }
1657 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1658 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1659 a436ad14 2019-08-13 stsp if (tag)
1660 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1661 a436ad14 2019-08-13 stsp if (cmp != 0)
1662 a436ad14 2019-08-13 stsp continue;
1663 199a4027 2019-02-02 stsp s = refs_str;
1664 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1665 199a4027 2019-02-02 stsp name) == -1) {
1666 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1667 199a4027 2019-02-02 stsp free(s);
1668 34ca4898 2019-08-13 stsp return err;
1669 199a4027 2019-02-02 stsp }
1670 199a4027 2019-02-02 stsp free(s);
1671 199a4027 2019-02-02 stsp }
1672 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1673 8bf5b3c9 2018-03-17 stsp if (err)
1674 8bf5b3c9 2018-03-17 stsp return err;
1675 788c352e 2018-06-16 stsp
1676 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1677 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1678 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1679 832c249c 2018-06-10 stsp free(id_str);
1680 d3d493d7 2019-02-21 stsp id_str = NULL;
1681 d3d493d7 2019-02-21 stsp free(refs_str);
1682 d3d493d7 2019-02-21 stsp refs_str = NULL;
1683 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1684 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1685 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1686 09867e48 2019-08-13 stsp if (datestr)
1687 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1688 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1689 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1690 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1691 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1692 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1693 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1694 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1695 3fe1abad 2018-06-10 stsp int n = 1;
1696 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1697 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1698 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1699 a0603db2 2018-06-10 stsp if (err)
1700 a0603db2 2018-06-10 stsp return err;
1701 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1702 a0603db2 2018-06-10 stsp free(id_str);
1703 a0603db2 2018-06-10 stsp }
1704 a0603db2 2018-06-10 stsp }
1705 832c249c 2018-06-10 stsp
1706 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1707 5943eee2 2019-08-13 stsp if (err)
1708 5943eee2 2019-08-13 stsp return err;
1709 8bf5b3c9 2018-03-17 stsp
1710 621015ac 2018-07-23 stsp logmsg = logmsg0;
1711 832c249c 2018-06-10 stsp do {
1712 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1713 832c249c 2018-06-10 stsp if (line)
1714 832c249c 2018-06-10 stsp printf(" %s\n", line);
1715 832c249c 2018-06-10 stsp } while (line);
1716 621015ac 2018-07-23 stsp free(logmsg0);
1717 832c249c 2018-06-10 stsp
1718 971751ac 2018-03-27 stsp if (show_patch) {
1719 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1720 971751ac 2018-03-27 stsp if (err == 0)
1721 971751ac 2018-03-27 stsp printf("\n");
1722 971751ac 2018-03-27 stsp }
1723 07862c20 2018-09-15 stsp
1724 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1725 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1726 07862c20 2018-09-15 stsp return err;
1727 f42b1b34 2018-03-12 stsp }
1728 5c860e29 2018-03-12 stsp
1729 f42b1b34 2018-03-12 stsp static const struct got_error *
1730 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1731 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1732 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
1733 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1734 f42b1b34 2018-03-12 stsp {
1735 f42b1b34 2018-03-12 stsp const struct got_error *err;
1736 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1737 6841bf13 2019-11-29 kn regex_t regex;
1738 6841bf13 2019-11-29 kn int have_match;
1739 372ccdbb 2018-06-10 stsp
1740 6841bf13 2019-11-29 kn if (search_pattern &&
1741 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1742 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1743 6841bf13 2019-11-29 kn
1744 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
1745 f42b1b34 2018-03-12 stsp if (err)
1746 f42b1b34 2018-03-12 stsp return err;
1747 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1748 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1749 372ccdbb 2018-06-10 stsp if (err)
1750 fcc85cad 2018-07-22 stsp goto done;
1751 656b1f76 2019-05-11 jcs for (;;) {
1752 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1753 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1754 8bf5b3c9 2018-03-17 stsp
1755 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1756 84453469 2018-11-11 stsp break;
1757 84453469 2018-11-11 stsp
1758 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1759 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1760 372ccdbb 2018-06-10 stsp if (err) {
1761 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1762 9ba79e04 2018-06-11 stsp err = NULL;
1763 ee780d5c 2020-01-04 stsp break;
1764 7e665116 2018-04-02 stsp }
1765 b43fbaa0 2018-06-11 stsp if (id == NULL)
1766 7e665116 2018-04-02 stsp break;
1767 b43fbaa0 2018-06-11 stsp
1768 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1769 b43fbaa0 2018-06-11 stsp if (err)
1770 fcc85cad 2018-07-22 stsp break;
1771 6841bf13 2019-11-29 kn
1772 6841bf13 2019-11-29 kn if (search_pattern) {
1773 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
1774 6841bf13 2019-11-29 kn if (err) {
1775 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1776 6841bf13 2019-11-29 kn break;
1777 6841bf13 2019-11-29 kn }
1778 6841bf13 2019-11-29 kn if (have_match == 0) {
1779 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1780 6841bf13 2019-11-29 kn continue;
1781 6841bf13 2019-11-29 kn }
1782 6841bf13 2019-11-29 kn }
1783 6841bf13 2019-11-29 kn
1784 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1785 44392932 2019-08-25 stsp diff_context, refs);
1786 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1787 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1788 7e665116 2018-04-02 stsp break;
1789 31cedeaf 2018-09-15 stsp }
1790 fcc85cad 2018-07-22 stsp done:
1791 6841bf13 2019-11-29 kn if (search_pattern)
1792 6841bf13 2019-11-29 kn regfree(&regex);
1793 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1794 f42b1b34 2018-03-12 stsp return err;
1795 f42b1b34 2018-03-12 stsp }
1796 5c860e29 2018-03-12 stsp
1797 4ed7e80c 2018-05-20 stsp __dead static void
1798 6f3d1eb0 2018-03-12 stsp usage_log(void)
1799 6f3d1eb0 2018-03-12 stsp {
1800 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1801 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1802 6f3d1eb0 2018-03-12 stsp exit(1);
1803 b1ebc001 2019-08-13 stsp }
1804 b1ebc001 2019-08-13 stsp
1805 b1ebc001 2019-08-13 stsp static int
1806 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1807 b1ebc001 2019-08-13 stsp {
1808 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1809 b1ebc001 2019-08-13 stsp long long n;
1810 b1ebc001 2019-08-13 stsp const char *errstr;
1811 b1ebc001 2019-08-13 stsp
1812 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1813 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1814 b1ebc001 2019-08-13 stsp return 0;
1815 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1816 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1817 b1ebc001 2019-08-13 stsp return 0;
1818 b1ebc001 2019-08-13 stsp return n;
1819 6f3d1eb0 2018-03-12 stsp }
1820 6f3d1eb0 2018-03-12 stsp
1821 4ed7e80c 2018-05-20 stsp static const struct got_error *
1822 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1823 f42b1b34 2018-03-12 stsp {
1824 f42b1b34 2018-03-12 stsp const struct got_error *error;
1825 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1826 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1827 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1828 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1829 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1830 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
1831 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
1832 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
1833 64a96a6d 2018-04-01 stsp const char *errstr;
1834 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1835 1b3893a2 2019-03-18 stsp
1836 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1837 5c860e29 2018-03-12 stsp
1838 6715a751 2018-03-16 stsp #ifndef PROFILE
1839 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1840 6098196c 2019-01-04 stsp NULL)
1841 ad242220 2018-09-08 stsp == -1)
1842 f42b1b34 2018-03-12 stsp err(1, "pledge");
1843 6715a751 2018-03-16 stsp #endif
1844 79109fed 2018-03-27 stsp
1845 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1846 b1ebc001 2019-08-13 stsp
1847 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
1848 79109fed 2018-03-27 stsp switch (ch) {
1849 79109fed 2018-03-27 stsp case 'p':
1850 79109fed 2018-03-27 stsp show_patch = 1;
1851 d142fc45 2018-04-01 stsp break;
1852 d142fc45 2018-04-01 stsp case 'c':
1853 d142fc45 2018-04-01 stsp start_commit = optarg;
1854 64a96a6d 2018-04-01 stsp break;
1855 c0cc5c62 2018-10-18 stsp case 'C':
1856 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1857 4a8520aa 2018-10-18 stsp &errstr);
1858 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1859 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1860 c0cc5c62 2018-10-18 stsp break;
1861 64a96a6d 2018-04-01 stsp case 'l':
1862 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1863 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1864 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1865 cc54c501 2019-07-15 stsp break;
1866 48c8c60d 2020-01-27 stsp case 'b':
1867 48c8c60d 2020-01-27 stsp log_branches = 1;
1868 a0603db2 2018-06-10 stsp break;
1869 04ca23f4 2018-07-16 stsp case 'r':
1870 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1871 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1872 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1873 9ba1d308 2019-10-21 stsp optarg);
1874 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1875 04ca23f4 2018-07-16 stsp break;
1876 6841bf13 2019-11-29 kn case 's':
1877 6841bf13 2019-11-29 kn search_pattern = optarg;
1878 6841bf13 2019-11-29 kn break;
1879 79109fed 2018-03-27 stsp default:
1880 2deda0b9 2019-03-07 stsp usage_log();
1881 79109fed 2018-03-27 stsp /* NOTREACHED */
1882 79109fed 2018-03-27 stsp }
1883 79109fed 2018-03-27 stsp }
1884 79109fed 2018-03-27 stsp
1885 79109fed 2018-03-27 stsp argc -= optind;
1886 79109fed 2018-03-27 stsp argv += optind;
1887 f42b1b34 2018-03-12 stsp
1888 dc1edbfa 2019-11-29 kn if (diff_context == -1)
1889 dc1edbfa 2019-11-29 kn diff_context = 3;
1890 dc1edbfa 2019-11-29 kn else if (!show_patch)
1891 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
1892 dc1edbfa 2019-11-29 kn
1893 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1894 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1895 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1896 04ca23f4 2018-07-16 stsp goto done;
1897 04ca23f4 2018-07-16 stsp }
1898 cffc0aa4 2019-02-05 stsp
1899 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1900 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1901 cffc0aa4 2019-02-05 stsp goto done;
1902 cffc0aa4 2019-02-05 stsp error = NULL;
1903 cffc0aa4 2019-02-05 stsp
1904 e7301579 2019-03-18 stsp if (argc == 0) {
1905 cbd1af7a 2019-03-18 stsp path = strdup("");
1906 e7301579 2019-03-18 stsp if (path == NULL) {
1907 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1908 cbd1af7a 2019-03-18 stsp goto done;
1909 e7301579 2019-03-18 stsp }
1910 e7301579 2019-03-18 stsp } else if (argc == 1) {
1911 e7301579 2019-03-18 stsp if (worktree) {
1912 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1913 e7301579 2019-03-18 stsp argv[0]);
1914 e7301579 2019-03-18 stsp if (error)
1915 e7301579 2019-03-18 stsp goto done;
1916 e7301579 2019-03-18 stsp } else {
1917 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1918 e7301579 2019-03-18 stsp if (path == NULL) {
1919 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1920 e7301579 2019-03-18 stsp goto done;
1921 e7301579 2019-03-18 stsp }
1922 e7301579 2019-03-18 stsp }
1923 cbd1af7a 2019-03-18 stsp } else
1924 cbd1af7a 2019-03-18 stsp usage_log();
1925 cbd1af7a 2019-03-18 stsp
1926 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1927 5486daa2 2019-05-11 stsp repo_path = worktree ?
1928 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1929 5486daa2 2019-05-11 stsp }
1930 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1931 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1932 cffc0aa4 2019-02-05 stsp goto done;
1933 04ca23f4 2018-07-16 stsp }
1934 6098196c 2019-01-04 stsp
1935 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1936 d7d4f210 2018-03-12 stsp if (error != NULL)
1937 04ca23f4 2018-07-16 stsp goto done;
1938 f42b1b34 2018-03-12 stsp
1939 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1940 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1941 c02c541e 2019-03-29 stsp if (error)
1942 c02c541e 2019-03-29 stsp goto done;
1943 c02c541e 2019-03-29 stsp
1944 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1945 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1946 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1947 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1948 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1949 3235492e 2018-04-01 stsp if (error != NULL)
1950 3235492e 2018-04-01 stsp return error;
1951 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1952 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1953 3235492e 2018-04-01 stsp if (error != NULL)
1954 3235492e 2018-04-01 stsp return error;
1955 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1956 3235492e 2018-04-01 stsp } else {
1957 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1958 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1959 3235492e 2018-04-01 stsp if (error == NULL) {
1960 1caad61b 2019-02-01 stsp int obj_type;
1961 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1962 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1963 d1f2edc9 2018-06-13 stsp if (error != NULL)
1964 1caad61b 2019-02-01 stsp goto done;
1965 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1966 1caad61b 2019-02-01 stsp if (error != NULL)
1967 1caad61b 2019-02-01 stsp goto done;
1968 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1969 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1970 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1971 1caad61b 2019-02-01 stsp if (error != NULL)
1972 1caad61b 2019-02-01 stsp goto done;
1973 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1974 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1975 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1976 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1977 1caad61b 2019-02-01 stsp goto done;
1978 1caad61b 2019-02-01 stsp }
1979 1caad61b 2019-02-01 stsp free(id);
1980 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1981 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1982 1caad61b 2019-02-01 stsp if (id == NULL)
1983 638f9024 2019-05-13 stsp error = got_error_from_errno(
1984 230a42bd 2019-05-11 jcs "got_object_id_dup");
1985 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1986 1caad61b 2019-02-01 stsp if (error)
1987 1caad61b 2019-02-01 stsp goto done;
1988 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1989 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1990 1caad61b 2019-02-01 stsp goto done;
1991 1caad61b 2019-02-01 stsp }
1992 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1993 d1f2edc9 2018-06-13 stsp if (error != NULL)
1994 1caad61b 2019-02-01 stsp goto done;
1995 d1f2edc9 2018-06-13 stsp }
1996 15a94983 2018-12-23 stsp if (commit == NULL) {
1997 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1998 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1999 d1f2edc9 2018-06-13 stsp if (error != NULL)
2000 d1f2edc9 2018-06-13 stsp return error;
2001 3235492e 2018-04-01 stsp }
2002 3235492e 2018-04-01 stsp }
2003 d7d4f210 2018-03-12 stsp if (error != NULL)
2004 04ca23f4 2018-07-16 stsp goto done;
2005 04ca23f4 2018-07-16 stsp
2006 deeabeae 2019-08-27 stsp if (worktree) {
2007 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2008 deeabeae 2019-08-27 stsp char *p;
2009 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2010 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2011 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2012 deeabeae 2019-08-27 stsp goto done;
2013 deeabeae 2019-08-27 stsp }
2014 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2015 deeabeae 2019-08-27 stsp free(p);
2016 deeabeae 2019-08-27 stsp } else
2017 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2018 04ca23f4 2018-07-16 stsp if (error != NULL)
2019 04ca23f4 2018-07-16 stsp goto done;
2020 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2021 04ca23f4 2018-07-16 stsp free(path);
2022 04ca23f4 2018-07-16 stsp path = in_repo_path;
2023 04ca23f4 2018-07-16 stsp }
2024 199a4027 2019-02-02 stsp
2025 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2026 199a4027 2019-02-02 stsp if (error)
2027 199a4027 2019-02-02 stsp goto done;
2028 04ca23f4 2018-07-16 stsp
2029 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2030 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2031 04ca23f4 2018-07-16 stsp done:
2032 04ca23f4 2018-07-16 stsp free(path);
2033 04ca23f4 2018-07-16 stsp free(repo_path);
2034 04ca23f4 2018-07-16 stsp free(cwd);
2035 f42b1b34 2018-03-12 stsp free(id);
2036 cffc0aa4 2019-02-05 stsp if (worktree)
2037 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2038 ad242220 2018-09-08 stsp if (repo) {
2039 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2040 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2041 ad242220 2018-09-08 stsp if (error == NULL)
2042 ad242220 2018-09-08 stsp error = repo_error;
2043 ad242220 2018-09-08 stsp }
2044 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2045 8bf5b3c9 2018-03-17 stsp return error;
2046 5c860e29 2018-03-12 stsp }
2047 5c860e29 2018-03-12 stsp
2048 4ed7e80c 2018-05-20 stsp __dead static void
2049 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2050 3f8b7d6a 2018-04-01 stsp {
2051 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2052 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2053 3f8b7d6a 2018-04-01 stsp exit(1);
2054 b00d56cd 2018-04-01 stsp }
2055 b00d56cd 2018-04-01 stsp
2056 b72f483a 2019-02-05 stsp struct print_diff_arg {
2057 b72f483a 2019-02-05 stsp struct got_repository *repo;
2058 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2059 b72f483a 2019-02-05 stsp int diff_context;
2060 3fc0c068 2019-02-10 stsp const char *id_str;
2061 3fc0c068 2019-02-10 stsp int header_shown;
2062 98eaaa12 2019-08-03 stsp int diff_staged;
2063 63035f9f 2019-10-06 stsp int ignore_whitespace;
2064 b72f483a 2019-02-05 stsp };
2065 b72f483a 2019-02-05 stsp
2066 4ed7e80c 2018-05-20 stsp static const struct got_error *
2067 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2068 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2069 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2070 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2071 b72f483a 2019-02-05 stsp {
2072 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2073 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2074 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2075 12463d8b 2019-12-13 stsp int fd = -1;
2076 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2077 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2078 b72f483a 2019-02-05 stsp struct stat sb;
2079 b72f483a 2019-02-05 stsp
2080 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2081 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2082 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2083 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2084 98eaaa12 2019-08-03 stsp return NULL;
2085 98eaaa12 2019-08-03 stsp } else {
2086 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2087 98eaaa12 2019-08-03 stsp return NULL;
2088 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2089 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2090 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2091 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2092 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2093 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2094 98eaaa12 2019-08-03 stsp return NULL;
2095 98eaaa12 2019-08-03 stsp }
2096 3fc0c068 2019-02-10 stsp
2097 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2098 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2099 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2100 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2101 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2102 3fc0c068 2019-02-10 stsp }
2103 b72f483a 2019-02-05 stsp
2104 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2105 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2106 98eaaa12 2019-08-03 stsp switch (staged_status) {
2107 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2108 98eaaa12 2019-08-03 stsp label1 = path;
2109 98eaaa12 2019-08-03 stsp label2 = path;
2110 98eaaa12 2019-08-03 stsp break;
2111 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2112 98eaaa12 2019-08-03 stsp label2 = path;
2113 98eaaa12 2019-08-03 stsp break;
2114 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2115 98eaaa12 2019-08-03 stsp label1 = path;
2116 98eaaa12 2019-08-03 stsp break;
2117 98eaaa12 2019-08-03 stsp default:
2118 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2119 98eaaa12 2019-08-03 stsp }
2120 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2121 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2122 63035f9f 2019-10-06 stsp a->repo, stdout);
2123 98eaaa12 2019-08-03 stsp }
2124 98eaaa12 2019-08-03 stsp
2125 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2126 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2127 4ce46740 2019-08-08 stsp char *id_str;
2128 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2129 408b4ebc 2019-08-03 stsp 8192);
2130 4ce46740 2019-08-08 stsp if (err)
2131 4ce46740 2019-08-08 stsp goto done;
2132 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2133 4ce46740 2019-08-08 stsp if (err)
2134 4ce46740 2019-08-08 stsp goto done;
2135 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2136 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2137 4ce46740 2019-08-08 stsp free(id_str);
2138 4ce46740 2019-08-08 stsp goto done;
2139 4ce46740 2019-08-08 stsp }
2140 4ce46740 2019-08-08 stsp free(id_str);
2141 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2142 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2143 4ce46740 2019-08-08 stsp if (err)
2144 4ce46740 2019-08-08 stsp goto done;
2145 4ce46740 2019-08-08 stsp }
2146 d00136be 2019-03-26 stsp
2147 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2148 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2149 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2150 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2151 2ec1f75b 2019-03-26 stsp goto done;
2152 2ec1f75b 2019-03-26 stsp }
2153 b72f483a 2019-02-05 stsp
2154 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2155 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2156 12463d8b 2019-12-13 stsp if (fd == -1) {
2157 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2158 12463d8b 2019-12-13 stsp goto done;
2159 12463d8b 2019-12-13 stsp }
2160 12463d8b 2019-12-13 stsp } else {
2161 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2162 12463d8b 2019-12-13 stsp if (fd == -1) {
2163 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2164 12463d8b 2019-12-13 stsp goto done;
2165 12463d8b 2019-12-13 stsp }
2166 12463d8b 2019-12-13 stsp }
2167 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2168 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2169 2ec1f75b 2019-03-26 stsp goto done;
2170 2ec1f75b 2019-03-26 stsp }
2171 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2172 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2173 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2174 2ec1f75b 2019-03-26 stsp goto done;
2175 2ec1f75b 2019-03-26 stsp }
2176 12463d8b 2019-12-13 stsp fd = -1;
2177 2ec1f75b 2019-03-26 stsp } else
2178 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2179 b72f483a 2019-02-05 stsp
2180 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2181 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2182 b72f483a 2019-02-05 stsp done:
2183 b72f483a 2019-02-05 stsp if (blob1)
2184 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2185 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2186 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2187 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2188 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2189 b72f483a 2019-02-05 stsp free(abspath);
2190 927df6b7 2019-02-10 stsp return err;
2191 927df6b7 2019-02-10 stsp }
2192 927df6b7 2019-02-10 stsp
2193 927df6b7 2019-02-10 stsp static const struct got_error *
2194 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2195 b00d56cd 2018-04-01 stsp {
2196 b00d56cd 2018-04-01 stsp const struct got_error *error;
2197 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2198 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2199 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2200 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2201 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2202 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2203 15a94983 2018-12-23 stsp int type1, type2;
2204 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2205 c0cc5c62 2018-10-18 stsp const char *errstr;
2206 927df6b7 2019-02-10 stsp char *path = NULL;
2207 b00d56cd 2018-04-01 stsp
2208 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2209 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2210 25eccc22 2019-01-04 stsp NULL) == -1)
2211 b00d56cd 2018-04-01 stsp err(1, "pledge");
2212 b00d56cd 2018-04-01 stsp #endif
2213 b00d56cd 2018-04-01 stsp
2214 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2215 b00d56cd 2018-04-01 stsp switch (ch) {
2216 c0cc5c62 2018-10-18 stsp case 'C':
2217 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2218 dfcab68b 2019-11-29 kn &errstr);
2219 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2220 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2221 c0cc5c62 2018-10-18 stsp break;
2222 b72f483a 2019-02-05 stsp case 'r':
2223 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2224 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2225 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2226 9ba1d308 2019-10-21 stsp optarg);
2227 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2228 b72f483a 2019-02-05 stsp break;
2229 98eaaa12 2019-08-03 stsp case 's':
2230 98eaaa12 2019-08-03 stsp diff_staged = 1;
2231 63035f9f 2019-10-06 stsp break;
2232 63035f9f 2019-10-06 stsp case 'w':
2233 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2234 98eaaa12 2019-08-03 stsp break;
2235 b00d56cd 2018-04-01 stsp default:
2236 2deda0b9 2019-03-07 stsp usage_diff();
2237 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2238 b00d56cd 2018-04-01 stsp }
2239 b00d56cd 2018-04-01 stsp }
2240 b00d56cd 2018-04-01 stsp
2241 b00d56cd 2018-04-01 stsp argc -= optind;
2242 b00d56cd 2018-04-01 stsp argv += optind;
2243 b00d56cd 2018-04-01 stsp
2244 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2245 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2246 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2247 b72f483a 2019-02-05 stsp goto done;
2248 b72f483a 2019-02-05 stsp }
2249 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2250 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2251 927df6b7 2019-02-10 stsp goto done;
2252 927df6b7 2019-02-10 stsp if (argc <= 1) {
2253 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2254 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2255 927df6b7 2019-02-10 stsp goto done;
2256 927df6b7 2019-02-10 stsp }
2257 b72f483a 2019-02-05 stsp if (repo_path)
2258 b72f483a 2019-02-05 stsp errx(1,
2259 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2260 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2261 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2262 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2263 927df6b7 2019-02-10 stsp goto done;
2264 927df6b7 2019-02-10 stsp }
2265 927df6b7 2019-02-10 stsp if (argc == 1) {
2266 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2267 cbd1af7a 2019-03-18 stsp argv[0]);
2268 927df6b7 2019-02-10 stsp if (error)
2269 927df6b7 2019-02-10 stsp goto done;
2270 927df6b7 2019-02-10 stsp } else {
2271 927df6b7 2019-02-10 stsp path = strdup("");
2272 927df6b7 2019-02-10 stsp if (path == NULL) {
2273 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2274 927df6b7 2019-02-10 stsp goto done;
2275 927df6b7 2019-02-10 stsp }
2276 927df6b7 2019-02-10 stsp }
2277 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2278 98eaaa12 2019-08-03 stsp if (diff_staged)
2279 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2280 98eaaa12 2019-08-03 stsp "objects in repository");
2281 15a94983 2018-12-23 stsp id_str1 = argv[0];
2282 15a94983 2018-12-23 stsp id_str2 = argv[1];
2283 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2284 30db809c 2019-06-05 stsp repo_path =
2285 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2286 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2287 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2288 30db809c 2019-06-05 stsp goto done;
2289 30db809c 2019-06-05 stsp }
2290 30db809c 2019-06-05 stsp }
2291 b00d56cd 2018-04-01 stsp } else
2292 b00d56cd 2018-04-01 stsp usage_diff();
2293 25eccc22 2019-01-04 stsp
2294 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2295 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2296 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2297 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2298 b72f483a 2019-02-05 stsp }
2299 b00d56cd 2018-04-01 stsp
2300 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2301 76089277 2018-04-01 stsp free(repo_path);
2302 b00d56cd 2018-04-01 stsp if (error != NULL)
2303 b00d56cd 2018-04-01 stsp goto done;
2304 b00d56cd 2018-04-01 stsp
2305 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2306 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2307 c02c541e 2019-03-29 stsp if (error)
2308 c02c541e 2019-03-29 stsp goto done;
2309 c02c541e 2019-03-29 stsp
2310 30db809c 2019-06-05 stsp if (argc <= 1) {
2311 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2312 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2313 b72f483a 2019-02-05 stsp char *id_str;
2314 72ea6654 2019-07-27 stsp
2315 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2316 72ea6654 2019-07-27 stsp
2317 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2318 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2319 b72f483a 2019-02-05 stsp if (error)
2320 b72f483a 2019-02-05 stsp goto done;
2321 b72f483a 2019-02-05 stsp arg.repo = repo;
2322 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2323 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2324 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2325 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2326 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2327 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2328 b72f483a 2019-02-05 stsp
2329 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2330 72ea6654 2019-07-27 stsp if (error)
2331 72ea6654 2019-07-27 stsp goto done;
2332 72ea6654 2019-07-27 stsp
2333 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2334 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2335 b72f483a 2019-02-05 stsp free(id_str);
2336 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2337 b72f483a 2019-02-05 stsp goto done;
2338 b72f483a 2019-02-05 stsp }
2339 b72f483a 2019-02-05 stsp
2340 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2341 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2342 0adb7196 2019-08-11 stsp if (error)
2343 0adb7196 2019-08-11 stsp goto done;
2344 b00d56cd 2018-04-01 stsp
2345 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2346 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2347 0adb7196 2019-08-11 stsp if (error)
2348 0adb7196 2019-08-11 stsp goto done;
2349 b00d56cd 2018-04-01 stsp
2350 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2351 15a94983 2018-12-23 stsp if (error)
2352 15a94983 2018-12-23 stsp goto done;
2353 15a94983 2018-12-23 stsp
2354 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2355 15a94983 2018-12-23 stsp if (error)
2356 15a94983 2018-12-23 stsp goto done;
2357 15a94983 2018-12-23 stsp
2358 15a94983 2018-12-23 stsp if (type1 != type2) {
2359 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2360 b00d56cd 2018-04-01 stsp goto done;
2361 b00d56cd 2018-04-01 stsp }
2362 b00d56cd 2018-04-01 stsp
2363 15a94983 2018-12-23 stsp switch (type1) {
2364 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2365 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2366 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2367 b00d56cd 2018-04-01 stsp break;
2368 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2369 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2370 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2371 b00d56cd 2018-04-01 stsp break;
2372 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2373 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2374 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2375 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2376 b00d56cd 2018-04-01 stsp break;
2377 b00d56cd 2018-04-01 stsp default:
2378 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2379 b00d56cd 2018-04-01 stsp }
2380 b00d56cd 2018-04-01 stsp done:
2381 5e70831e 2019-05-28 stsp free(label1);
2382 5e70831e 2019-05-28 stsp free(label2);
2383 15a94983 2018-12-23 stsp free(id1);
2384 15a94983 2018-12-23 stsp free(id2);
2385 927df6b7 2019-02-10 stsp free(path);
2386 b72f483a 2019-02-05 stsp if (worktree)
2387 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2388 ad242220 2018-09-08 stsp if (repo) {
2389 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2390 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2391 ad242220 2018-09-08 stsp if (error == NULL)
2392 ad242220 2018-09-08 stsp error = repo_error;
2393 ad242220 2018-09-08 stsp }
2394 b00d56cd 2018-04-01 stsp return error;
2395 404c43c4 2018-06-21 stsp }
2396 404c43c4 2018-06-21 stsp
2397 404c43c4 2018-06-21 stsp __dead static void
2398 404c43c4 2018-06-21 stsp usage_blame(void)
2399 404c43c4 2018-06-21 stsp {
2400 9270e621 2019-02-05 stsp fprintf(stderr,
2401 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2402 404c43c4 2018-06-21 stsp getprogname());
2403 404c43c4 2018-06-21 stsp exit(1);
2404 b00d56cd 2018-04-01 stsp }
2405 b00d56cd 2018-04-01 stsp
2406 28315671 2019-08-14 stsp struct blame_line {
2407 28315671 2019-08-14 stsp int annotated;
2408 28315671 2019-08-14 stsp char *id_str;
2409 82f6abb8 2019-08-14 stsp char *committer;
2410 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2411 28315671 2019-08-14 stsp };
2412 28315671 2019-08-14 stsp
2413 28315671 2019-08-14 stsp struct blame_cb_args {
2414 28315671 2019-08-14 stsp struct blame_line *lines;
2415 28315671 2019-08-14 stsp int nlines;
2416 7ef28ff8 2019-08-14 stsp int nlines_prec;
2417 28315671 2019-08-14 stsp int lineno_cur;
2418 28315671 2019-08-14 stsp off_t *line_offsets;
2419 28315671 2019-08-14 stsp FILE *f;
2420 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2421 28315671 2019-08-14 stsp };
2422 28315671 2019-08-14 stsp
2423 404c43c4 2018-06-21 stsp static const struct got_error *
2424 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2425 28315671 2019-08-14 stsp {
2426 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2427 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2428 28315671 2019-08-14 stsp struct blame_line *bline;
2429 82f6abb8 2019-08-14 stsp char *line = NULL;
2430 28315671 2019-08-14 stsp size_t linesize = 0;
2431 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2432 28315671 2019-08-14 stsp off_t offset;
2433 bcb49d15 2019-08-14 stsp struct tm tm;
2434 bcb49d15 2019-08-14 stsp time_t committer_time;
2435 28315671 2019-08-14 stsp
2436 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2437 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2438 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2439 28315671 2019-08-14 stsp
2440 28315671 2019-08-14 stsp if (sigint_received)
2441 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2442 28315671 2019-08-14 stsp
2443 2839f8b9 2019-08-18 stsp if (lineno == -1)
2444 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2445 2839f8b9 2019-08-18 stsp
2446 28315671 2019-08-14 stsp /* Annotate this line. */
2447 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2448 28315671 2019-08-14 stsp if (bline->annotated)
2449 28315671 2019-08-14 stsp return NULL;
2450 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2451 28315671 2019-08-14 stsp if (err)
2452 28315671 2019-08-14 stsp return err;
2453 82f6abb8 2019-08-14 stsp
2454 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2455 82f6abb8 2019-08-14 stsp if (err)
2456 82f6abb8 2019-08-14 stsp goto done;
2457 82f6abb8 2019-08-14 stsp
2458 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2459 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2460 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2461 bcb49d15 2019-08-14 stsp goto done;
2462 bcb49d15 2019-08-14 stsp }
2463 bcb49d15 2019-08-14 stsp
2464 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2465 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2466 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2467 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2468 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2469 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2470 82f6abb8 2019-08-14 stsp goto done;
2471 82f6abb8 2019-08-14 stsp }
2472 28315671 2019-08-14 stsp bline->annotated = 1;
2473 28315671 2019-08-14 stsp
2474 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2475 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2476 28315671 2019-08-14 stsp if (!bline->annotated)
2477 82f6abb8 2019-08-14 stsp goto done;
2478 28315671 2019-08-14 stsp
2479 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2480 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2481 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2482 82f6abb8 2019-08-14 stsp goto done;
2483 82f6abb8 2019-08-14 stsp }
2484 28315671 2019-08-14 stsp
2485 28315671 2019-08-14 stsp while (bline->annotated) {
2486 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2487 82f6abb8 2019-08-14 stsp size_t len;
2488 82f6abb8 2019-08-14 stsp
2489 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2490 28315671 2019-08-14 stsp if (ferror(a->f))
2491 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2492 28315671 2019-08-14 stsp break;
2493 28315671 2019-08-14 stsp }
2494 82f6abb8 2019-08-14 stsp
2495 82f6abb8 2019-08-14 stsp committer = bline->committer;
2496 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2497 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2498 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2499 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2500 82f6abb8 2019-08-14 stsp if (at)
2501 82f6abb8 2019-08-14 stsp *at = '\0';
2502 82f6abb8 2019-08-14 stsp len = strlen(committer);
2503 82f6abb8 2019-08-14 stsp if (len >= 9)
2504 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2505 28315671 2019-08-14 stsp
2506 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2507 28315671 2019-08-14 stsp if (nl)
2508 28315671 2019-08-14 stsp *nl = '\0';
2509 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2510 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2511 28315671 2019-08-14 stsp
2512 28315671 2019-08-14 stsp a->lineno_cur++;
2513 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2514 28315671 2019-08-14 stsp }
2515 82f6abb8 2019-08-14 stsp done:
2516 82f6abb8 2019-08-14 stsp if (commit)
2517 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2518 28315671 2019-08-14 stsp free(line);
2519 28315671 2019-08-14 stsp return err;
2520 28315671 2019-08-14 stsp }
2521 28315671 2019-08-14 stsp
2522 28315671 2019-08-14 stsp static const struct got_error *
2523 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2524 404c43c4 2018-06-21 stsp {
2525 404c43c4 2018-06-21 stsp const struct got_error *error;
2526 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2527 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2528 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2529 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2530 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2531 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2532 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2533 28315671 2019-08-14 stsp struct blame_cb_args bca;
2534 28315671 2019-08-14 stsp int ch, obj_type, i;
2535 b02560ec 2019-08-19 stsp size_t filesize;
2536 90f3c347 2019-08-19 stsp
2537 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2538 404c43c4 2018-06-21 stsp
2539 404c43c4 2018-06-21 stsp #ifndef PROFILE
2540 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2541 36e2fb66 2019-01-04 stsp NULL) == -1)
2542 404c43c4 2018-06-21 stsp err(1, "pledge");
2543 404c43c4 2018-06-21 stsp #endif
2544 404c43c4 2018-06-21 stsp
2545 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2546 404c43c4 2018-06-21 stsp switch (ch) {
2547 404c43c4 2018-06-21 stsp case 'c':
2548 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2549 404c43c4 2018-06-21 stsp break;
2550 66bea077 2018-08-02 stsp case 'r':
2551 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2552 66bea077 2018-08-02 stsp if (repo_path == NULL)
2553 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2554 9ba1d308 2019-10-21 stsp optarg);
2555 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2556 66bea077 2018-08-02 stsp break;
2557 404c43c4 2018-06-21 stsp default:
2558 2deda0b9 2019-03-07 stsp usage_blame();
2559 404c43c4 2018-06-21 stsp /* NOTREACHED */
2560 404c43c4 2018-06-21 stsp }
2561 404c43c4 2018-06-21 stsp }
2562 404c43c4 2018-06-21 stsp
2563 404c43c4 2018-06-21 stsp argc -= optind;
2564 404c43c4 2018-06-21 stsp argv += optind;
2565 404c43c4 2018-06-21 stsp
2566 a39318fd 2018-08-02 stsp if (argc == 1)
2567 404c43c4 2018-06-21 stsp path = argv[0];
2568 a39318fd 2018-08-02 stsp else
2569 404c43c4 2018-06-21 stsp usage_blame();
2570 404c43c4 2018-06-21 stsp
2571 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2572 66bea077 2018-08-02 stsp if (cwd == NULL) {
2573 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2574 66bea077 2018-08-02 stsp goto done;
2575 66bea077 2018-08-02 stsp }
2576 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2577 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2578 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2579 66bea077 2018-08-02 stsp goto done;
2580 0c06baac 2019-02-05 stsp else
2581 0c06baac 2019-02-05 stsp error = NULL;
2582 0c06baac 2019-02-05 stsp if (worktree) {
2583 0c06baac 2019-02-05 stsp repo_path =
2584 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2585 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2586 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2587 0c06baac 2019-02-05 stsp if (error)
2588 0c06baac 2019-02-05 stsp goto done;
2589 0c06baac 2019-02-05 stsp } else {
2590 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2591 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2592 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2593 0c06baac 2019-02-05 stsp goto done;
2594 0c06baac 2019-02-05 stsp }
2595 66bea077 2018-08-02 stsp }
2596 66bea077 2018-08-02 stsp }
2597 36e2fb66 2019-01-04 stsp
2598 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2599 c02c541e 2019-03-29 stsp if (error != NULL)
2600 36e2fb66 2019-01-04 stsp goto done;
2601 66bea077 2018-08-02 stsp
2602 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2603 c02c541e 2019-03-29 stsp if (error)
2604 404c43c4 2018-06-21 stsp goto done;
2605 404c43c4 2018-06-21 stsp
2606 0c06baac 2019-02-05 stsp if (worktree) {
2607 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2608 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2609 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2610 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2611 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2612 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2613 6efaaa2d 2019-02-05 stsp path) == -1) {
2614 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2615 0c06baac 2019-02-05 stsp goto done;
2616 0c06baac 2019-02-05 stsp }
2617 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2618 0c06baac 2019-02-05 stsp free(p);
2619 0c06baac 2019-02-05 stsp } else {
2620 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2621 0c06baac 2019-02-05 stsp }
2622 0c06baac 2019-02-05 stsp if (error)
2623 66bea077 2018-08-02 stsp goto done;
2624 66bea077 2018-08-02 stsp
2625 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2626 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2627 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
2628 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2629 404c43c4 2018-06-21 stsp if (error != NULL)
2630 66bea077 2018-08-02 stsp goto done;
2631 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2632 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2633 404c43c4 2018-06-21 stsp if (error != NULL)
2634 66bea077 2018-08-02 stsp goto done;
2635 404c43c4 2018-06-21 stsp } else {
2636 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2637 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2638 30837e32 2019-07-25 stsp if (error)
2639 66bea077 2018-08-02 stsp goto done;
2640 404c43c4 2018-06-21 stsp }
2641 404c43c4 2018-06-21 stsp
2642 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2643 28315671 2019-08-14 stsp if (error)
2644 28315671 2019-08-14 stsp goto done;
2645 28315671 2019-08-14 stsp
2646 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2647 28315671 2019-08-14 stsp if (error)
2648 28315671 2019-08-14 stsp goto done;
2649 28315671 2019-08-14 stsp
2650 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2651 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2652 28315671 2019-08-14 stsp goto done;
2653 28315671 2019-08-14 stsp }
2654 28315671 2019-08-14 stsp
2655 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2656 28315671 2019-08-14 stsp if (error)
2657 28315671 2019-08-14 stsp goto done;
2658 28315671 2019-08-14 stsp bca.f = got_opentemp();
2659 28315671 2019-08-14 stsp if (bca.f == NULL) {
2660 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2661 28315671 2019-08-14 stsp goto done;
2662 28315671 2019-08-14 stsp }
2663 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2664 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2665 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2666 28315671 2019-08-14 stsp goto done;
2667 28315671 2019-08-14 stsp
2668 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2669 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2670 b02560ec 2019-08-19 stsp bca.nlines--;
2671 b02560ec 2019-08-19 stsp
2672 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2673 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2674 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2675 28315671 2019-08-14 stsp goto done;
2676 28315671 2019-08-14 stsp }
2677 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2678 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2679 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2680 7ef28ff8 2019-08-14 stsp while (i > 0) {
2681 7ef28ff8 2019-08-14 stsp i /= 10;
2682 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2683 7ef28ff8 2019-08-14 stsp }
2684 82f6abb8 2019-08-14 stsp bca.repo = repo;
2685 7ef28ff8 2019-08-14 stsp
2686 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2687 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2688 404c43c4 2018-06-21 stsp done:
2689 66bea077 2018-08-02 stsp free(in_repo_path);
2690 66bea077 2018-08-02 stsp free(repo_path);
2691 66bea077 2018-08-02 stsp free(cwd);
2692 404c43c4 2018-06-21 stsp free(commit_id);
2693 28315671 2019-08-14 stsp free(obj_id);
2694 28315671 2019-08-14 stsp if (blob)
2695 28315671 2019-08-14 stsp got_object_blob_close(blob);
2696 0c06baac 2019-02-05 stsp if (worktree)
2697 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2698 ad242220 2018-09-08 stsp if (repo) {
2699 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2700 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2701 ad242220 2018-09-08 stsp if (error == NULL)
2702 ad242220 2018-09-08 stsp error = repo_error;
2703 ad242220 2018-09-08 stsp }
2704 3affba96 2019-09-22 stsp if (bca.lines) {
2705 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2706 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2707 3affba96 2019-09-22 stsp free(bline->id_str);
2708 3affba96 2019-09-22 stsp free(bline->committer);
2709 3affba96 2019-09-22 stsp }
2710 3affba96 2019-09-22 stsp free(bca.lines);
2711 28315671 2019-08-14 stsp }
2712 28315671 2019-08-14 stsp free(bca.line_offsets);
2713 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2714 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2715 404c43c4 2018-06-21 stsp return error;
2716 5de5890b 2018-10-18 stsp }
2717 5de5890b 2018-10-18 stsp
2718 5de5890b 2018-10-18 stsp __dead static void
2719 5de5890b 2018-10-18 stsp usage_tree(void)
2720 5de5890b 2018-10-18 stsp {
2721 c1669e2e 2019-01-09 stsp fprintf(stderr,
2722 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2723 5de5890b 2018-10-18 stsp getprogname());
2724 5de5890b 2018-10-18 stsp exit(1);
2725 5de5890b 2018-10-18 stsp }
2726 5de5890b 2018-10-18 stsp
2727 c1669e2e 2019-01-09 stsp static void
2728 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2729 c1669e2e 2019-01-09 stsp const char *root_path)
2730 c1669e2e 2019-01-09 stsp {
2731 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2732 848d6979 2019-08-12 stsp const char *modestr = "";
2733 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2734 5de5890b 2018-10-18 stsp
2735 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2736 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2737 c1669e2e 2019-01-09 stsp path++;
2738 c1669e2e 2019-01-09 stsp
2739 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2740 63c5ca5d 2019-08-24 stsp modestr = "$";
2741 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2742 848d6979 2019-08-12 stsp modestr = "@";
2743 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2744 848d6979 2019-08-12 stsp modestr = "/";
2745 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2746 848d6979 2019-08-12 stsp modestr = "*";
2747 848d6979 2019-08-12 stsp
2748 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2749 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2750 c1669e2e 2019-01-09 stsp }
2751 c1669e2e 2019-01-09 stsp
2752 5de5890b 2018-10-18 stsp static const struct got_error *
2753 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2754 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2755 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2756 5de5890b 2018-10-18 stsp {
2757 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2758 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2759 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2760 56e0773d 2019-11-28 stsp int nentries, i;
2761 5de5890b 2018-10-18 stsp
2762 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2763 5de5890b 2018-10-18 stsp if (err)
2764 5de5890b 2018-10-18 stsp goto done;
2765 5de5890b 2018-10-18 stsp
2766 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2767 5de5890b 2018-10-18 stsp if (err)
2768 5de5890b 2018-10-18 stsp goto done;
2769 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2770 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2771 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2772 5de5890b 2018-10-18 stsp char *id = NULL;
2773 84453469 2018-11-11 stsp
2774 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2775 84453469 2018-11-11 stsp break;
2776 84453469 2018-11-11 stsp
2777 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2778 5de5890b 2018-10-18 stsp if (show_ids) {
2779 5de5890b 2018-10-18 stsp char *id_str;
2780 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2781 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2782 5de5890b 2018-10-18 stsp if (err)
2783 5de5890b 2018-10-18 stsp goto done;
2784 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2785 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2786 5de5890b 2018-10-18 stsp free(id_str);
2787 5de5890b 2018-10-18 stsp goto done;
2788 5de5890b 2018-10-18 stsp }
2789 5de5890b 2018-10-18 stsp free(id_str);
2790 5de5890b 2018-10-18 stsp }
2791 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2792 5de5890b 2018-10-18 stsp free(id);
2793 c1669e2e 2019-01-09 stsp
2794 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2795 c1669e2e 2019-01-09 stsp char *child_path;
2796 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2797 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2798 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
2799 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2800 c1669e2e 2019-01-09 stsp goto done;
2801 c1669e2e 2019-01-09 stsp }
2802 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2803 c1669e2e 2019-01-09 stsp root_path, repo);
2804 c1669e2e 2019-01-09 stsp free(child_path);
2805 c1669e2e 2019-01-09 stsp if (err)
2806 c1669e2e 2019-01-09 stsp goto done;
2807 c1669e2e 2019-01-09 stsp }
2808 5de5890b 2018-10-18 stsp }
2809 5de5890b 2018-10-18 stsp done:
2810 5de5890b 2018-10-18 stsp if (tree)
2811 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2812 5de5890b 2018-10-18 stsp free(tree_id);
2813 5de5890b 2018-10-18 stsp return err;
2814 404c43c4 2018-06-21 stsp }
2815 404c43c4 2018-06-21 stsp
2816 5de5890b 2018-10-18 stsp static const struct got_error *
2817 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2818 5de5890b 2018-10-18 stsp {
2819 5de5890b 2018-10-18 stsp const struct got_error *error;
2820 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2821 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2822 7a2c19d6 2019-02-05 stsp const char *path;
2823 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2824 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2825 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2826 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2827 5de5890b 2018-10-18 stsp int ch;
2828 5de5890b 2018-10-18 stsp
2829 5de5890b 2018-10-18 stsp #ifndef PROFILE
2830 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2831 0f8d269b 2019-01-04 stsp NULL) == -1)
2832 5de5890b 2018-10-18 stsp err(1, "pledge");
2833 5de5890b 2018-10-18 stsp #endif
2834 5de5890b 2018-10-18 stsp
2835 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2836 5de5890b 2018-10-18 stsp switch (ch) {
2837 5de5890b 2018-10-18 stsp case 'c':
2838 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2839 5de5890b 2018-10-18 stsp break;
2840 5de5890b 2018-10-18 stsp case 'r':
2841 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2842 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2843 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2844 9ba1d308 2019-10-21 stsp optarg);
2845 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2846 5de5890b 2018-10-18 stsp break;
2847 5de5890b 2018-10-18 stsp case 'i':
2848 5de5890b 2018-10-18 stsp show_ids = 1;
2849 5de5890b 2018-10-18 stsp break;
2850 c1669e2e 2019-01-09 stsp case 'R':
2851 c1669e2e 2019-01-09 stsp recurse = 1;
2852 c1669e2e 2019-01-09 stsp break;
2853 5de5890b 2018-10-18 stsp default:
2854 2deda0b9 2019-03-07 stsp usage_tree();
2855 5de5890b 2018-10-18 stsp /* NOTREACHED */
2856 5de5890b 2018-10-18 stsp }
2857 5de5890b 2018-10-18 stsp }
2858 5de5890b 2018-10-18 stsp
2859 5de5890b 2018-10-18 stsp argc -= optind;
2860 5de5890b 2018-10-18 stsp argv += optind;
2861 5de5890b 2018-10-18 stsp
2862 5de5890b 2018-10-18 stsp if (argc == 1)
2863 5de5890b 2018-10-18 stsp path = argv[0];
2864 5de5890b 2018-10-18 stsp else if (argc > 1)
2865 5de5890b 2018-10-18 stsp usage_tree();
2866 5de5890b 2018-10-18 stsp else
2867 7a2c19d6 2019-02-05 stsp path = NULL;
2868 7a2c19d6 2019-02-05 stsp
2869 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2870 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2871 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2872 9bf7a39b 2019-02-05 stsp goto done;
2873 9bf7a39b 2019-02-05 stsp }
2874 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2875 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2876 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2877 7a2c19d6 2019-02-05 stsp goto done;
2878 7a2c19d6 2019-02-05 stsp else
2879 7a2c19d6 2019-02-05 stsp error = NULL;
2880 7a2c19d6 2019-02-05 stsp if (worktree) {
2881 7a2c19d6 2019-02-05 stsp repo_path =
2882 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2883 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2884 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2885 7a2c19d6 2019-02-05 stsp if (error)
2886 7a2c19d6 2019-02-05 stsp goto done;
2887 7a2c19d6 2019-02-05 stsp } else {
2888 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2889 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2890 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2891 7a2c19d6 2019-02-05 stsp goto done;
2892 7a2c19d6 2019-02-05 stsp }
2893 5de5890b 2018-10-18 stsp }
2894 5de5890b 2018-10-18 stsp }
2895 5de5890b 2018-10-18 stsp
2896 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2897 5de5890b 2018-10-18 stsp if (error != NULL)
2898 c02c541e 2019-03-29 stsp goto done;
2899 c02c541e 2019-03-29 stsp
2900 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2901 c02c541e 2019-03-29 stsp if (error)
2902 5de5890b 2018-10-18 stsp goto done;
2903 5de5890b 2018-10-18 stsp
2904 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2905 9bf7a39b 2019-02-05 stsp if (worktree) {
2906 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2907 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2908 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2909 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2910 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2911 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2912 9bf7a39b 2019-02-05 stsp goto done;
2913 9bf7a39b 2019-02-05 stsp }
2914 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2915 9bf7a39b 2019-02-05 stsp free(p);
2916 9bf7a39b 2019-02-05 stsp if (error)
2917 9bf7a39b 2019-02-05 stsp goto done;
2918 9bf7a39b 2019-02-05 stsp } else
2919 9bf7a39b 2019-02-05 stsp path = "/";
2920 9bf7a39b 2019-02-05 stsp }
2921 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2922 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2923 9bf7a39b 2019-02-05 stsp if (error != NULL)
2924 9bf7a39b 2019-02-05 stsp goto done;
2925 9bf7a39b 2019-02-05 stsp }
2926 5de5890b 2018-10-18 stsp
2927 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2928 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2929 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2930 5de5890b 2018-10-18 stsp if (error != NULL)
2931 5de5890b 2018-10-18 stsp goto done;
2932 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2933 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2934 5de5890b 2018-10-18 stsp if (error != NULL)
2935 5de5890b 2018-10-18 stsp goto done;
2936 5de5890b 2018-10-18 stsp } else {
2937 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2938 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2939 30837e32 2019-07-25 stsp if (error)
2940 5de5890b 2018-10-18 stsp goto done;
2941 5de5890b 2018-10-18 stsp }
2942 5de5890b 2018-10-18 stsp
2943 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2944 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2945 5de5890b 2018-10-18 stsp done:
2946 5de5890b 2018-10-18 stsp free(in_repo_path);
2947 5de5890b 2018-10-18 stsp free(repo_path);
2948 5de5890b 2018-10-18 stsp free(cwd);
2949 5de5890b 2018-10-18 stsp free(commit_id);
2950 7a2c19d6 2019-02-05 stsp if (worktree)
2951 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2952 5de5890b 2018-10-18 stsp if (repo) {
2953 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2954 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2955 5de5890b 2018-10-18 stsp if (error == NULL)
2956 5de5890b 2018-10-18 stsp error = repo_error;
2957 5de5890b 2018-10-18 stsp }
2958 5de5890b 2018-10-18 stsp return error;
2959 5de5890b 2018-10-18 stsp }
2960 5de5890b 2018-10-18 stsp
2961 6bad629b 2019-02-04 stsp __dead static void
2962 6bad629b 2019-02-04 stsp usage_status(void)
2963 6bad629b 2019-02-04 stsp {
2964 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2965 6bad629b 2019-02-04 stsp exit(1);
2966 6bad629b 2019-02-04 stsp }
2967 5c860e29 2018-03-12 stsp
2968 b72f483a 2019-02-05 stsp static const struct got_error *
2969 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2970 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2971 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2972 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2973 6bad629b 2019-02-04 stsp {
2974 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2975 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2976 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2977 b72f483a 2019-02-05 stsp return NULL;
2978 6bad629b 2019-02-04 stsp }
2979 5c860e29 2018-03-12 stsp
2980 6bad629b 2019-02-04 stsp static const struct got_error *
2981 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2982 6bad629b 2019-02-04 stsp {
2983 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2984 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2985 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2986 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2987 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2988 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2989 a5edda0a 2019-07-27 stsp int ch;
2990 5c860e29 2018-03-12 stsp
2991 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2992 72ea6654 2019-07-27 stsp
2993 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2994 6bad629b 2019-02-04 stsp switch (ch) {
2995 5c860e29 2018-03-12 stsp default:
2996 2deda0b9 2019-03-07 stsp usage_status();
2997 6bad629b 2019-02-04 stsp /* NOTREACHED */
2998 5c860e29 2018-03-12 stsp }
2999 5c860e29 2018-03-12 stsp }
3000 5c860e29 2018-03-12 stsp
3001 6bad629b 2019-02-04 stsp argc -= optind;
3002 6bad629b 2019-02-04 stsp argv += optind;
3003 5c860e29 2018-03-12 stsp
3004 6bad629b 2019-02-04 stsp #ifndef PROFILE
3005 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3006 6bad629b 2019-02-04 stsp NULL) == -1)
3007 6bad629b 2019-02-04 stsp err(1, "pledge");
3008 f42b1b34 2018-03-12 stsp #endif
3009 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3010 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3011 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3012 927df6b7 2019-02-10 stsp goto done;
3013 927df6b7 2019-02-10 stsp }
3014 927df6b7 2019-02-10 stsp
3015 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3016 927df6b7 2019-02-10 stsp if (error != NULL)
3017 a5edda0a 2019-07-27 stsp goto done;
3018 6bad629b 2019-02-04 stsp
3019 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3020 c9956ddf 2019-09-08 stsp NULL);
3021 6bad629b 2019-02-04 stsp if (error != NULL)
3022 6bad629b 2019-02-04 stsp goto done;
3023 6bad629b 2019-02-04 stsp
3024 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3025 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3026 087fb88c 2019-08-04 stsp if (error)
3027 087fb88c 2019-08-04 stsp goto done;
3028 087fb88c 2019-08-04 stsp
3029 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3030 6bad629b 2019-02-04 stsp if (error)
3031 6bad629b 2019-02-04 stsp goto done;
3032 6bad629b 2019-02-04 stsp
3033 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3034 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3035 6bad629b 2019-02-04 stsp done:
3036 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3037 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3038 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3039 927df6b7 2019-02-10 stsp free(cwd);
3040 d0eebce4 2019-03-11 stsp return error;
3041 d0eebce4 2019-03-11 stsp }
3042 d0eebce4 2019-03-11 stsp
3043 d0eebce4 2019-03-11 stsp __dead static void
3044 d0eebce4 2019-03-11 stsp usage_ref(void)
3045 d0eebce4 2019-03-11 stsp {
3046 d0eebce4 2019-03-11 stsp fprintf(stderr,
3047 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3048 d0eebce4 2019-03-11 stsp getprogname());
3049 d0eebce4 2019-03-11 stsp exit(1);
3050 d0eebce4 2019-03-11 stsp }
3051 d0eebce4 2019-03-11 stsp
3052 d0eebce4 2019-03-11 stsp static const struct got_error *
3053 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3054 d0eebce4 2019-03-11 stsp {
3055 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3056 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3057 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3058 d0eebce4 2019-03-11 stsp
3059 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3060 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3061 d0eebce4 2019-03-11 stsp if (err)
3062 d0eebce4 2019-03-11 stsp return err;
3063 d0eebce4 2019-03-11 stsp
3064 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3065 d0eebce4 2019-03-11 stsp char *refstr;
3066 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3067 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3068 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3069 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3070 d0eebce4 2019-03-11 stsp free(refstr);
3071 d0eebce4 2019-03-11 stsp }
3072 d0eebce4 2019-03-11 stsp
3073 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3074 d0eebce4 2019-03-11 stsp return NULL;
3075 d0eebce4 2019-03-11 stsp }
3076 d0eebce4 2019-03-11 stsp
3077 d0eebce4 2019-03-11 stsp static const struct got_error *
3078 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3079 d0eebce4 2019-03-11 stsp {
3080 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3081 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3082 d0eebce4 2019-03-11 stsp
3083 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3084 d0eebce4 2019-03-11 stsp if (err)
3085 d0eebce4 2019-03-11 stsp return err;
3086 d0eebce4 2019-03-11 stsp
3087 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3088 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3089 d0eebce4 2019-03-11 stsp return err;
3090 d0eebce4 2019-03-11 stsp }
3091 d0eebce4 2019-03-11 stsp
3092 d0eebce4 2019-03-11 stsp static const struct got_error *
3093 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3094 d0eebce4 2019-03-11 stsp {
3095 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3096 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3097 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3098 d1644381 2019-07-14 stsp
3099 d1644381 2019-07-14 stsp /*
3100 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3101 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3102 d1644381 2019-07-14 stsp * an unintended typo.
3103 d1644381 2019-07-14 stsp */
3104 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3105 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3106 d0eebce4 2019-03-11 stsp
3107 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3108 dd88155e 2019-06-29 stsp repo);
3109 d83d9d5c 2019-05-13 stsp if (err) {
3110 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3111 d0eebce4 2019-03-11 stsp
3112 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3113 d83d9d5c 2019-05-13 stsp return err;
3114 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3115 d83d9d5c 2019-05-13 stsp if (err)
3116 d83d9d5c 2019-05-13 stsp return err;
3117 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3118 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3119 d83d9d5c 2019-05-13 stsp if (err)
3120 d83d9d5c 2019-05-13 stsp return err;
3121 d83d9d5c 2019-05-13 stsp }
3122 d83d9d5c 2019-05-13 stsp
3123 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3124 d0eebce4 2019-03-11 stsp if (err)
3125 d0eebce4 2019-03-11 stsp goto done;
3126 d0eebce4 2019-03-11 stsp
3127 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3128 d0eebce4 2019-03-11 stsp done:
3129 d0eebce4 2019-03-11 stsp if (ref)
3130 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3131 d0eebce4 2019-03-11 stsp free(id);
3132 d1c1ae5f 2019-08-12 stsp return err;
3133 d1c1ae5f 2019-08-12 stsp }
3134 d1c1ae5f 2019-08-12 stsp
3135 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3136 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3137 d1c1ae5f 2019-08-12 stsp {
3138 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3139 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3140 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3141 d1c1ae5f 2019-08-12 stsp
3142 d1c1ae5f 2019-08-12 stsp /*
3143 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3144 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3145 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3146 d1c1ae5f 2019-08-12 stsp */
3147 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3148 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3149 d1c1ae5f 2019-08-12 stsp
3150 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3151 d1c1ae5f 2019-08-12 stsp if (err)
3152 d1c1ae5f 2019-08-12 stsp return err;
3153 d1c1ae5f 2019-08-12 stsp
3154 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3155 d1c1ae5f 2019-08-12 stsp if (err)
3156 d1c1ae5f 2019-08-12 stsp goto done;
3157 d1c1ae5f 2019-08-12 stsp
3158 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3159 d1c1ae5f 2019-08-12 stsp done:
3160 d1c1ae5f 2019-08-12 stsp if (target_ref)
3161 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3162 d1c1ae5f 2019-08-12 stsp if (ref)
3163 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3164 d0eebce4 2019-03-11 stsp return err;
3165 d0eebce4 2019-03-11 stsp }
3166 d0eebce4 2019-03-11 stsp
3167 d0eebce4 2019-03-11 stsp static const struct got_error *
3168 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3169 d0eebce4 2019-03-11 stsp {
3170 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3171 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3172 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3173 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3174 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3175 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3176 d0eebce4 2019-03-11 stsp
3177 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3178 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3179 d0eebce4 2019-03-11 stsp switch (ch) {
3180 d0eebce4 2019-03-11 stsp case 'd':
3181 d0eebce4 2019-03-11 stsp delref = optarg;
3182 d0eebce4 2019-03-11 stsp break;
3183 d0eebce4 2019-03-11 stsp case 'r':
3184 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3185 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3186 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3187 9ba1d308 2019-10-21 stsp optarg);
3188 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3189 d0eebce4 2019-03-11 stsp break;
3190 d0eebce4 2019-03-11 stsp case 'l':
3191 d0eebce4 2019-03-11 stsp do_list = 1;
3192 d1c1ae5f 2019-08-12 stsp break;
3193 d1c1ae5f 2019-08-12 stsp case 's':
3194 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3195 d0eebce4 2019-03-11 stsp break;
3196 d0eebce4 2019-03-11 stsp default:
3197 d0eebce4 2019-03-11 stsp usage_ref();
3198 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3199 d0eebce4 2019-03-11 stsp }
3200 d0eebce4 2019-03-11 stsp }
3201 d0eebce4 2019-03-11 stsp
3202 d0eebce4 2019-03-11 stsp if (do_list && delref)
3203 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3204 d0eebce4 2019-03-11 stsp
3205 d0eebce4 2019-03-11 stsp argc -= optind;
3206 d0eebce4 2019-03-11 stsp argv += optind;
3207 d0eebce4 2019-03-11 stsp
3208 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3209 d1c1ae5f 2019-08-12 stsp if (create_symref)
3210 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3211 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3212 d0eebce4 2019-03-11 stsp if (argc > 0)
3213 d0eebce4 2019-03-11 stsp usage_ref();
3214 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3215 d0eebce4 2019-03-11 stsp usage_ref();
3216 d0eebce4 2019-03-11 stsp
3217 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3218 e0b57350 2019-03-12 stsp if (do_list) {
3219 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3220 e0b57350 2019-03-12 stsp NULL) == -1)
3221 e0b57350 2019-03-12 stsp err(1, "pledge");
3222 e0b57350 2019-03-12 stsp } else {
3223 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3224 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3225 e0b57350 2019-03-12 stsp err(1, "pledge");
3226 e0b57350 2019-03-12 stsp }
3227 d0eebce4 2019-03-11 stsp #endif
3228 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3229 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3230 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3231 d0eebce4 2019-03-11 stsp goto done;
3232 d0eebce4 2019-03-11 stsp }
3233 d0eebce4 2019-03-11 stsp
3234 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3235 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3236 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3237 d0eebce4 2019-03-11 stsp goto done;
3238 d0eebce4 2019-03-11 stsp else
3239 d0eebce4 2019-03-11 stsp error = NULL;
3240 d0eebce4 2019-03-11 stsp if (worktree) {
3241 d0eebce4 2019-03-11 stsp repo_path =
3242 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3243 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3244 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3245 d0eebce4 2019-03-11 stsp if (error)
3246 d0eebce4 2019-03-11 stsp goto done;
3247 d0eebce4 2019-03-11 stsp } else {
3248 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3249 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3250 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3251 d0eebce4 2019-03-11 stsp goto done;
3252 d0eebce4 2019-03-11 stsp }
3253 d0eebce4 2019-03-11 stsp }
3254 d0eebce4 2019-03-11 stsp }
3255 d0eebce4 2019-03-11 stsp
3256 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3257 d0eebce4 2019-03-11 stsp if (error != NULL)
3258 d0eebce4 2019-03-11 stsp goto done;
3259 d0eebce4 2019-03-11 stsp
3260 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3261 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3262 c02c541e 2019-03-29 stsp if (error)
3263 c02c541e 2019-03-29 stsp goto done;
3264 c02c541e 2019-03-29 stsp
3265 d0eebce4 2019-03-11 stsp if (do_list)
3266 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3267 d0eebce4 2019-03-11 stsp else if (delref)
3268 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3269 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3270 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3271 d0eebce4 2019-03-11 stsp else
3272 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3273 4e759de4 2019-06-26 stsp done:
3274 4e759de4 2019-06-26 stsp if (repo)
3275 4e759de4 2019-06-26 stsp got_repo_close(repo);
3276 4e759de4 2019-06-26 stsp if (worktree)
3277 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3278 4e759de4 2019-06-26 stsp free(cwd);
3279 4e759de4 2019-06-26 stsp free(repo_path);
3280 4e759de4 2019-06-26 stsp return error;
3281 4e759de4 2019-06-26 stsp }
3282 4e759de4 2019-06-26 stsp
3283 4e759de4 2019-06-26 stsp __dead static void
3284 4e759de4 2019-06-26 stsp usage_branch(void)
3285 4e759de4 2019-06-26 stsp {
3286 4e759de4 2019-06-26 stsp fprintf(stderr,
3287 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3288 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3289 4e759de4 2019-06-26 stsp exit(1);
3290 4e759de4 2019-06-26 stsp }
3291 4e759de4 2019-06-26 stsp
3292 4e759de4 2019-06-26 stsp static const struct got_error *
3293 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3294 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3295 4e99b47e 2019-10-04 stsp {
3296 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3297 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3298 4e99b47e 2019-10-04 stsp char *refstr;
3299 4e99b47e 2019-10-04 stsp
3300 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3301 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3302 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3303 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3304 4e99b47e 2019-10-04 stsp
3305 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3306 4e99b47e 2019-10-04 stsp if (err)
3307 4e99b47e 2019-10-04 stsp return err;
3308 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3309 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3310 4e99b47e 2019-10-04 stsp marker = "* ";
3311 4e99b47e 2019-10-04 stsp else
3312 4e99b47e 2019-10-04 stsp marker = "~ ";
3313 4e99b47e 2019-10-04 stsp free(id);
3314 4e99b47e 2019-10-04 stsp }
3315 4e99b47e 2019-10-04 stsp
3316 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3317 4e99b47e 2019-10-04 stsp refname += 11;
3318 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3319 4e99b47e 2019-10-04 stsp refname += 18;
3320 4e99b47e 2019-10-04 stsp
3321 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3322 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3323 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3324 4e99b47e 2019-10-04 stsp
3325 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3326 4e99b47e 2019-10-04 stsp free(refstr);
3327 4e99b47e 2019-10-04 stsp return NULL;
3328 4e99b47e 2019-10-04 stsp }
3329 4e99b47e 2019-10-04 stsp
3330 4e99b47e 2019-10-04 stsp static const struct got_error *
3331 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3332 ad89fa31 2019-10-04 stsp {
3333 ad89fa31 2019-10-04 stsp const char *refname;
3334 ad89fa31 2019-10-04 stsp
3335 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3336 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3337 ad89fa31 2019-10-04 stsp
3338 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3339 ad89fa31 2019-10-04 stsp
3340 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3341 ad89fa31 2019-10-04 stsp refname += 11;
3342 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3343 ad89fa31 2019-10-04 stsp refname += 18;
3344 ad89fa31 2019-10-04 stsp
3345 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3346 ad89fa31 2019-10-04 stsp
3347 ad89fa31 2019-10-04 stsp return NULL;
3348 ad89fa31 2019-10-04 stsp }
3349 ad89fa31 2019-10-04 stsp
3350 ad89fa31 2019-10-04 stsp static const struct got_error *
3351 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3352 4e759de4 2019-06-26 stsp {
3353 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3354 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3355 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3356 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3357 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3358 4e759de4 2019-06-26 stsp
3359 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3360 ba882ee3 2019-07-11 stsp
3361 4e99b47e 2019-10-04 stsp if (worktree) {
3362 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3363 4e99b47e 2019-10-04 stsp worktree);
3364 4e99b47e 2019-10-04 stsp if (err)
3365 4e99b47e 2019-10-04 stsp return err;
3366 4e99b47e 2019-10-04 stsp
3367 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3368 4e99b47e 2019-10-04 stsp worktree);
3369 4e99b47e 2019-10-04 stsp if (err)
3370 4e99b47e 2019-10-04 stsp return err;
3371 4e99b47e 2019-10-04 stsp
3372 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3373 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3374 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3375 4e99b47e 2019-10-04 stsp if (err)
3376 4e99b47e 2019-10-04 stsp return err;
3377 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3378 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3379 4e99b47e 2019-10-04 stsp }
3380 4e99b47e 2019-10-04 stsp }
3381 4e99b47e 2019-10-04 stsp
3382 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3383 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3384 4e759de4 2019-06-26 stsp if (err)
3385 4e759de4 2019-06-26 stsp return err;
3386 4e759de4 2019-06-26 stsp
3387 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3388 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3389 4e759de4 2019-06-26 stsp
3390 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3391 4e759de4 2019-06-26 stsp return NULL;
3392 4e759de4 2019-06-26 stsp }
3393 4e759de4 2019-06-26 stsp
3394 4e759de4 2019-06-26 stsp static const struct got_error *
3395 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3396 45cd4e47 2019-08-25 stsp const char *branch_name)
3397 4e759de4 2019-06-26 stsp {
3398 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3399 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3400 4e759de4 2019-06-26 stsp char *refname;
3401 4e759de4 2019-06-26 stsp
3402 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3403 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3404 4e759de4 2019-06-26 stsp
3405 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3406 4e759de4 2019-06-26 stsp if (err)
3407 4e759de4 2019-06-26 stsp goto done;
3408 4e759de4 2019-06-26 stsp
3409 45cd4e47 2019-08-25 stsp if (worktree &&
3410 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3411 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3412 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3413 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3414 45cd4e47 2019-08-25 stsp goto done;
3415 45cd4e47 2019-08-25 stsp }
3416 45cd4e47 2019-08-25 stsp
3417 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3418 4e759de4 2019-06-26 stsp done:
3419 45cd4e47 2019-08-25 stsp if (ref)
3420 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3421 4e759de4 2019-06-26 stsp free(refname);
3422 4e759de4 2019-06-26 stsp return err;
3423 4e759de4 2019-06-26 stsp }
3424 4e759de4 2019-06-26 stsp
3425 4e759de4 2019-06-26 stsp static const struct got_error *
3426 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3427 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3428 4e759de4 2019-06-26 stsp {
3429 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3430 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3431 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3432 d3f84d51 2019-07-11 stsp
3433 d3f84d51 2019-07-11 stsp /*
3434 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3435 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3436 d3f84d51 2019-07-11 stsp * an unintended typo.
3437 d3f84d51 2019-07-11 stsp */
3438 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3439 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3440 4e759de4 2019-06-26 stsp
3441 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3442 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3443 4e759de4 2019-06-26 stsp goto done;
3444 4e759de4 2019-06-26 stsp }
3445 4e759de4 2019-06-26 stsp
3446 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3447 4e759de4 2019-06-26 stsp if (err == NULL) {
3448 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3449 4e759de4 2019-06-26 stsp goto done;
3450 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3451 4e759de4 2019-06-26 stsp goto done;
3452 4e759de4 2019-06-26 stsp
3453 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3454 4e759de4 2019-06-26 stsp if (err)
3455 4e759de4 2019-06-26 stsp goto done;
3456 4e759de4 2019-06-26 stsp
3457 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3458 d0eebce4 2019-03-11 stsp done:
3459 4e759de4 2019-06-26 stsp if (ref)
3460 4e759de4 2019-06-26 stsp got_ref_close(ref);
3461 4e759de4 2019-06-26 stsp free(base_refname);
3462 4e759de4 2019-06-26 stsp free(refname);
3463 4e759de4 2019-06-26 stsp return err;
3464 4e759de4 2019-06-26 stsp }
3465 4e759de4 2019-06-26 stsp
3466 4e759de4 2019-06-26 stsp static const struct got_error *
3467 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3468 4e759de4 2019-06-26 stsp {
3469 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3470 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3471 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3472 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3473 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3474 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3475 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3476 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3477 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3478 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3479 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3480 4e759de4 2019-06-26 stsp
3481 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3482 da76fce2 2020-02-24 stsp
3483 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3484 4e759de4 2019-06-26 stsp switch (ch) {
3485 a74f7e83 2019-11-10 stsp case 'c':
3486 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3487 a74f7e83 2019-11-10 stsp break;
3488 4e759de4 2019-06-26 stsp case 'd':
3489 4e759de4 2019-06-26 stsp delref = optarg;
3490 4e759de4 2019-06-26 stsp break;
3491 4e759de4 2019-06-26 stsp case 'r':
3492 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3493 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3494 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3495 9ba1d308 2019-10-21 stsp optarg);
3496 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3497 4e759de4 2019-06-26 stsp break;
3498 4e759de4 2019-06-26 stsp case 'l':
3499 4e759de4 2019-06-26 stsp do_list = 1;
3500 da76fce2 2020-02-24 stsp break;
3501 da76fce2 2020-02-24 stsp case 'n':
3502 da76fce2 2020-02-24 stsp do_update = 0;
3503 4e759de4 2019-06-26 stsp break;
3504 4e759de4 2019-06-26 stsp default:
3505 4e759de4 2019-06-26 stsp usage_branch();
3506 4e759de4 2019-06-26 stsp /* NOTREACHED */
3507 4e759de4 2019-06-26 stsp }
3508 4e759de4 2019-06-26 stsp }
3509 4e759de4 2019-06-26 stsp
3510 4e759de4 2019-06-26 stsp if (do_list && delref)
3511 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3512 4e759de4 2019-06-26 stsp
3513 4e759de4 2019-06-26 stsp argc -= optind;
3514 4e759de4 2019-06-26 stsp argv += optind;
3515 ad89fa31 2019-10-04 stsp
3516 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3517 ad89fa31 2019-10-04 stsp do_show = 1;
3518 4e759de4 2019-06-26 stsp
3519 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3520 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3521 a74f7e83 2019-11-10 stsp
3522 4e759de4 2019-06-26 stsp if (do_list || delref) {
3523 4e759de4 2019-06-26 stsp if (argc > 0)
3524 4e759de4 2019-06-26 stsp usage_branch();
3525 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3526 4e759de4 2019-06-26 stsp usage_branch();
3527 4e759de4 2019-06-26 stsp
3528 4e759de4 2019-06-26 stsp #ifndef PROFILE
3529 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3530 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3531 4e759de4 2019-06-26 stsp NULL) == -1)
3532 4e759de4 2019-06-26 stsp err(1, "pledge");
3533 4e759de4 2019-06-26 stsp } else {
3534 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3535 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3536 4e759de4 2019-06-26 stsp err(1, "pledge");
3537 4e759de4 2019-06-26 stsp }
3538 4e759de4 2019-06-26 stsp #endif
3539 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3540 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3541 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
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 if (repo_path == NULL) {
3546 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3547 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3548 4e759de4 2019-06-26 stsp goto done;
3549 4e759de4 2019-06-26 stsp else
3550 4e759de4 2019-06-26 stsp error = NULL;
3551 4e759de4 2019-06-26 stsp if (worktree) {
3552 4e759de4 2019-06-26 stsp repo_path =
3553 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3554 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3555 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3556 4e759de4 2019-06-26 stsp if (error)
3557 4e759de4 2019-06-26 stsp goto done;
3558 4e759de4 2019-06-26 stsp } else {
3559 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3560 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3561 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3562 4e759de4 2019-06-26 stsp goto done;
3563 4e759de4 2019-06-26 stsp }
3564 4e759de4 2019-06-26 stsp }
3565 4e759de4 2019-06-26 stsp }
3566 4e759de4 2019-06-26 stsp
3567 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3568 4e759de4 2019-06-26 stsp if (error != NULL)
3569 4e759de4 2019-06-26 stsp goto done;
3570 4e759de4 2019-06-26 stsp
3571 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3572 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3573 4e759de4 2019-06-26 stsp if (error)
3574 4e759de4 2019-06-26 stsp goto done;
3575 4e759de4 2019-06-26 stsp
3576 ad89fa31 2019-10-04 stsp if (do_show)
3577 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3578 ad89fa31 2019-10-04 stsp else if (do_list)
3579 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3580 4e759de4 2019-06-26 stsp else if (delref)
3581 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3582 4e759de4 2019-06-26 stsp else {
3583 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3584 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3585 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3586 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3587 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3588 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3589 a74f7e83 2019-11-10 stsp if (error)
3590 a74f7e83 2019-11-10 stsp goto done;
3591 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3592 da76fce2 2020-02-24 stsp if (error)
3593 da76fce2 2020-02-24 stsp goto done;
3594 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3595 da76fce2 2020-02-24 stsp int did_something = 0;
3596 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3597 da76fce2 2020-02-24 stsp
3598 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3599 da76fce2 2020-02-24 stsp if (error)
3600 da76fce2 2020-02-24 stsp goto done;
3601 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3602 da76fce2 2020-02-24 stsp worktree);
3603 da76fce2 2020-02-24 stsp if (error)
3604 da76fce2 2020-02-24 stsp goto done;
3605 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3606 da76fce2 2020-02-24 stsp == -1) {
3607 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3608 da76fce2 2020-02-24 stsp goto done;
3609 da76fce2 2020-02-24 stsp }
3610 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
3611 da76fce2 2020-02-24 stsp free(branch_refname);
3612 da76fce2 2020-02-24 stsp if (error)
3613 da76fce2 2020-02-24 stsp goto done;
3614 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
3615 da76fce2 2020-02-24 stsp repo);
3616 da76fce2 2020-02-24 stsp if (error)
3617 da76fce2 2020-02-24 stsp goto done;
3618 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3619 da76fce2 2020-02-24 stsp commit_id);
3620 da76fce2 2020-02-24 stsp if (error)
3621 da76fce2 2020-02-24 stsp goto done;
3622 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
3623 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
3624 da76fce2 2020-02-24 stsp check_cancelled, NULL);
3625 da76fce2 2020-02-24 stsp if (error)
3626 da76fce2 2020-02-24 stsp goto done;
3627 da76fce2 2020-02-24 stsp if (did_something)
3628 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
3629 da76fce2 2020-02-24 stsp }
3630 4e759de4 2019-06-26 stsp }
3631 4e759de4 2019-06-26 stsp done:
3632 da76fce2 2020-02-24 stsp if (ref)
3633 da76fce2 2020-02-24 stsp got_ref_close(ref);
3634 d0eebce4 2019-03-11 stsp if (repo)
3635 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3636 d0eebce4 2019-03-11 stsp if (worktree)
3637 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3638 d0eebce4 2019-03-11 stsp free(cwd);
3639 d0eebce4 2019-03-11 stsp free(repo_path);
3640 da76fce2 2020-02-24 stsp free(commit_id);
3641 da76fce2 2020-02-24 stsp free(commit_id_str);
3642 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
3643 da76fce2 2020-02-24 stsp free((char *)pe->path);
3644 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
3645 d00136be 2019-03-26 stsp return error;
3646 d00136be 2019-03-26 stsp }
3647 d00136be 2019-03-26 stsp
3648 8e7bd50a 2019-08-22 stsp
3649 d00136be 2019-03-26 stsp __dead static void
3650 8e7bd50a 2019-08-22 stsp usage_tag(void)
3651 8e7bd50a 2019-08-22 stsp {
3652 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3653 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
3654 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
3655 8e7bd50a 2019-08-22 stsp exit(1);
3656 b8bad2ba 2019-08-23 stsp }
3657 b8bad2ba 2019-08-23 stsp
3658 b8bad2ba 2019-08-23 stsp #if 0
3659 b8bad2ba 2019-08-23 stsp static const struct got_error *
3660 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3661 b8bad2ba 2019-08-23 stsp {
3662 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3663 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3664 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3665 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3666 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3667 b8bad2ba 2019-08-23 stsp
3668 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3669 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3670 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3671 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3672 b8bad2ba 2019-08-23 stsp if (err)
3673 b8bad2ba 2019-08-23 stsp return err;
3674 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3675 b8bad2ba 2019-08-23 stsp continue;
3676 b8bad2ba 2019-08-23 stsp } else {
3677 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3678 b8bad2ba 2019-08-23 stsp if (err)
3679 b8bad2ba 2019-08-23 stsp break;
3680 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3681 b8bad2ba 2019-08-23 stsp free(re_id);
3682 b8bad2ba 2019-08-23 stsp if (err)
3683 b8bad2ba 2019-08-23 stsp break;
3684 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3685 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3686 b8bad2ba 2019-08-23 stsp }
3687 b8bad2ba 2019-08-23 stsp
3688 b8bad2ba 2019-08-23 stsp while (se) {
3689 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3690 b8bad2ba 2019-08-23 stsp if (err)
3691 b8bad2ba 2019-08-23 stsp break;
3692 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3693 b8bad2ba 2019-08-23 stsp free(se_id);
3694 b8bad2ba 2019-08-23 stsp if (err)
3695 b8bad2ba 2019-08-23 stsp break;
3696 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3697 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3698 b8bad2ba 2019-08-23 stsp
3699 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3700 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3701 b8bad2ba 2019-08-23 stsp if (err)
3702 b8bad2ba 2019-08-23 stsp return err;
3703 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3704 b8bad2ba 2019-08-23 stsp break;
3705 b8bad2ba 2019-08-23 stsp }
3706 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3707 b8bad2ba 2019-08-23 stsp continue;
3708 b8bad2ba 2019-08-23 stsp }
3709 b8bad2ba 2019-08-23 stsp }
3710 b8bad2ba 2019-08-23 stsp done:
3711 b8bad2ba 2019-08-23 stsp return err;
3712 8e7bd50a 2019-08-22 stsp }
3713 b8bad2ba 2019-08-23 stsp #endif
3714 b8bad2ba 2019-08-23 stsp
3715 b8bad2ba 2019-08-23 stsp static const struct got_error *
3716 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3717 8e7bd50a 2019-08-22 stsp {
3718 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3719 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3720 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3721 8e7bd50a 2019-08-22 stsp
3722 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3723 8e7bd50a 2019-08-22 stsp
3724 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3725 8e7bd50a 2019-08-22 stsp if (err)
3726 8e7bd50a 2019-08-22 stsp return err;
3727 8e7bd50a 2019-08-22 stsp
3728 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3729 8e7bd50a 2019-08-22 stsp const char *refname;
3730 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3731 8e7bd50a 2019-08-22 stsp char datebuf[26];
3732 d4efa91b 2020-01-14 stsp const char *tagger;
3733 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3734 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3735 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3736 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
3737 8e7bd50a 2019-08-22 stsp
3738 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3739 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3740 8e7bd50a 2019-08-22 stsp continue;
3741 8e7bd50a 2019-08-22 stsp refname += 10;
3742 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3743 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3744 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3745 8e7bd50a 2019-08-22 stsp break;
3746 8e7bd50a 2019-08-22 stsp }
3747 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3748 8e7bd50a 2019-08-22 stsp free(refstr);
3749 8e7bd50a 2019-08-22 stsp
3750 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3751 8e7bd50a 2019-08-22 stsp if (err)
3752 8e7bd50a 2019-08-22 stsp break;
3753 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3754 d4efa91b 2020-01-14 stsp if (err) {
3755 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3756 d4efa91b 2020-01-14 stsp free(id);
3757 d4efa91b 2020-01-14 stsp break;
3758 d4efa91b 2020-01-14 stsp }
3759 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3760 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
3761 d4efa91b 2020-01-14 stsp if (err) {
3762 d4efa91b 2020-01-14 stsp free(id);
3763 d4efa91b 2020-01-14 stsp break;
3764 d4efa91b 2020-01-14 stsp }
3765 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
3766 d4efa91b 2020-01-14 stsp tagger_time =
3767 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
3768 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
3769 d4efa91b 2020-01-14 stsp free(id);
3770 d4efa91b 2020-01-14 stsp if (err)
3771 d4efa91b 2020-01-14 stsp break;
3772 d4efa91b 2020-01-14 stsp } else {
3773 d4efa91b 2020-01-14 stsp free(id);
3774 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
3775 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3776 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
3777 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
3778 d4efa91b 2020-01-14 stsp if (err)
3779 d4efa91b 2020-01-14 stsp break;
3780 d4efa91b 2020-01-14 stsp }
3781 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
3782 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3783 2417344c 2019-08-23 stsp if (datestr)
3784 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3785 d4efa91b 2020-01-14 stsp if (commit)
3786 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3787 d4efa91b 2020-01-14 stsp else {
3788 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
3789 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
3790 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3791 d4efa91b 2020-01-14 stsp id_str);
3792 d4efa91b 2020-01-14 stsp break;
3793 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
3794 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3795 d4efa91b 2020-01-14 stsp id_str);
3796 d4efa91b 2020-01-14 stsp break;
3797 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
3798 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3799 d4efa91b 2020-01-14 stsp id_str);
3800 d4efa91b 2020-01-14 stsp break;
3801 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
3802 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3803 d4efa91b 2020-01-14 stsp id_str);
3804 d4efa91b 2020-01-14 stsp break;
3805 d4efa91b 2020-01-14 stsp default:
3806 d4efa91b 2020-01-14 stsp break;
3807 d4efa91b 2020-01-14 stsp }
3808 8e7bd50a 2019-08-22 stsp }
3809 8e7bd50a 2019-08-22 stsp free(id_str);
3810 d4efa91b 2020-01-14 stsp if (commit) {
3811 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
3812 d4efa91b 2020-01-14 stsp if (err)
3813 d4efa91b 2020-01-14 stsp break;
3814 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
3815 d4efa91b 2020-01-14 stsp } else {
3816 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3817 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
3818 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
3819 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
3820 d4efa91b 2020-01-14 stsp break;
3821 d4efa91b 2020-01-14 stsp }
3822 8e7bd50a 2019-08-22 stsp }
3823 8e7bd50a 2019-08-22 stsp
3824 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3825 8e7bd50a 2019-08-22 stsp do {
3826 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3827 8e7bd50a 2019-08-22 stsp if (line)
3828 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3829 8e7bd50a 2019-08-22 stsp } while (line);
3830 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3831 8e7bd50a 2019-08-22 stsp }
3832 8e7bd50a 2019-08-22 stsp
3833 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3834 8e7bd50a 2019-08-22 stsp return NULL;
3835 8e7bd50a 2019-08-22 stsp }
3836 8e7bd50a 2019-08-22 stsp
3837 8e7bd50a 2019-08-22 stsp static const struct got_error *
3838 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3839 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3840 8e7bd50a 2019-08-22 stsp {
3841 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3842 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3843 f372d5cd 2019-10-21 stsp char *editor = NULL;
3844 8e7bd50a 2019-08-22 stsp int fd = -1;
3845 8e7bd50a 2019-08-22 stsp
3846 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
3847 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3848 8e7bd50a 2019-08-22 stsp goto done;
3849 8e7bd50a 2019-08-22 stsp }
3850 8e7bd50a 2019-08-22 stsp
3851 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3852 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3853 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3854 8e7bd50a 2019-08-22 stsp goto done;
3855 8e7bd50a 2019-08-22 stsp }
3856 8e7bd50a 2019-08-22 stsp
3857 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3858 8e7bd50a 2019-08-22 stsp if (err)
3859 8e7bd50a 2019-08-22 stsp goto done;
3860 8e7bd50a 2019-08-22 stsp
3861 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3862 8e7bd50a 2019-08-22 stsp close(fd);
3863 8e7bd50a 2019-08-22 stsp
3864 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3865 8e7bd50a 2019-08-22 stsp if (err)
3866 8e7bd50a 2019-08-22 stsp goto done;
3867 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3868 8e7bd50a 2019-08-22 stsp done:
3869 8e7bd50a 2019-08-22 stsp free(initial_content);
3870 8e7bd50a 2019-08-22 stsp free(template);
3871 8e7bd50a 2019-08-22 stsp free(editor);
3872 8e7bd50a 2019-08-22 stsp
3873 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3874 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3875 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3876 8e7bd50a 2019-08-22 stsp if (err) {
3877 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3878 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3879 8e7bd50a 2019-08-22 stsp }
3880 8e7bd50a 2019-08-22 stsp }
3881 8e7bd50a 2019-08-22 stsp return err;
3882 8e7bd50a 2019-08-22 stsp }
3883 8e7bd50a 2019-08-22 stsp
3884 8e7bd50a 2019-08-22 stsp static const struct got_error *
3885 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3886 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3887 8e7bd50a 2019-08-22 stsp {
3888 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3889 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3890 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3891 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3892 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3893 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
3894 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
3895 8e7bd50a 2019-08-22 stsp
3896 8e7bd50a 2019-08-22 stsp /*
3897 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
3898 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3899 8e7bd50a 2019-08-22 stsp * an unintended typo.
3900 8e7bd50a 2019-08-22 stsp */
3901 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
3902 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3903 8e7bd50a 2019-08-22 stsp
3904 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3905 8e7bd50a 2019-08-22 stsp if (err)
3906 8e7bd50a 2019-08-22 stsp return err;
3907 8e7bd50a 2019-08-22 stsp
3908 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3909 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3910 8e7bd50a 2019-08-22 stsp if (err)
3911 8e7bd50a 2019-08-22 stsp goto done;
3912 8e7bd50a 2019-08-22 stsp
3913 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3914 8e7bd50a 2019-08-22 stsp if (err)
3915 8e7bd50a 2019-08-22 stsp goto done;
3916 8e7bd50a 2019-08-22 stsp
3917 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3918 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3919 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3920 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3921 8e7bd50a 2019-08-22 stsp goto done;
3922 8e7bd50a 2019-08-22 stsp }
3923 8e7bd50a 2019-08-22 stsp tag_name += 10;
3924 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3925 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3926 8e7bd50a 2019-08-22 stsp goto done;
3927 8e7bd50a 2019-08-22 stsp }
3928 8e7bd50a 2019-08-22 stsp
3929 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3930 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3931 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3932 8e7bd50a 2019-08-22 stsp goto done;
3933 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3934 8e7bd50a 2019-08-22 stsp goto done;
3935 8e7bd50a 2019-08-22 stsp
3936 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3937 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3938 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3939 f372d5cd 2019-10-21 stsp if (err) {
3940 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3941 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
3942 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3943 8e7bd50a 2019-08-22 stsp goto done;
3944 f372d5cd 2019-10-21 stsp }
3945 8e7bd50a 2019-08-22 stsp }
3946 8e7bd50a 2019-08-22 stsp
3947 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3948 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3949 f372d5cd 2019-10-21 stsp if (err) {
3950 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3951 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3952 8e7bd50a 2019-08-22 stsp goto done;
3953 f372d5cd 2019-10-21 stsp }
3954 8e7bd50a 2019-08-22 stsp
3955 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
3956 f372d5cd 2019-10-21 stsp if (err) {
3957 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3958 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3959 8e7bd50a 2019-08-22 stsp goto done;
3960 f372d5cd 2019-10-21 stsp }
3961 8e7bd50a 2019-08-22 stsp
3962 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
3963 f372d5cd 2019-10-21 stsp if (err) {
3964 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3965 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3966 f372d5cd 2019-10-21 stsp goto done;
3967 f372d5cd 2019-10-21 stsp }
3968 8e7bd50a 2019-08-22 stsp
3969 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
3970 f372d5cd 2019-10-21 stsp if (err) {
3971 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3972 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3973 f372d5cd 2019-10-21 stsp goto done;
3974 8e7bd50a 2019-08-22 stsp }
3975 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
3976 8e7bd50a 2019-08-22 stsp done:
3977 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
3978 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
3979 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
3980 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3981 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
3982 f372d5cd 2019-10-21 stsp free(tag_id_str);
3983 8e7bd50a 2019-08-22 stsp if (ref)
3984 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
3985 8e7bd50a 2019-08-22 stsp free(commit_id);
3986 8e7bd50a 2019-08-22 stsp free(commit_id_str);
3987 8e7bd50a 2019-08-22 stsp free(refname);
3988 8e7bd50a 2019-08-22 stsp free(tagmsg);
3989 f372d5cd 2019-10-21 stsp free(tagmsg_path);
3990 aba9c984 2019-09-08 stsp free(tagger);
3991 8e7bd50a 2019-08-22 stsp return err;
3992 8e7bd50a 2019-08-22 stsp }
3993 8e7bd50a 2019-08-22 stsp
3994 8e7bd50a 2019-08-22 stsp static const struct got_error *
3995 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
3996 8e7bd50a 2019-08-22 stsp {
3997 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
3998 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
3999 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4000 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4001 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4002 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4003 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4004 8e7bd50a 2019-08-22 stsp
4005 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4006 8e7bd50a 2019-08-22 stsp switch (ch) {
4007 80106605 2020-02-24 stsp case 'c':
4008 80106605 2020-02-24 stsp commit_id_arg = optarg;
4009 80106605 2020-02-24 stsp break;
4010 8e7bd50a 2019-08-22 stsp case 'm':
4011 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4012 8e7bd50a 2019-08-22 stsp break;
4013 8e7bd50a 2019-08-22 stsp case 'r':
4014 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4015 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4016 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4017 9ba1d308 2019-10-21 stsp optarg);
4018 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4019 8e7bd50a 2019-08-22 stsp break;
4020 8e7bd50a 2019-08-22 stsp case 'l':
4021 8e7bd50a 2019-08-22 stsp do_list = 1;
4022 8e7bd50a 2019-08-22 stsp break;
4023 8e7bd50a 2019-08-22 stsp default:
4024 8e7bd50a 2019-08-22 stsp usage_tag();
4025 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4026 8e7bd50a 2019-08-22 stsp }
4027 8e7bd50a 2019-08-22 stsp }
4028 8e7bd50a 2019-08-22 stsp
4029 8e7bd50a 2019-08-22 stsp argc -= optind;
4030 8e7bd50a 2019-08-22 stsp argv += optind;
4031 8e7bd50a 2019-08-22 stsp
4032 8e7bd50a 2019-08-22 stsp if (do_list) {
4033 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4034 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4035 8e7bd50a 2019-08-22 stsp if (tagmsg)
4036 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4037 8e7bd50a 2019-08-22 stsp if (argc > 0)
4038 8e7bd50a 2019-08-22 stsp usage_tag();
4039 80106605 2020-02-24 stsp } else if (argc != 1)
4040 8e7bd50a 2019-08-22 stsp usage_tag();
4041 80106605 2020-02-24 stsp
4042 a2887370 2019-08-23 stsp tag_name = argv[0];
4043 8e7bd50a 2019-08-22 stsp
4044 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4045 8e7bd50a 2019-08-22 stsp if (do_list) {
4046 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4047 8e7bd50a 2019-08-22 stsp NULL) == -1)
4048 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4049 8e7bd50a 2019-08-22 stsp } else {
4050 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4051 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4052 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4053 8e7bd50a 2019-08-22 stsp }
4054 8e7bd50a 2019-08-22 stsp #endif
4055 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4056 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4057 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4058 8e7bd50a 2019-08-22 stsp goto done;
4059 8e7bd50a 2019-08-22 stsp }
4060 8e7bd50a 2019-08-22 stsp
4061 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4062 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4063 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4064 8e7bd50a 2019-08-22 stsp goto done;
4065 8e7bd50a 2019-08-22 stsp else
4066 8e7bd50a 2019-08-22 stsp error = NULL;
4067 8e7bd50a 2019-08-22 stsp if (worktree) {
4068 8e7bd50a 2019-08-22 stsp repo_path =
4069 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4070 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4071 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4072 8e7bd50a 2019-08-22 stsp if (error)
4073 8e7bd50a 2019-08-22 stsp goto done;
4074 8e7bd50a 2019-08-22 stsp } else {
4075 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4076 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4077 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4078 8e7bd50a 2019-08-22 stsp goto done;
4079 8e7bd50a 2019-08-22 stsp }
4080 8e7bd50a 2019-08-22 stsp }
4081 8e7bd50a 2019-08-22 stsp }
4082 8e7bd50a 2019-08-22 stsp
4083 8e7bd50a 2019-08-22 stsp if (do_list) {
4084 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4085 c9956ddf 2019-09-08 stsp if (error != NULL)
4086 c9956ddf 2019-09-08 stsp goto done;
4087 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4088 8e7bd50a 2019-08-22 stsp if (error)
4089 8e7bd50a 2019-08-22 stsp goto done;
4090 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4091 8e7bd50a 2019-08-22 stsp } else {
4092 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4093 c9956ddf 2019-09-08 stsp if (error)
4094 c9956ddf 2019-09-08 stsp goto done;
4095 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4096 c9956ddf 2019-09-08 stsp if (error != NULL)
4097 c9956ddf 2019-09-08 stsp goto done;
4098 c9956ddf 2019-09-08 stsp
4099 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4100 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4101 8e7bd50a 2019-08-22 stsp if (error)
4102 8e7bd50a 2019-08-22 stsp goto done;
4103 8e7bd50a 2019-08-22 stsp }
4104 8e7bd50a 2019-08-22 stsp
4105 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4106 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4107 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4108 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4109 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4110 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4111 8e7bd50a 2019-08-22 stsp if (error)
4112 8e7bd50a 2019-08-22 stsp goto done;
4113 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4114 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4115 8e7bd50a 2019-08-22 stsp if (error)
4116 8e7bd50a 2019-08-22 stsp goto done;
4117 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4118 8e7bd50a 2019-08-22 stsp free(commit_id);
4119 8e7bd50a 2019-08-22 stsp if (error)
4120 8e7bd50a 2019-08-22 stsp goto done;
4121 8e7bd50a 2019-08-22 stsp }
4122 8e7bd50a 2019-08-22 stsp
4123 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4124 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4125 8e7bd50a 2019-08-22 stsp }
4126 8e7bd50a 2019-08-22 stsp done:
4127 8e7bd50a 2019-08-22 stsp if (repo)
4128 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4129 8e7bd50a 2019-08-22 stsp if (worktree)
4130 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4131 8e7bd50a 2019-08-22 stsp free(cwd);
4132 8e7bd50a 2019-08-22 stsp free(repo_path);
4133 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4134 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4135 8e7bd50a 2019-08-22 stsp return error;
4136 8e7bd50a 2019-08-22 stsp }
4137 8e7bd50a 2019-08-22 stsp
4138 8e7bd50a 2019-08-22 stsp __dead static void
4139 d00136be 2019-03-26 stsp usage_add(void)
4140 d00136be 2019-03-26 stsp {
4141 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4142 022fae89 2019-12-06 tracey getprogname());
4143 d00136be 2019-03-26 stsp exit(1);
4144 d00136be 2019-03-26 stsp }
4145 d00136be 2019-03-26 stsp
4146 d00136be 2019-03-26 stsp static const struct got_error *
4147 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4148 4e68cba3 2019-11-23 stsp {
4149 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4150 4e68cba3 2019-11-23 stsp path++;
4151 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4152 4e68cba3 2019-11-23 stsp return NULL;
4153 4e68cba3 2019-11-23 stsp }
4154 4e68cba3 2019-11-23 stsp
4155 4e68cba3 2019-11-23 stsp static const struct got_error *
4156 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4157 d00136be 2019-03-26 stsp {
4158 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4159 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4160 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4161 1dd54920 2019-05-11 stsp char *cwd = NULL;
4162 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4163 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4164 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4165 1dd54920 2019-05-11 stsp
4166 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4167 d00136be 2019-03-26 stsp
4168 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4169 d00136be 2019-03-26 stsp switch (ch) {
4170 022fae89 2019-12-06 tracey case 'I':
4171 022fae89 2019-12-06 tracey no_ignores = 1;
4172 022fae89 2019-12-06 tracey break;
4173 4e68cba3 2019-11-23 stsp case 'R':
4174 4e68cba3 2019-11-23 stsp can_recurse = 1;
4175 4e68cba3 2019-11-23 stsp break;
4176 d00136be 2019-03-26 stsp default:
4177 d00136be 2019-03-26 stsp usage_add();
4178 d00136be 2019-03-26 stsp /* NOTREACHED */
4179 d00136be 2019-03-26 stsp }
4180 d00136be 2019-03-26 stsp }
4181 d00136be 2019-03-26 stsp
4182 d00136be 2019-03-26 stsp argc -= optind;
4183 d00136be 2019-03-26 stsp argv += optind;
4184 d00136be 2019-03-26 stsp
4185 43012d58 2019-07-14 stsp #ifndef PROFILE
4186 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4187 43012d58 2019-07-14 stsp NULL) == -1)
4188 43012d58 2019-07-14 stsp err(1, "pledge");
4189 43012d58 2019-07-14 stsp #endif
4190 723c305c 2019-05-11 jcs if (argc < 1)
4191 d00136be 2019-03-26 stsp usage_add();
4192 d00136be 2019-03-26 stsp
4193 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4194 d00136be 2019-03-26 stsp if (cwd == NULL) {
4195 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4196 d00136be 2019-03-26 stsp goto done;
4197 d00136be 2019-03-26 stsp }
4198 723c305c 2019-05-11 jcs
4199 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4200 d00136be 2019-03-26 stsp if (error)
4201 d00136be 2019-03-26 stsp goto done;
4202 d00136be 2019-03-26 stsp
4203 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4204 c9956ddf 2019-09-08 stsp NULL);
4205 031a5338 2019-03-26 stsp if (error != NULL)
4206 031a5338 2019-03-26 stsp goto done;
4207 031a5338 2019-03-26 stsp
4208 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4209 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4210 d00136be 2019-03-26 stsp if (error)
4211 d00136be 2019-03-26 stsp goto done;
4212 d00136be 2019-03-26 stsp
4213 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4214 6d022e97 2019-08-04 stsp if (error)
4215 022fae89 2019-12-06 tracey goto done;
4216 022fae89 2019-12-06 tracey
4217 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4218 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4219 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4220 6d022e97 2019-08-04 stsp goto done;
4221 723c305c 2019-05-11 jcs
4222 022fae89 2019-12-06 tracey }
4223 022fae89 2019-12-06 tracey
4224 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4225 4e68cba3 2019-11-23 stsp char *ondisk_path;
4226 4e68cba3 2019-11-23 stsp struct stat sb;
4227 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4228 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4229 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4230 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4231 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4232 4e68cba3 2019-11-23 stsp goto done;
4233 4e68cba3 2019-11-23 stsp }
4234 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4235 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4236 4e68cba3 2019-11-23 stsp free(ondisk_path);
4237 4e68cba3 2019-11-23 stsp continue;
4238 4e68cba3 2019-11-23 stsp }
4239 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4240 4e68cba3 2019-11-23 stsp ondisk_path);
4241 4e68cba3 2019-11-23 stsp free(ondisk_path);
4242 4e68cba3 2019-11-23 stsp goto done;
4243 4e68cba3 2019-11-23 stsp }
4244 4e68cba3 2019-11-23 stsp free(ondisk_path);
4245 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4246 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4247 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4248 4e68cba3 2019-11-23 stsp goto done;
4249 4e68cba3 2019-11-23 stsp }
4250 4e68cba3 2019-11-23 stsp }
4251 4e68cba3 2019-11-23 stsp }
4252 022fae89 2019-12-06 tracey
4253 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4254 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4255 d00136be 2019-03-26 stsp done:
4256 031a5338 2019-03-26 stsp if (repo)
4257 031a5338 2019-03-26 stsp got_repo_close(repo);
4258 d00136be 2019-03-26 stsp if (worktree)
4259 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4260 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4261 1dd54920 2019-05-11 stsp free((char *)pe->path);
4262 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4263 2ec1f75b 2019-03-26 stsp free(cwd);
4264 2ec1f75b 2019-03-26 stsp return error;
4265 2ec1f75b 2019-03-26 stsp }
4266 2ec1f75b 2019-03-26 stsp
4267 2ec1f75b 2019-03-26 stsp __dead static void
4268 648e4ef7 2019-07-09 stsp usage_remove(void)
4269 2ec1f75b 2019-03-26 stsp {
4270 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4271 f2a9dc41 2019-12-13 tracey getprogname());
4272 2ec1f75b 2019-03-26 stsp exit(1);
4273 2a06fe5f 2019-08-24 stsp }
4274 2a06fe5f 2019-08-24 stsp
4275 2a06fe5f 2019-08-24 stsp static const struct got_error *
4276 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4277 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4278 2a06fe5f 2019-08-24 stsp {
4279 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4280 f2a9dc41 2019-12-13 tracey path++;
4281 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4282 2a06fe5f 2019-08-24 stsp return NULL;
4283 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4284 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4285 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4286 2a06fe5f 2019-08-24 stsp return NULL;
4287 2ec1f75b 2019-03-26 stsp }
4288 2ec1f75b 2019-03-26 stsp
4289 2ec1f75b 2019-03-26 stsp static const struct got_error *
4290 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4291 2ec1f75b 2019-03-26 stsp {
4292 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4293 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4294 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4295 17ed4618 2019-06-02 stsp char *cwd = NULL;
4296 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4297 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4298 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4299 2ec1f75b 2019-03-26 stsp
4300 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4301 17ed4618 2019-06-02 stsp
4302 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4303 2ec1f75b 2019-03-26 stsp switch (ch) {
4304 2ec1f75b 2019-03-26 stsp case 'f':
4305 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4306 2ec1f75b 2019-03-26 stsp break;
4307 70e3e7f5 2019-12-13 tracey case 'k':
4308 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4309 70e3e7f5 2019-12-13 tracey break;
4310 f2a9dc41 2019-12-13 tracey case 'R':
4311 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4312 f2a9dc41 2019-12-13 tracey break;
4313 2ec1f75b 2019-03-26 stsp default:
4314 f2a9dc41 2019-12-13 tracey usage_remove();
4315 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4316 2ec1f75b 2019-03-26 stsp }
4317 2ec1f75b 2019-03-26 stsp }
4318 2ec1f75b 2019-03-26 stsp
4319 2ec1f75b 2019-03-26 stsp argc -= optind;
4320 2ec1f75b 2019-03-26 stsp argv += optind;
4321 2ec1f75b 2019-03-26 stsp
4322 43012d58 2019-07-14 stsp #ifndef PROFILE
4323 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4324 43012d58 2019-07-14 stsp NULL) == -1)
4325 43012d58 2019-07-14 stsp err(1, "pledge");
4326 43012d58 2019-07-14 stsp #endif
4327 17ed4618 2019-06-02 stsp if (argc < 1)
4328 648e4ef7 2019-07-09 stsp usage_remove();
4329 2ec1f75b 2019-03-26 stsp
4330 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4331 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4332 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4333 2ec1f75b 2019-03-26 stsp goto done;
4334 2ec1f75b 2019-03-26 stsp }
4335 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4336 2ec1f75b 2019-03-26 stsp if (error)
4337 2ec1f75b 2019-03-26 stsp goto done;
4338 2ec1f75b 2019-03-26 stsp
4339 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4340 c9956ddf 2019-09-08 stsp NULL);
4341 2af4a041 2019-05-11 jcs if (error)
4342 2ec1f75b 2019-03-26 stsp goto done;
4343 2ec1f75b 2019-03-26 stsp
4344 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4345 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4346 2ec1f75b 2019-03-26 stsp if (error)
4347 2ec1f75b 2019-03-26 stsp goto done;
4348 2ec1f75b 2019-03-26 stsp
4349 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4350 6d022e97 2019-08-04 stsp if (error)
4351 6d022e97 2019-08-04 stsp goto done;
4352 17ed4618 2019-06-02 stsp
4353 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4354 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4355 f2a9dc41 2019-12-13 tracey struct stat sb;
4356 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4357 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4358 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4359 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4360 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4361 f2a9dc41 2019-12-13 tracey goto done;
4362 f2a9dc41 2019-12-13 tracey }
4363 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4364 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4365 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4366 f2a9dc41 2019-12-13 tracey continue;
4367 f2a9dc41 2019-12-13 tracey }
4368 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4369 f2a9dc41 2019-12-13 tracey ondisk_path);
4370 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4371 f2a9dc41 2019-12-13 tracey goto done;
4372 f2a9dc41 2019-12-13 tracey }
4373 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4374 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4375 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4376 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4377 f2a9dc41 2019-12-13 tracey goto done;
4378 f2a9dc41 2019-12-13 tracey }
4379 f2a9dc41 2019-12-13 tracey }
4380 f2a9dc41 2019-12-13 tracey }
4381 f2a9dc41 2019-12-13 tracey
4382 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4383 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4384 a129376b 2019-03-28 stsp done:
4385 a129376b 2019-03-28 stsp if (repo)
4386 a129376b 2019-03-28 stsp got_repo_close(repo);
4387 a129376b 2019-03-28 stsp if (worktree)
4388 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4389 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4390 17ed4618 2019-06-02 stsp free((char *)pe->path);
4391 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4392 a129376b 2019-03-28 stsp free(cwd);
4393 a129376b 2019-03-28 stsp return error;
4394 a129376b 2019-03-28 stsp }
4395 a129376b 2019-03-28 stsp
4396 a129376b 2019-03-28 stsp __dead static void
4397 a129376b 2019-03-28 stsp usage_revert(void)
4398 a129376b 2019-03-28 stsp {
4399 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4400 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4401 a129376b 2019-03-28 stsp exit(1);
4402 a129376b 2019-03-28 stsp }
4403 a129376b 2019-03-28 stsp
4404 1ee397ad 2019-07-12 stsp static const struct got_error *
4405 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4406 a129376b 2019-03-28 stsp {
4407 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4408 fb9704af 2020-01-27 stsp return NULL;
4409 fb9704af 2020-01-27 stsp
4410 a129376b 2019-03-28 stsp while (path[0] == '/')
4411 a129376b 2019-03-28 stsp path++;
4412 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4413 33aa809d 2019-08-08 stsp return NULL;
4414 33aa809d 2019-08-08 stsp }
4415 33aa809d 2019-08-08 stsp
4416 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4417 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4418 33aa809d 2019-08-08 stsp const char *action;
4419 33aa809d 2019-08-08 stsp };
4420 33aa809d 2019-08-08 stsp
4421 33aa809d 2019-08-08 stsp static const struct got_error *
4422 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4423 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4424 33aa809d 2019-08-08 stsp {
4425 33aa809d 2019-08-08 stsp char *line = NULL;
4426 33aa809d 2019-08-08 stsp size_t linesize = 0;
4427 33aa809d 2019-08-08 stsp ssize_t linelen;
4428 33aa809d 2019-08-08 stsp
4429 33aa809d 2019-08-08 stsp switch (status) {
4430 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4431 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4432 33aa809d 2019-08-08 stsp break;
4433 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4434 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4435 33aa809d 2019-08-08 stsp break;
4436 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4437 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4438 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4439 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4440 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4441 33aa809d 2019-08-08 stsp printf("%s", line);
4442 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4443 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4444 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4445 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4446 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4447 33aa809d 2019-08-08 stsp break;
4448 33aa809d 2019-08-08 stsp default:
4449 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4450 33aa809d 2019-08-08 stsp }
4451 33aa809d 2019-08-08 stsp
4452 33aa809d 2019-08-08 stsp return NULL;
4453 33aa809d 2019-08-08 stsp }
4454 33aa809d 2019-08-08 stsp
4455 33aa809d 2019-08-08 stsp static const struct got_error *
4456 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4457 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4458 33aa809d 2019-08-08 stsp {
4459 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4460 33aa809d 2019-08-08 stsp char *line = NULL;
4461 33aa809d 2019-08-08 stsp size_t linesize = 0;
4462 33aa809d 2019-08-08 stsp ssize_t linelen;
4463 33aa809d 2019-08-08 stsp int resp = ' ';
4464 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4465 33aa809d 2019-08-08 stsp
4466 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4467 33aa809d 2019-08-08 stsp
4468 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4469 33aa809d 2019-08-08 stsp char *nl;
4470 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4471 33aa809d 2019-08-08 stsp a->action);
4472 33aa809d 2019-08-08 stsp if (err)
4473 33aa809d 2019-08-08 stsp return err;
4474 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4475 33aa809d 2019-08-08 stsp if (linelen == -1) {
4476 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4477 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4478 33aa809d 2019-08-08 stsp return NULL;
4479 33aa809d 2019-08-08 stsp }
4480 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4481 33aa809d 2019-08-08 stsp if (nl)
4482 33aa809d 2019-08-08 stsp *nl = '\0';
4483 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4484 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4485 33aa809d 2019-08-08 stsp printf("y\n");
4486 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4487 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4488 33aa809d 2019-08-08 stsp printf("n\n");
4489 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4490 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4491 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4492 33aa809d 2019-08-08 stsp printf("q\n");
4493 33aa809d 2019-08-08 stsp } else
4494 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4495 33aa809d 2019-08-08 stsp free(line);
4496 33aa809d 2019-08-08 stsp return NULL;
4497 33aa809d 2019-08-08 stsp }
4498 33aa809d 2019-08-08 stsp
4499 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4500 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4501 33aa809d 2019-08-08 stsp a->action);
4502 33aa809d 2019-08-08 stsp if (err)
4503 33aa809d 2019-08-08 stsp return err;
4504 33aa809d 2019-08-08 stsp resp = getchar();
4505 33aa809d 2019-08-08 stsp if (resp == '\n')
4506 33aa809d 2019-08-08 stsp resp = getchar();
4507 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4508 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4509 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4510 33aa809d 2019-08-08 stsp resp = ' ';
4511 33aa809d 2019-08-08 stsp }
4512 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4513 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4514 33aa809d 2019-08-08 stsp resp = ' ';
4515 33aa809d 2019-08-08 stsp }
4516 33aa809d 2019-08-08 stsp }
4517 33aa809d 2019-08-08 stsp
4518 33aa809d 2019-08-08 stsp if (resp == 'y')
4519 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4520 33aa809d 2019-08-08 stsp else if (resp == 'n')
4521 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4522 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4523 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4524 33aa809d 2019-08-08 stsp
4525 1ee397ad 2019-07-12 stsp return NULL;
4526 a129376b 2019-03-28 stsp }
4527 a129376b 2019-03-28 stsp
4528 33aa809d 2019-08-08 stsp
4529 a129376b 2019-03-28 stsp static const struct got_error *
4530 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4531 a129376b 2019-03-28 stsp {
4532 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4533 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4534 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4535 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4536 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4537 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4538 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4539 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4540 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4541 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4542 a129376b 2019-03-28 stsp
4543 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4544 e20a8b6f 2019-06-04 stsp
4545 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4546 a129376b 2019-03-28 stsp switch (ch) {
4547 33aa809d 2019-08-08 stsp case 'p':
4548 33aa809d 2019-08-08 stsp pflag = 1;
4549 33aa809d 2019-08-08 stsp break;
4550 33aa809d 2019-08-08 stsp case 'F':
4551 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4552 33aa809d 2019-08-08 stsp break;
4553 0f6d7415 2019-08-08 stsp case 'R':
4554 0f6d7415 2019-08-08 stsp can_recurse = 1;
4555 0f6d7415 2019-08-08 stsp break;
4556 a129376b 2019-03-28 stsp default:
4557 a129376b 2019-03-28 stsp usage_revert();
4558 a129376b 2019-03-28 stsp /* NOTREACHED */
4559 a129376b 2019-03-28 stsp }
4560 a129376b 2019-03-28 stsp }
4561 a129376b 2019-03-28 stsp
4562 a129376b 2019-03-28 stsp argc -= optind;
4563 a129376b 2019-03-28 stsp argv += optind;
4564 a129376b 2019-03-28 stsp
4565 43012d58 2019-07-14 stsp #ifndef PROFILE
4566 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4567 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4568 43012d58 2019-07-14 stsp err(1, "pledge");
4569 43012d58 2019-07-14 stsp #endif
4570 e20a8b6f 2019-06-04 stsp if (argc < 1)
4571 a129376b 2019-03-28 stsp usage_revert();
4572 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4573 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4574 a129376b 2019-03-28 stsp
4575 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4576 a129376b 2019-03-28 stsp if (cwd == NULL) {
4577 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4578 a129376b 2019-03-28 stsp goto done;
4579 a129376b 2019-03-28 stsp }
4580 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4581 2ec1f75b 2019-03-26 stsp if (error)
4582 2ec1f75b 2019-03-26 stsp goto done;
4583 a129376b 2019-03-28 stsp
4584 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4585 c9956ddf 2019-09-08 stsp NULL);
4586 a129376b 2019-03-28 stsp if (error != NULL)
4587 a129376b 2019-03-28 stsp goto done;
4588 a129376b 2019-03-28 stsp
4589 33aa809d 2019-08-08 stsp if (patch_script_path) {
4590 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4591 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4592 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4593 33aa809d 2019-08-08 stsp patch_script_path);
4594 33aa809d 2019-08-08 stsp goto done;
4595 33aa809d 2019-08-08 stsp }
4596 33aa809d 2019-08-08 stsp }
4597 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4598 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4599 a129376b 2019-03-28 stsp if (error)
4600 a129376b 2019-03-28 stsp goto done;
4601 a129376b 2019-03-28 stsp
4602 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4603 6d022e97 2019-08-04 stsp if (error)
4604 6d022e97 2019-08-04 stsp goto done;
4605 00db391e 2019-08-03 stsp
4606 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4607 0f6d7415 2019-08-08 stsp char *ondisk_path;
4608 0f6d7415 2019-08-08 stsp struct stat sb;
4609 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4610 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4611 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4612 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4613 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4614 0f6d7415 2019-08-08 stsp goto done;
4615 0f6d7415 2019-08-08 stsp }
4616 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4617 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4618 0f6d7415 2019-08-08 stsp free(ondisk_path);
4619 0f6d7415 2019-08-08 stsp continue;
4620 0f6d7415 2019-08-08 stsp }
4621 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4622 0f6d7415 2019-08-08 stsp ondisk_path);
4623 0f6d7415 2019-08-08 stsp free(ondisk_path);
4624 0f6d7415 2019-08-08 stsp goto done;
4625 0f6d7415 2019-08-08 stsp }
4626 0f6d7415 2019-08-08 stsp free(ondisk_path);
4627 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4628 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4629 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4630 0f6d7415 2019-08-08 stsp goto done;
4631 0f6d7415 2019-08-08 stsp }
4632 0f6d7415 2019-08-08 stsp }
4633 0f6d7415 2019-08-08 stsp }
4634 0f6d7415 2019-08-08 stsp
4635 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4636 33aa809d 2019-08-08 stsp cpa.action = "revert";
4637 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4638 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4639 2ec1f75b 2019-03-26 stsp done:
4640 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4641 33aa809d 2019-08-08 stsp error == NULL)
4642 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4643 2ec1f75b 2019-03-26 stsp if (repo)
4644 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4645 2ec1f75b 2019-03-26 stsp if (worktree)
4646 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4647 2ec1f75b 2019-03-26 stsp free(path);
4648 d00136be 2019-03-26 stsp free(cwd);
4649 6bad629b 2019-02-04 stsp return error;
4650 c4296144 2019-05-09 stsp }
4651 c4296144 2019-05-09 stsp
4652 c4296144 2019-05-09 stsp __dead static void
4653 c4296144 2019-05-09 stsp usage_commit(void)
4654 c4296144 2019-05-09 stsp {
4655 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4656 5c1e53bc 2019-07-28 stsp getprogname());
4657 c4296144 2019-05-09 stsp exit(1);
4658 33ad4cbe 2019-05-12 jcs }
4659 33ad4cbe 2019-05-12 jcs
4660 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4661 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4662 e2ba3d07 2019-05-13 stsp const char *editor;
4663 e0870e44 2019-05-13 stsp const char *worktree_path;
4664 76d98825 2019-06-03 stsp const char *branch_name;
4665 314a6357 2019-05-13 stsp const char *repo_path;
4666 e0870e44 2019-05-13 stsp char *logmsg_path;
4667 e2ba3d07 2019-05-13 stsp
4668 e2ba3d07 2019-05-13 stsp };
4669 e0870e44 2019-05-13 stsp
4670 33ad4cbe 2019-05-12 jcs static const struct got_error *
4671 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4672 33ad4cbe 2019-05-12 jcs void *arg)
4673 33ad4cbe 2019-05-12 jcs {
4674 76d98825 2019-06-03 stsp char *initial_content = NULL;
4675 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4676 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4677 e0870e44 2019-05-13 stsp char *template = NULL;
4678 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4679 3ce1b845 2019-07-15 stsp int fd;
4680 33ad4cbe 2019-05-12 jcs size_t len;
4681 33ad4cbe 2019-05-12 jcs
4682 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4683 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4684 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4685 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4686 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4687 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4688 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4689 33ad4cbe 2019-05-12 jcs return NULL;
4690 33ad4cbe 2019-05-12 jcs }
4691 33ad4cbe 2019-05-12 jcs
4692 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4693 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4694 76d98825 2019-06-03 stsp
4695 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4696 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4697 76d98825 2019-06-03 stsp a->branch_name) == -1)
4698 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4699 e0870e44 2019-05-13 stsp
4700 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4701 793c30b5 2019-05-13 stsp if (err)
4702 e0870e44 2019-05-13 stsp goto done;
4703 33ad4cbe 2019-05-12 jcs
4704 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4705 33ad4cbe 2019-05-12 jcs
4706 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4707 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4708 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4709 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4710 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4711 33ad4cbe 2019-05-12 jcs }
4712 33ad4cbe 2019-05-12 jcs close(fd);
4713 33ad4cbe 2019-05-12 jcs
4714 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4715 33ad4cbe 2019-05-12 jcs done:
4716 76d98825 2019-06-03 stsp free(initial_content);
4717 e0870e44 2019-05-13 stsp free(template);
4718 314a6357 2019-05-13 stsp
4719 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4720 3ce1b845 2019-07-15 stsp if (err == NULL) {
4721 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4722 3ce1b845 2019-07-15 stsp if (err) {
4723 3ce1b845 2019-07-15 stsp free(*logmsg);
4724 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4725 3ce1b845 2019-07-15 stsp }
4726 3ce1b845 2019-07-15 stsp }
4727 33ad4cbe 2019-05-12 jcs return err;
4728 6bad629b 2019-02-04 stsp }
4729 c4296144 2019-05-09 stsp
4730 c4296144 2019-05-09 stsp static const struct got_error *
4731 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4732 c4296144 2019-05-09 stsp {
4733 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4734 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4735 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4736 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4737 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4738 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4739 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4740 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4741 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4742 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4743 5c1e53bc 2019-07-28 stsp
4744 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4745 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4746 c4296144 2019-05-09 stsp
4747 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4748 c4296144 2019-05-09 stsp switch (ch) {
4749 c4296144 2019-05-09 stsp case 'm':
4750 c4296144 2019-05-09 stsp logmsg = optarg;
4751 c4296144 2019-05-09 stsp break;
4752 c4296144 2019-05-09 stsp default:
4753 c4296144 2019-05-09 stsp usage_commit();
4754 c4296144 2019-05-09 stsp /* NOTREACHED */
4755 c4296144 2019-05-09 stsp }
4756 c4296144 2019-05-09 stsp }
4757 c4296144 2019-05-09 stsp
4758 c4296144 2019-05-09 stsp argc -= optind;
4759 c4296144 2019-05-09 stsp argv += optind;
4760 c4296144 2019-05-09 stsp
4761 43012d58 2019-07-14 stsp #ifndef PROFILE
4762 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4763 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4764 43012d58 2019-07-14 stsp err(1, "pledge");
4765 43012d58 2019-07-14 stsp #endif
4766 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4767 c4296144 2019-05-09 stsp if (cwd == NULL) {
4768 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4769 c4296144 2019-05-09 stsp goto done;
4770 c4296144 2019-05-09 stsp }
4771 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4772 7d5807f4 2019-07-11 stsp if (error)
4773 7d5807f4 2019-07-11 stsp goto done;
4774 7d5807f4 2019-07-11 stsp
4775 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4776 c4296144 2019-05-09 stsp if (error)
4777 a698f62e 2019-07-25 stsp goto done;
4778 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4779 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4780 c4296144 2019-05-09 stsp goto done;
4781 a698f62e 2019-07-25 stsp }
4782 5c1e53bc 2019-07-28 stsp
4783 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4784 916f288c 2019-07-30 stsp worktree);
4785 5c1e53bc 2019-07-28 stsp if (error)
4786 5c1e53bc 2019-07-28 stsp goto done;
4787 c4296144 2019-05-09 stsp
4788 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4789 c9956ddf 2019-09-08 stsp if (error)
4790 c9956ddf 2019-09-08 stsp goto done;
4791 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4792 c9956ddf 2019-09-08 stsp gitconfig_path);
4793 c4296144 2019-05-09 stsp if (error != NULL)
4794 c4296144 2019-05-09 stsp goto done;
4795 c4296144 2019-05-09 stsp
4796 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4797 aba9c984 2019-09-08 stsp if (error)
4798 aba9c984 2019-09-08 stsp return error;
4799 aba9c984 2019-09-08 stsp
4800 314a6357 2019-05-13 stsp /*
4801 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4802 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4803 314a6357 2019-05-13 stsp */
4804 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4805 314a6357 2019-05-13 stsp error = get_editor(&editor);
4806 314a6357 2019-05-13 stsp else
4807 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4808 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4809 c4296144 2019-05-09 stsp if (error)
4810 c4296144 2019-05-09 stsp goto done;
4811 c4296144 2019-05-09 stsp
4812 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4813 087fb88c 2019-08-04 stsp if (error)
4814 087fb88c 2019-08-04 stsp goto done;
4815 087fb88c 2019-08-04 stsp
4816 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4817 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4818 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4819 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4820 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4821 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4822 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4823 916f288c 2019-07-30 stsp goto done;
4824 916f288c 2019-07-30 stsp }
4825 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4826 916f288c 2019-07-30 stsp }
4827 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4828 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4829 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4830 e0870e44 2019-05-13 stsp if (error) {
4831 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4832 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4833 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4834 c4296144 2019-05-09 stsp goto done;
4835 e0870e44 2019-05-13 stsp }
4836 c4296144 2019-05-09 stsp
4837 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4838 c4296144 2019-05-09 stsp if (error)
4839 c4296144 2019-05-09 stsp goto done;
4840 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4841 c4296144 2019-05-09 stsp done:
4842 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4843 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4844 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4845 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4846 7266f21f 2019-10-21 stsp error == NULL)
4847 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4848 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4849 c4296144 2019-05-09 stsp if (repo)
4850 c4296144 2019-05-09 stsp got_repo_close(repo);
4851 c4296144 2019-05-09 stsp if (worktree)
4852 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4853 c4296144 2019-05-09 stsp free(cwd);
4854 c4296144 2019-05-09 stsp free(id_str);
4855 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4856 e2ba3d07 2019-05-13 stsp free(editor);
4857 aba9c984 2019-09-08 stsp free(author);
4858 234035bc 2019-06-01 stsp return error;
4859 234035bc 2019-06-01 stsp }
4860 234035bc 2019-06-01 stsp
4861 234035bc 2019-06-01 stsp __dead static void
4862 234035bc 2019-06-01 stsp usage_cherrypick(void)
4863 234035bc 2019-06-01 stsp {
4864 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4865 234035bc 2019-06-01 stsp exit(1);
4866 234035bc 2019-06-01 stsp }
4867 234035bc 2019-06-01 stsp
4868 234035bc 2019-06-01 stsp static const struct got_error *
4869 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4870 234035bc 2019-06-01 stsp {
4871 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4872 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4873 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4874 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4875 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4876 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4877 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4878 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4879 234035bc 2019-06-01 stsp int ch, did_something = 0;
4880 234035bc 2019-06-01 stsp
4881 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4882 234035bc 2019-06-01 stsp switch (ch) {
4883 234035bc 2019-06-01 stsp default:
4884 234035bc 2019-06-01 stsp usage_cherrypick();
4885 234035bc 2019-06-01 stsp /* NOTREACHED */
4886 234035bc 2019-06-01 stsp }
4887 234035bc 2019-06-01 stsp }
4888 234035bc 2019-06-01 stsp
4889 234035bc 2019-06-01 stsp argc -= optind;
4890 234035bc 2019-06-01 stsp argv += optind;
4891 234035bc 2019-06-01 stsp
4892 43012d58 2019-07-14 stsp #ifndef PROFILE
4893 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4894 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4895 43012d58 2019-07-14 stsp err(1, "pledge");
4896 43012d58 2019-07-14 stsp #endif
4897 234035bc 2019-06-01 stsp if (argc != 1)
4898 234035bc 2019-06-01 stsp usage_cherrypick();
4899 234035bc 2019-06-01 stsp
4900 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4901 234035bc 2019-06-01 stsp if (cwd == NULL) {
4902 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4903 234035bc 2019-06-01 stsp goto done;
4904 234035bc 2019-06-01 stsp }
4905 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4906 234035bc 2019-06-01 stsp if (error)
4907 234035bc 2019-06-01 stsp goto done;
4908 234035bc 2019-06-01 stsp
4909 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4910 c9956ddf 2019-09-08 stsp NULL);
4911 234035bc 2019-06-01 stsp if (error != NULL)
4912 234035bc 2019-06-01 stsp goto done;
4913 234035bc 2019-06-01 stsp
4914 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4915 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4916 234035bc 2019-06-01 stsp if (error)
4917 234035bc 2019-06-01 stsp goto done;
4918 234035bc 2019-06-01 stsp
4919 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4920 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4921 234035bc 2019-06-01 stsp if (error != NULL) {
4922 234035bc 2019-06-01 stsp struct got_reference *ref;
4923 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4924 234035bc 2019-06-01 stsp goto done;
4925 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4926 234035bc 2019-06-01 stsp if (error != NULL)
4927 234035bc 2019-06-01 stsp goto done;
4928 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4929 234035bc 2019-06-01 stsp got_ref_close(ref);
4930 234035bc 2019-06-01 stsp if (error != NULL)
4931 234035bc 2019-06-01 stsp goto done;
4932 234035bc 2019-06-01 stsp }
4933 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4934 234035bc 2019-06-01 stsp if (error)
4935 234035bc 2019-06-01 stsp goto done;
4936 234035bc 2019-06-01 stsp
4937 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4938 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4939 234035bc 2019-06-01 stsp if (error != NULL)
4940 234035bc 2019-06-01 stsp goto done;
4941 234035bc 2019-06-01 stsp
4942 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4943 234035bc 2019-06-01 stsp if (error) {
4944 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4945 234035bc 2019-06-01 stsp goto done;
4946 234035bc 2019-06-01 stsp error = NULL;
4947 234035bc 2019-06-01 stsp } else {
4948 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4949 234035bc 2019-06-01 stsp goto done;
4950 234035bc 2019-06-01 stsp }
4951 234035bc 2019-06-01 stsp
4952 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4953 234035bc 2019-06-01 stsp if (error)
4954 234035bc 2019-06-01 stsp goto done;
4955 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4956 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4957 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4958 03415a1a 2019-06-02 stsp NULL);
4959 234035bc 2019-06-01 stsp if (error != NULL)
4960 234035bc 2019-06-01 stsp goto done;
4961 234035bc 2019-06-01 stsp
4962 234035bc 2019-06-01 stsp if (did_something)
4963 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4964 234035bc 2019-06-01 stsp done:
4965 234035bc 2019-06-01 stsp if (commit)
4966 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4967 234035bc 2019-06-01 stsp free(commit_id_str);
4968 234035bc 2019-06-01 stsp if (head_ref)
4969 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4970 234035bc 2019-06-01 stsp if (worktree)
4971 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4972 234035bc 2019-06-01 stsp if (repo)
4973 234035bc 2019-06-01 stsp got_repo_close(repo);
4974 c4296144 2019-05-09 stsp return error;
4975 c4296144 2019-05-09 stsp }
4976 5ef14e63 2019-06-02 stsp
4977 5ef14e63 2019-06-02 stsp __dead static void
4978 5ef14e63 2019-06-02 stsp usage_backout(void)
4979 5ef14e63 2019-06-02 stsp {
4980 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4981 5ef14e63 2019-06-02 stsp exit(1);
4982 5ef14e63 2019-06-02 stsp }
4983 5ef14e63 2019-06-02 stsp
4984 5ef14e63 2019-06-02 stsp static const struct got_error *
4985 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4986 5ef14e63 2019-06-02 stsp {
4987 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4988 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4989 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4990 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4991 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4992 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4993 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4994 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4995 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4996 5ef14e63 2019-06-02 stsp
4997 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4998 5ef14e63 2019-06-02 stsp switch (ch) {
4999 5ef14e63 2019-06-02 stsp default:
5000 5ef14e63 2019-06-02 stsp usage_backout();
5001 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5002 5ef14e63 2019-06-02 stsp }
5003 5ef14e63 2019-06-02 stsp }
5004 5ef14e63 2019-06-02 stsp
5005 5ef14e63 2019-06-02 stsp argc -= optind;
5006 5ef14e63 2019-06-02 stsp argv += optind;
5007 5ef14e63 2019-06-02 stsp
5008 43012d58 2019-07-14 stsp #ifndef PROFILE
5009 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5010 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5011 43012d58 2019-07-14 stsp err(1, "pledge");
5012 43012d58 2019-07-14 stsp #endif
5013 5ef14e63 2019-06-02 stsp if (argc != 1)
5014 5ef14e63 2019-06-02 stsp usage_backout();
5015 5ef14e63 2019-06-02 stsp
5016 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5017 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5018 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5019 5ef14e63 2019-06-02 stsp goto done;
5020 5ef14e63 2019-06-02 stsp }
5021 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5022 5ef14e63 2019-06-02 stsp if (error)
5023 5ef14e63 2019-06-02 stsp goto done;
5024 5ef14e63 2019-06-02 stsp
5025 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5026 c9956ddf 2019-09-08 stsp NULL);
5027 5ef14e63 2019-06-02 stsp if (error != NULL)
5028 5ef14e63 2019-06-02 stsp goto done;
5029 5ef14e63 2019-06-02 stsp
5030 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5031 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5032 5ef14e63 2019-06-02 stsp if (error)
5033 5ef14e63 2019-06-02 stsp goto done;
5034 5ef14e63 2019-06-02 stsp
5035 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5036 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5037 5ef14e63 2019-06-02 stsp if (error != NULL) {
5038 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5039 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5040 5ef14e63 2019-06-02 stsp goto done;
5041 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5042 5ef14e63 2019-06-02 stsp if (error != NULL)
5043 5ef14e63 2019-06-02 stsp goto done;
5044 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5045 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5046 5ef14e63 2019-06-02 stsp if (error != NULL)
5047 5ef14e63 2019-06-02 stsp goto done;
5048 5ef14e63 2019-06-02 stsp }
5049 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5050 5ef14e63 2019-06-02 stsp if (error)
5051 5ef14e63 2019-06-02 stsp goto done;
5052 5ef14e63 2019-06-02 stsp
5053 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5054 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5055 5ef14e63 2019-06-02 stsp if (error != NULL)
5056 5ef14e63 2019-06-02 stsp goto done;
5057 5ef14e63 2019-06-02 stsp
5058 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5059 5ef14e63 2019-06-02 stsp if (error)
5060 5ef14e63 2019-06-02 stsp goto done;
5061 5ef14e63 2019-06-02 stsp
5062 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5063 5ef14e63 2019-06-02 stsp if (error)
5064 5ef14e63 2019-06-02 stsp goto done;
5065 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5066 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5067 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5068 5ef14e63 2019-06-02 stsp goto done;
5069 5ef14e63 2019-06-02 stsp }
5070 5ef14e63 2019-06-02 stsp
5071 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5072 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5073 5ef14e63 2019-06-02 stsp if (error != NULL)
5074 5ef14e63 2019-06-02 stsp goto done;
5075 5ef14e63 2019-06-02 stsp
5076 5ef14e63 2019-06-02 stsp if (did_something)
5077 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5078 5ef14e63 2019-06-02 stsp done:
5079 5ef14e63 2019-06-02 stsp if (commit)
5080 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5081 5ef14e63 2019-06-02 stsp free(commit_id_str);
5082 5ef14e63 2019-06-02 stsp if (head_ref)
5083 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5084 818c7501 2019-07-11 stsp if (worktree)
5085 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5086 818c7501 2019-07-11 stsp if (repo)
5087 818c7501 2019-07-11 stsp got_repo_close(repo);
5088 818c7501 2019-07-11 stsp return error;
5089 818c7501 2019-07-11 stsp }
5090 818c7501 2019-07-11 stsp
5091 818c7501 2019-07-11 stsp __dead static void
5092 818c7501 2019-07-11 stsp usage_rebase(void)
5093 818c7501 2019-07-11 stsp {
5094 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5095 af54c8f8 2019-07-11 stsp getprogname());
5096 818c7501 2019-07-11 stsp exit(1);
5097 0ebf8283 2019-07-24 stsp }
5098 0ebf8283 2019-07-24 stsp
5099 0ebf8283 2019-07-24 stsp void
5100 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5101 0ebf8283 2019-07-24 stsp {
5102 0ebf8283 2019-07-24 stsp char *nl;
5103 0ebf8283 2019-07-24 stsp size_t len;
5104 0ebf8283 2019-07-24 stsp
5105 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5106 0ebf8283 2019-07-24 stsp if (len > limit)
5107 0ebf8283 2019-07-24 stsp len = limit;
5108 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5109 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5110 0ebf8283 2019-07-24 stsp if (nl)
5111 0ebf8283 2019-07-24 stsp *nl = '\0';
5112 0ebf8283 2019-07-24 stsp }
5113 0ebf8283 2019-07-24 stsp
5114 0ebf8283 2019-07-24 stsp static const struct got_error *
5115 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5116 0ebf8283 2019-07-24 stsp {
5117 5943eee2 2019-08-13 stsp const struct got_error *err;
5118 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5119 5943eee2 2019-08-13 stsp const char *s;
5120 0ebf8283 2019-07-24 stsp
5121 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5122 5943eee2 2019-08-13 stsp if (err)
5123 5943eee2 2019-08-13 stsp return err;
5124 0ebf8283 2019-07-24 stsp
5125 5943eee2 2019-08-13 stsp s = logmsg0;
5126 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5127 5943eee2 2019-08-13 stsp s++;
5128 5943eee2 2019-08-13 stsp
5129 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5130 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5131 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5132 5943eee2 2019-08-13 stsp goto done;
5133 5943eee2 2019-08-13 stsp }
5134 0ebf8283 2019-07-24 stsp
5135 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5136 5943eee2 2019-08-13 stsp done:
5137 5943eee2 2019-08-13 stsp free(logmsg0);
5138 5943eee2 2019-08-13 stsp return err;
5139 818c7501 2019-07-11 stsp }
5140 818c7501 2019-07-11 stsp
5141 818c7501 2019-07-11 stsp static const struct got_error *
5142 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5143 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5144 818c7501 2019-07-11 stsp {
5145 818c7501 2019-07-11 stsp const struct got_error *err;
5146 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5147 818c7501 2019-07-11 stsp
5148 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5149 818c7501 2019-07-11 stsp if (err)
5150 818c7501 2019-07-11 stsp goto done;
5151 818c7501 2019-07-11 stsp
5152 ff0d2220 2019-07-11 stsp if (new_id) {
5153 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5154 ff0d2220 2019-07-11 stsp if (err)
5155 ff0d2220 2019-07-11 stsp goto done;
5156 ff0d2220 2019-07-11 stsp }
5157 818c7501 2019-07-11 stsp
5158 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5159 ff0d2220 2019-07-11 stsp if (new_id_str)
5160 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5161 0ebf8283 2019-07-24 stsp
5162 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5163 0ebf8283 2019-07-24 stsp if (err)
5164 0ebf8283 2019-07-24 stsp goto done;
5165 0ebf8283 2019-07-24 stsp
5166 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5167 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5168 818c7501 2019-07-11 stsp done:
5169 818c7501 2019-07-11 stsp free(old_id_str);
5170 818c7501 2019-07-11 stsp free(new_id_str);
5171 818c7501 2019-07-11 stsp return err;
5172 818c7501 2019-07-11 stsp }
5173 818c7501 2019-07-11 stsp
5174 1ee397ad 2019-07-12 stsp static const struct got_error *
5175 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5176 818c7501 2019-07-11 stsp {
5177 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5178 818c7501 2019-07-11 stsp
5179 818c7501 2019-07-11 stsp while (path[0] == '/')
5180 818c7501 2019-07-11 stsp path++;
5181 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5182 818c7501 2019-07-11 stsp
5183 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5184 1ee397ad 2019-07-12 stsp return NULL;
5185 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5186 818c7501 2019-07-11 stsp *rebase_status = status;
5187 1ee397ad 2019-07-12 stsp return NULL;
5188 818c7501 2019-07-11 stsp }
5189 818c7501 2019-07-11 stsp
5190 818c7501 2019-07-11 stsp static const struct got_error *
5191 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5192 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5193 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5194 818c7501 2019-07-11 stsp {
5195 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5196 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5197 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5198 818c7501 2019-07-11 stsp }
5199 818c7501 2019-07-11 stsp
5200 818c7501 2019-07-11 stsp static const struct got_error *
5201 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5202 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5203 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5204 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5205 ff0d2220 2019-07-11 stsp {
5206 ff0d2220 2019-07-11 stsp const struct got_error *error;
5207 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5208 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5209 ff0d2220 2019-07-11 stsp
5210 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5211 ff0d2220 2019-07-11 stsp if (error)
5212 ff0d2220 2019-07-11 stsp return error;
5213 ff0d2220 2019-07-11 stsp
5214 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5215 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5216 ff0d2220 2019-07-11 stsp if (error) {
5217 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5218 ff0d2220 2019-07-11 stsp goto done;
5219 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5220 ff0d2220 2019-07-11 stsp } else {
5221 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5222 ff0d2220 2019-07-11 stsp free(new_commit_id);
5223 ff0d2220 2019-07-11 stsp }
5224 ff0d2220 2019-07-11 stsp done:
5225 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5226 ff0d2220 2019-07-11 stsp return error;
5227 64c6d990 2019-07-11 stsp }
5228 64c6d990 2019-07-11 stsp
5229 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5230 64c6d990 2019-07-11 stsp const char *path_prefix;
5231 64c6d990 2019-07-11 stsp size_t len;
5232 8ca9bd68 2019-07-25 stsp int errcode;
5233 64c6d990 2019-07-11 stsp };
5234 64c6d990 2019-07-11 stsp
5235 64c6d990 2019-07-11 stsp static const struct got_error *
5236 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5237 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5238 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5239 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5240 64c6d990 2019-07-11 stsp {
5241 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5242 64c6d990 2019-07-11 stsp
5243 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5244 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5245 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5246 64c6d990 2019-07-11 stsp
5247 64c6d990 2019-07-11 stsp return NULL;
5248 64c6d990 2019-07-11 stsp }
5249 64c6d990 2019-07-11 stsp
5250 64c6d990 2019-07-11 stsp static const struct got_error *
5251 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5252 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5253 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5254 64c6d990 2019-07-11 stsp {
5255 64c6d990 2019-07-11 stsp const struct got_error *err;
5256 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5257 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5258 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5259 64c6d990 2019-07-11 stsp
5260 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5261 64c6d990 2019-07-11 stsp return NULL;
5262 64c6d990 2019-07-11 stsp
5263 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5264 64c6d990 2019-07-11 stsp if (err)
5265 64c6d990 2019-07-11 stsp goto done;
5266 64c6d990 2019-07-11 stsp
5267 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5268 64c6d990 2019-07-11 stsp if (err)
5269 64c6d990 2019-07-11 stsp goto done;
5270 64c6d990 2019-07-11 stsp
5271 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5272 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5273 64c6d990 2019-07-11 stsp if (err)
5274 64c6d990 2019-07-11 stsp goto done;
5275 64c6d990 2019-07-11 stsp
5276 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5277 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5278 64c6d990 2019-07-11 stsp if (err)
5279 64c6d990 2019-07-11 stsp goto done;
5280 64c6d990 2019-07-11 stsp
5281 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5282 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5283 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5284 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5285 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5286 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5287 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5288 64c6d990 2019-07-11 stsp done:
5289 64c6d990 2019-07-11 stsp if (tree1)
5290 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5291 64c6d990 2019-07-11 stsp if (tree2)
5292 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5293 64c6d990 2019-07-11 stsp if (commit)
5294 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5295 64c6d990 2019-07-11 stsp if (parent_commit)
5296 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5297 64c6d990 2019-07-11 stsp return err;
5298 ff0d2220 2019-07-11 stsp }
5299 ff0d2220 2019-07-11 stsp
5300 ff0d2220 2019-07-11 stsp static const struct got_error *
5301 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5302 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5303 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5304 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5305 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5306 af61c510 2019-07-19 stsp {
5307 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5308 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5309 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5310 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5311 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5312 af61c510 2019-07-19 stsp
5313 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5314 af61c510 2019-07-19 stsp if (err)
5315 af61c510 2019-07-19 stsp return err;
5316 af61c510 2019-07-19 stsp
5317 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5318 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5319 af61c510 2019-07-19 stsp if (err)
5320 af61c510 2019-07-19 stsp goto done;
5321 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5322 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5323 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5324 af61c510 2019-07-19 stsp if (err) {
5325 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5326 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5327 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5328 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5329 af61c510 2019-07-19 stsp "been reached?!?");
5330 ee780d5c 2020-01-04 stsp }
5331 ee780d5c 2020-01-04 stsp goto done;
5332 af61c510 2019-07-19 stsp } else {
5333 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5334 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5335 af61c510 2019-07-19 stsp if (err)
5336 af61c510 2019-07-19 stsp goto done;
5337 af61c510 2019-07-19 stsp
5338 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5339 af61c510 2019-07-19 stsp if (err)
5340 af61c510 2019-07-19 stsp goto done;
5341 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5342 af61c510 2019-07-19 stsp commit_id = parent_id;
5343 af61c510 2019-07-19 stsp }
5344 af61c510 2019-07-19 stsp }
5345 af61c510 2019-07-19 stsp done:
5346 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5347 af61c510 2019-07-19 stsp return err;
5348 af61c510 2019-07-19 stsp }
5349 af61c510 2019-07-19 stsp
5350 af61c510 2019-07-19 stsp static const struct got_error *
5351 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5352 818c7501 2019-07-11 stsp {
5353 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5354 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5355 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5356 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5357 818c7501 2019-07-11 stsp char *cwd = NULL;
5358 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5359 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5360 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5361 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5362 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5363 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5364 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5365 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5366 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5367 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5368 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5369 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5370 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5371 818c7501 2019-07-11 stsp
5372 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5373 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5374 818c7501 2019-07-11 stsp
5375 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5376 818c7501 2019-07-11 stsp switch (ch) {
5377 818c7501 2019-07-11 stsp case 'a':
5378 818c7501 2019-07-11 stsp abort_rebase = 1;
5379 818c7501 2019-07-11 stsp break;
5380 818c7501 2019-07-11 stsp case 'c':
5381 818c7501 2019-07-11 stsp continue_rebase = 1;
5382 818c7501 2019-07-11 stsp break;
5383 818c7501 2019-07-11 stsp default:
5384 818c7501 2019-07-11 stsp usage_rebase();
5385 818c7501 2019-07-11 stsp /* NOTREACHED */
5386 818c7501 2019-07-11 stsp }
5387 818c7501 2019-07-11 stsp }
5388 818c7501 2019-07-11 stsp
5389 818c7501 2019-07-11 stsp argc -= optind;
5390 818c7501 2019-07-11 stsp argv += optind;
5391 818c7501 2019-07-11 stsp
5392 43012d58 2019-07-14 stsp #ifndef PROFILE
5393 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5394 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5395 43012d58 2019-07-14 stsp err(1, "pledge");
5396 43012d58 2019-07-14 stsp #endif
5397 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5398 818c7501 2019-07-11 stsp usage_rebase();
5399 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5400 818c7501 2019-07-11 stsp if (argc != 0)
5401 818c7501 2019-07-11 stsp usage_rebase();
5402 818c7501 2019-07-11 stsp } else if (argc != 1)
5403 818c7501 2019-07-11 stsp usage_rebase();
5404 818c7501 2019-07-11 stsp
5405 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5406 818c7501 2019-07-11 stsp if (cwd == NULL) {
5407 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5408 818c7501 2019-07-11 stsp goto done;
5409 818c7501 2019-07-11 stsp }
5410 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5411 818c7501 2019-07-11 stsp if (error)
5412 818c7501 2019-07-11 stsp goto done;
5413 818c7501 2019-07-11 stsp
5414 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5415 c9956ddf 2019-09-08 stsp NULL);
5416 818c7501 2019-07-11 stsp if (error != NULL)
5417 818c7501 2019-07-11 stsp goto done;
5418 818c7501 2019-07-11 stsp
5419 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5420 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5421 7ef62c4e 2020-02-24 stsp if (error)
5422 7ef62c4e 2020-02-24 stsp goto done;
5423 7ef62c4e 2020-02-24 stsp
5424 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5425 7ef62c4e 2020-02-24 stsp worktree);
5426 818c7501 2019-07-11 stsp if (error)
5427 7ef62c4e 2020-02-24 stsp goto done;
5428 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5429 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5430 818c7501 2019-07-11 stsp goto done;
5431 7ef62c4e 2020-02-24 stsp }
5432 818c7501 2019-07-11 stsp
5433 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5434 818c7501 2019-07-11 stsp if (error)
5435 818c7501 2019-07-11 stsp goto done;
5436 818c7501 2019-07-11 stsp
5437 f6794adc 2019-07-23 stsp if (abort_rebase) {
5438 818c7501 2019-07-11 stsp int did_something;
5439 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5440 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5441 f6794adc 2019-07-23 stsp goto done;
5442 f6794adc 2019-07-23 stsp }
5443 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5444 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5445 3e3a69f1 2019-07-25 stsp worktree, repo);
5446 818c7501 2019-07-11 stsp if (error)
5447 818c7501 2019-07-11 stsp goto done;
5448 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5449 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5450 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5451 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5452 818c7501 2019-07-11 stsp if (error)
5453 818c7501 2019-07-11 stsp goto done;
5454 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5455 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5456 818c7501 2019-07-11 stsp }
5457 818c7501 2019-07-11 stsp
5458 818c7501 2019-07-11 stsp if (continue_rebase) {
5459 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5460 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5461 f6794adc 2019-07-23 stsp goto done;
5462 f6794adc 2019-07-23 stsp }
5463 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5464 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5465 3e3a69f1 2019-07-25 stsp worktree, repo);
5466 818c7501 2019-07-11 stsp if (error)
5467 818c7501 2019-07-11 stsp goto done;
5468 818c7501 2019-07-11 stsp
5469 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5470 01757395 2019-07-12 stsp resume_commit_id, repo);
5471 818c7501 2019-07-11 stsp if (error)
5472 818c7501 2019-07-11 stsp goto done;
5473 818c7501 2019-07-11 stsp
5474 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5475 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5476 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5477 818c7501 2019-07-11 stsp goto done;
5478 818c7501 2019-07-11 stsp }
5479 818c7501 2019-07-11 stsp } else {
5480 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5481 818c7501 2019-07-11 stsp if (error != NULL)
5482 818c7501 2019-07-11 stsp goto done;
5483 ff0d2220 2019-07-11 stsp }
5484 818c7501 2019-07-11 stsp
5485 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5486 ff0d2220 2019-07-11 stsp if (error)
5487 ff0d2220 2019-07-11 stsp goto done;
5488 ff0d2220 2019-07-11 stsp
5489 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5490 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5491 a51a74b3 2019-07-27 stsp
5492 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5493 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5494 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5495 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5496 818c7501 2019-07-11 stsp if (error)
5497 818c7501 2019-07-11 stsp goto done;
5498 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5499 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5500 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5501 818c7501 2019-07-11 stsp "with work tree's branch");
5502 818c7501 2019-07-11 stsp goto done;
5503 818c7501 2019-07-11 stsp }
5504 818c7501 2019-07-11 stsp
5505 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5506 a51a74b3 2019-07-27 stsp if (error) {
5507 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5508 a51a74b3 2019-07-27 stsp goto done;
5509 a51a74b3 2019-07-27 stsp error = NULL;
5510 a51a74b3 2019-07-27 stsp } else {
5511 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5512 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5513 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5514 a51a74b3 2019-07-27 stsp goto done;
5515 a51a74b3 2019-07-27 stsp }
5516 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5517 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5518 818c7501 2019-07-11 stsp if (error)
5519 818c7501 2019-07-11 stsp goto done;
5520 818c7501 2019-07-11 stsp }
5521 818c7501 2019-07-11 stsp
5522 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5523 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5524 818c7501 2019-07-11 stsp if (error)
5525 818c7501 2019-07-11 stsp goto done;
5526 818c7501 2019-07-11 stsp
5527 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5528 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5529 fc66b545 2019-08-12 stsp if (pid == NULL) {
5530 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5531 fc66b545 2019-08-12 stsp int did_something;
5532 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5533 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5534 fc66b545 2019-08-12 stsp &did_something);
5535 fc66b545 2019-08-12 stsp if (error)
5536 fc66b545 2019-08-12 stsp goto done;
5537 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5538 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5539 fc66b545 2019-08-12 stsp }
5540 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5541 fc66b545 2019-08-12 stsp goto done;
5542 fc66b545 2019-08-12 stsp }
5543 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5544 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5545 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5546 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5547 818c7501 2019-07-11 stsp commit = NULL;
5548 818c7501 2019-07-11 stsp if (error)
5549 818c7501 2019-07-11 stsp goto done;
5550 64c6d990 2019-07-11 stsp
5551 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5552 38b0338b 2019-11-29 stsp if (continue_rebase) {
5553 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5554 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5555 38b0338b 2019-11-29 stsp goto done;
5556 38b0338b 2019-11-29 stsp } else {
5557 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5558 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5559 38b0338b 2019-11-29 stsp char *id_str;
5560 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5561 38b0338b 2019-11-29 stsp new_base_branch);
5562 38b0338b 2019-11-29 stsp if (error)
5563 38b0338b 2019-11-29 stsp goto done;
5564 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5565 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5566 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5567 38b0338b 2019-11-29 stsp free(id_str);
5568 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5569 38b0338b 2019-11-29 stsp new_head_commit_id);
5570 38b0338b 2019-11-29 stsp if (error)
5571 38b0338b 2019-11-29 stsp goto done;
5572 38b0338b 2019-11-29 stsp }
5573 818c7501 2019-07-11 stsp }
5574 818c7501 2019-07-11 stsp
5575 818c7501 2019-07-11 stsp pid = NULL;
5576 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5577 818c7501 2019-07-11 stsp commit_id = qid->id;
5578 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5579 818c7501 2019-07-11 stsp pid = qid;
5580 818c7501 2019-07-11 stsp
5581 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5582 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5583 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5584 818c7501 2019-07-11 stsp if (error)
5585 818c7501 2019-07-11 stsp goto done;
5586 818c7501 2019-07-11 stsp
5587 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5588 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5589 818c7501 2019-07-11 stsp break;
5590 01757395 2019-07-12 stsp }
5591 818c7501 2019-07-11 stsp
5592 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5593 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5594 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5595 818c7501 2019-07-11 stsp if (error)
5596 818c7501 2019-07-11 stsp goto done;
5597 818c7501 2019-07-11 stsp }
5598 818c7501 2019-07-11 stsp
5599 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5600 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5601 818c7501 2019-07-11 stsp if (error)
5602 818c7501 2019-07-11 stsp goto done;
5603 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5604 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5605 818c7501 2019-07-11 stsp } else
5606 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5607 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5608 818c7501 2019-07-11 stsp done:
5609 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5610 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5611 818c7501 2019-07-11 stsp free(resume_commit_id);
5612 818c7501 2019-07-11 stsp free(yca_id);
5613 818c7501 2019-07-11 stsp if (commit)
5614 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5615 818c7501 2019-07-11 stsp if (branch)
5616 818c7501 2019-07-11 stsp got_ref_close(branch);
5617 818c7501 2019-07-11 stsp if (new_base_branch)
5618 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5619 818c7501 2019-07-11 stsp if (tmp_branch)
5620 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5621 5ef14e63 2019-06-02 stsp if (worktree)
5622 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5623 5ef14e63 2019-06-02 stsp if (repo)
5624 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5625 5ef14e63 2019-06-02 stsp return error;
5626 0ebf8283 2019-07-24 stsp }
5627 0ebf8283 2019-07-24 stsp
5628 0ebf8283 2019-07-24 stsp __dead static void
5629 0ebf8283 2019-07-24 stsp usage_histedit(void)
5630 0ebf8283 2019-07-24 stsp {
5631 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5632 0ebf8283 2019-07-24 stsp getprogname());
5633 0ebf8283 2019-07-24 stsp exit(1);
5634 0ebf8283 2019-07-24 stsp }
5635 0ebf8283 2019-07-24 stsp
5636 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5637 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5638 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5639 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5640 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5641 0ebf8283 2019-07-24 stsp
5642 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5643 0ebf8283 2019-07-24 stsp unsigned char code;
5644 0ebf8283 2019-07-24 stsp const char *name;
5645 0ebf8283 2019-07-24 stsp const char *desc;
5646 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5647 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5648 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5649 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5650 82997472 2020-01-29 stsp "be used" },
5651 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5652 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5653 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5654 0ebf8283 2019-07-24 stsp };
5655 0ebf8283 2019-07-24 stsp
5656 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5657 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5658 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5659 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5660 0ebf8283 2019-07-24 stsp char *logmsg;
5661 0ebf8283 2019-07-24 stsp };
5662 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5663 0ebf8283 2019-07-24 stsp
5664 0ebf8283 2019-07-24 stsp static const struct got_error *
5665 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5666 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5667 0ebf8283 2019-07-24 stsp {
5668 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5669 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5670 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5671 8138f3e1 2019-08-11 stsp int n;
5672 0ebf8283 2019-07-24 stsp
5673 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5674 0ebf8283 2019-07-24 stsp if (err)
5675 0ebf8283 2019-07-24 stsp goto done;
5676 0ebf8283 2019-07-24 stsp
5677 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5678 0ebf8283 2019-07-24 stsp if (err)
5679 0ebf8283 2019-07-24 stsp goto done;
5680 0ebf8283 2019-07-24 stsp
5681 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5682 0ebf8283 2019-07-24 stsp if (err)
5683 0ebf8283 2019-07-24 stsp goto done;
5684 0ebf8283 2019-07-24 stsp
5685 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5686 0ebf8283 2019-07-24 stsp if (n < 0)
5687 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5688 0ebf8283 2019-07-24 stsp done:
5689 0ebf8283 2019-07-24 stsp if (commit)
5690 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5691 0ebf8283 2019-07-24 stsp free(id_str);
5692 0ebf8283 2019-07-24 stsp free(logmsg);
5693 0ebf8283 2019-07-24 stsp return err;
5694 0ebf8283 2019-07-24 stsp }
5695 0ebf8283 2019-07-24 stsp
5696 0ebf8283 2019-07-24 stsp static const struct got_error *
5697 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
5698 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
5699 0ebf8283 2019-07-24 stsp {
5700 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5701 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5702 0ebf8283 2019-07-24 stsp
5703 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5704 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5705 0ebf8283 2019-07-24 stsp
5706 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5707 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5708 0ebf8283 2019-07-24 stsp f, repo);
5709 0ebf8283 2019-07-24 stsp if (err)
5710 0ebf8283 2019-07-24 stsp break;
5711 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
5712 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5713 083957f4 2020-02-24 stsp if (n < 0) {
5714 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
5715 083957f4 2020-02-24 stsp break;
5716 083957f4 2020-02-24 stsp }
5717 083957f4 2020-02-24 stsp }
5718 0ebf8283 2019-07-24 stsp }
5719 0ebf8283 2019-07-24 stsp
5720 0ebf8283 2019-07-24 stsp return err;
5721 0ebf8283 2019-07-24 stsp }
5722 0ebf8283 2019-07-24 stsp
5723 0ebf8283 2019-07-24 stsp static const struct got_error *
5724 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
5725 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
5726 0ebf8283 2019-07-24 stsp {
5727 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5728 0ebf8283 2019-07-24 stsp int n, i;
5729 514f2ffe 2020-01-29 stsp char *id_str;
5730 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
5731 514f2ffe 2020-01-29 stsp
5732 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
5733 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
5734 514f2ffe 2020-01-29 stsp if (err)
5735 514f2ffe 2020-01-29 stsp return err;
5736 514f2ffe 2020-01-29 stsp
5737 514f2ffe 2020-01-29 stsp n = fprintf(f,
5738 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
5739 514f2ffe 2020-01-29 stsp "# commit %s\n"
5740 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
5741 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
5742 514f2ffe 2020-01-29 stsp if (n < 0) {
5743 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5744 514f2ffe 2020-01-29 stsp goto done;
5745 514f2ffe 2020-01-29 stsp }
5746 0ebf8283 2019-07-24 stsp
5747 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5748 514f2ffe 2020-01-29 stsp if (n < 0) {
5749 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5750 514f2ffe 2020-01-29 stsp goto done;
5751 514f2ffe 2020-01-29 stsp }
5752 0ebf8283 2019-07-24 stsp
5753 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5754 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5755 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5756 0ebf8283 2019-07-24 stsp cmd->desc);
5757 0ebf8283 2019-07-24 stsp if (n < 0) {
5758 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5759 0ebf8283 2019-07-24 stsp break;
5760 0ebf8283 2019-07-24 stsp }
5761 0ebf8283 2019-07-24 stsp }
5762 514f2ffe 2020-01-29 stsp done:
5763 514f2ffe 2020-01-29 stsp free(id_str);
5764 0ebf8283 2019-07-24 stsp return err;
5765 0ebf8283 2019-07-24 stsp }
5766 0ebf8283 2019-07-24 stsp
5767 0ebf8283 2019-07-24 stsp static const struct got_error *
5768 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5769 0ebf8283 2019-07-24 stsp {
5770 0ebf8283 2019-07-24 stsp static char msg[42];
5771 0ebf8283 2019-07-24 stsp int ret;
5772 0ebf8283 2019-07-24 stsp
5773 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5774 0ebf8283 2019-07-24 stsp lineno);
5775 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5776 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5777 0ebf8283 2019-07-24 stsp
5778 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5779 0ebf8283 2019-07-24 stsp }
5780 0ebf8283 2019-07-24 stsp
5781 0ebf8283 2019-07-24 stsp static const struct got_error *
5782 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5783 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5784 0ebf8283 2019-07-24 stsp {
5785 0ebf8283 2019-07-24 stsp const struct got_error *err;
5786 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5787 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5788 0ebf8283 2019-07-24 stsp
5789 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5790 0ebf8283 2019-07-24 stsp if (err)
5791 0ebf8283 2019-07-24 stsp return err;
5792 0ebf8283 2019-07-24 stsp
5793 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5794 0ebf8283 2019-07-24 stsp if (err)
5795 0ebf8283 2019-07-24 stsp goto done;
5796 0ebf8283 2019-07-24 stsp
5797 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5798 5943eee2 2019-08-13 stsp if (err)
5799 5943eee2 2019-08-13 stsp goto done;
5800 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5801 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5802 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5803 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5804 0ebf8283 2019-07-24 stsp }
5805 0ebf8283 2019-07-24 stsp done:
5806 0ebf8283 2019-07-24 stsp if (folded_commit)
5807 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5808 0ebf8283 2019-07-24 stsp free(id_str);
5809 5943eee2 2019-08-13 stsp free(folded_logmsg);
5810 0ebf8283 2019-07-24 stsp return err;
5811 0ebf8283 2019-07-24 stsp }
5812 0ebf8283 2019-07-24 stsp
5813 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5814 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5815 0ebf8283 2019-07-24 stsp {
5816 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5817 0ebf8283 2019-07-24 stsp
5818 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5819 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5820 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5821 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5822 3f9de99f 2019-07-24 stsp folded = prev;
5823 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5824 0ebf8283 2019-07-24 stsp }
5825 0ebf8283 2019-07-24 stsp
5826 0ebf8283 2019-07-24 stsp return folded;
5827 0ebf8283 2019-07-24 stsp }
5828 0ebf8283 2019-07-24 stsp
5829 0ebf8283 2019-07-24 stsp static const struct got_error *
5830 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5831 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5832 0ebf8283 2019-07-24 stsp {
5833 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5834 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5835 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5836 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5837 0ebf8283 2019-07-24 stsp int fd;
5838 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5839 0ebf8283 2019-07-24 stsp
5840 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5841 0ebf8283 2019-07-24 stsp if (err)
5842 0ebf8283 2019-07-24 stsp return err;
5843 0ebf8283 2019-07-24 stsp
5844 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5845 0ebf8283 2019-07-24 stsp if (folded) {
5846 0ebf8283 2019-07-24 stsp while (folded != hle) {
5847 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5848 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5849 3f9de99f 2019-07-24 stsp continue;
5850 3f9de99f 2019-07-24 stsp }
5851 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5852 0ebf8283 2019-07-24 stsp logmsg, repo);
5853 0ebf8283 2019-07-24 stsp if (err)
5854 0ebf8283 2019-07-24 stsp goto done;
5855 0ebf8283 2019-07-24 stsp free(logmsg);
5856 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5857 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5858 0ebf8283 2019-07-24 stsp }
5859 0ebf8283 2019-07-24 stsp }
5860 0ebf8283 2019-07-24 stsp
5861 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5862 0ebf8283 2019-07-24 stsp if (err)
5863 0ebf8283 2019-07-24 stsp goto done;
5864 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5865 5943eee2 2019-08-13 stsp if (err)
5866 5943eee2 2019-08-13 stsp goto done;
5867 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5868 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5869 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5870 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5871 0ebf8283 2019-07-24 stsp goto done;
5872 0ebf8283 2019-07-24 stsp }
5873 0ebf8283 2019-07-24 stsp free(logmsg);
5874 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5875 0ebf8283 2019-07-24 stsp
5876 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5877 0ebf8283 2019-07-24 stsp if (err)
5878 0ebf8283 2019-07-24 stsp goto done;
5879 0ebf8283 2019-07-24 stsp
5880 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
5881 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
5882 0ebf8283 2019-07-24 stsp if (err)
5883 0ebf8283 2019-07-24 stsp goto done;
5884 0ebf8283 2019-07-24 stsp
5885 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5886 0ebf8283 2019-07-24 stsp close(fd);
5887 0ebf8283 2019-07-24 stsp
5888 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5889 0ebf8283 2019-07-24 stsp if (err)
5890 0ebf8283 2019-07-24 stsp goto done;
5891 0ebf8283 2019-07-24 stsp
5892 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5893 0ebf8283 2019-07-24 stsp if (err) {
5894 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5895 0ebf8283 2019-07-24 stsp goto done;
5896 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5897 0ebf8283 2019-07-24 stsp }
5898 0ebf8283 2019-07-24 stsp done:
5899 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5900 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5901 0ebf8283 2019-07-24 stsp free(logmsg_path);
5902 0ebf8283 2019-07-24 stsp free(logmsg);
5903 5943eee2 2019-08-13 stsp free(orig_logmsg);
5904 0ebf8283 2019-07-24 stsp free(editor);
5905 0ebf8283 2019-07-24 stsp if (commit)
5906 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5907 0ebf8283 2019-07-24 stsp return err;
5908 5ef14e63 2019-06-02 stsp }
5909 0ebf8283 2019-07-24 stsp
5910 0ebf8283 2019-07-24 stsp static const struct got_error *
5911 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5912 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5913 0ebf8283 2019-07-24 stsp {
5914 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5915 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5916 0ebf8283 2019-07-24 stsp size_t size;
5917 0ebf8283 2019-07-24 stsp ssize_t len;
5918 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5919 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5920 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5921 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5922 0ebf8283 2019-07-24 stsp
5923 0ebf8283 2019-07-24 stsp for (;;) {
5924 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5925 0ebf8283 2019-07-24 stsp if (len == -1) {
5926 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5927 0ebf8283 2019-07-24 stsp if (feof(f))
5928 0ebf8283 2019-07-24 stsp break;
5929 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5930 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5931 0ebf8283 2019-07-24 stsp break;
5932 0ebf8283 2019-07-24 stsp }
5933 0ebf8283 2019-07-24 stsp lineno++;
5934 0ebf8283 2019-07-24 stsp p = line;
5935 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5936 0ebf8283 2019-07-24 stsp p++;
5937 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5938 0ebf8283 2019-07-24 stsp free(line);
5939 0ebf8283 2019-07-24 stsp line = NULL;
5940 0ebf8283 2019-07-24 stsp continue;
5941 0ebf8283 2019-07-24 stsp }
5942 0ebf8283 2019-07-24 stsp cmd = NULL;
5943 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5944 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5945 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5946 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5947 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5948 0ebf8283 2019-07-24 stsp break;
5949 0ebf8283 2019-07-24 stsp }
5950 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5951 0ebf8283 2019-07-24 stsp p++;
5952 0ebf8283 2019-07-24 stsp break;
5953 0ebf8283 2019-07-24 stsp }
5954 0ebf8283 2019-07-24 stsp }
5955 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5956 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5957 0ebf8283 2019-07-24 stsp break;
5958 0ebf8283 2019-07-24 stsp }
5959 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5960 0ebf8283 2019-07-24 stsp p++;
5961 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5962 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5963 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5964 0ebf8283 2019-07-24 stsp break;
5965 0ebf8283 2019-07-24 stsp }
5966 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5967 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5968 0ebf8283 2019-07-24 stsp if (err)
5969 0ebf8283 2019-07-24 stsp break;
5970 0ebf8283 2019-07-24 stsp } else {
5971 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5972 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5973 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5974 0ebf8283 2019-07-24 stsp break;
5975 0ebf8283 2019-07-24 stsp }
5976 0ebf8283 2019-07-24 stsp }
5977 0ebf8283 2019-07-24 stsp free(line);
5978 0ebf8283 2019-07-24 stsp line = NULL;
5979 0ebf8283 2019-07-24 stsp continue;
5980 0ebf8283 2019-07-24 stsp } else {
5981 0ebf8283 2019-07-24 stsp end = p;
5982 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
5983 0ebf8283 2019-07-24 stsp end++;
5984 0ebf8283 2019-07-24 stsp *end = '\0';
5985 0ebf8283 2019-07-24 stsp
5986 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
5987 0ebf8283 2019-07-24 stsp if (err) {
5988 0ebf8283 2019-07-24 stsp /* override error code */
5989 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5990 0ebf8283 2019-07-24 stsp break;
5991 0ebf8283 2019-07-24 stsp }
5992 0ebf8283 2019-07-24 stsp }
5993 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
5994 0ebf8283 2019-07-24 stsp if (hle == NULL) {
5995 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
5996 0ebf8283 2019-07-24 stsp break;
5997 0ebf8283 2019-07-24 stsp }
5998 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
5999 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6000 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6001 0ebf8283 2019-07-24 stsp commit_id = NULL;
6002 0ebf8283 2019-07-24 stsp free(line);
6003 0ebf8283 2019-07-24 stsp line = NULL;
6004 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6005 0ebf8283 2019-07-24 stsp }
6006 0ebf8283 2019-07-24 stsp
6007 0ebf8283 2019-07-24 stsp free(line);
6008 0ebf8283 2019-07-24 stsp free(commit_id);
6009 0ebf8283 2019-07-24 stsp return err;
6010 0ebf8283 2019-07-24 stsp }
6011 0ebf8283 2019-07-24 stsp
6012 0ebf8283 2019-07-24 stsp static const struct got_error *
6013 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6014 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6015 bfce7f83 2019-07-27 stsp {
6016 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6017 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6018 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6019 bfce7f83 2019-07-27 stsp static char msg[80];
6020 bfce7f83 2019-07-27 stsp char *id_str;
6021 bfce7f83 2019-07-27 stsp
6022 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6023 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6024 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6025 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6026 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6027 bfce7f83 2019-07-27 stsp
6028 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6029 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6030 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6031 bfce7f83 2019-07-27 stsp break;
6032 bfce7f83 2019-07-27 stsp }
6033 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6034 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6035 bfce7f83 2019-07-27 stsp if (err)
6036 bfce7f83 2019-07-27 stsp return err;
6037 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6038 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6039 bfce7f83 2019-07-27 stsp free(id_str);
6040 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6041 bfce7f83 2019-07-27 stsp }
6042 bfce7f83 2019-07-27 stsp }
6043 bfce7f83 2019-07-27 stsp
6044 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6045 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6046 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6047 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6048 bfce7f83 2019-07-27 stsp
6049 bfce7f83 2019-07-27 stsp return NULL;
6050 bfce7f83 2019-07-27 stsp }
6051 bfce7f83 2019-07-27 stsp
6052 bfce7f83 2019-07-27 stsp static const struct got_error *
6053 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6054 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6055 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6056 0ebf8283 2019-07-24 stsp {
6057 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6058 0ebf8283 2019-07-24 stsp char *editor;
6059 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6060 0ebf8283 2019-07-24 stsp
6061 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6062 0ebf8283 2019-07-24 stsp if (err)
6063 0ebf8283 2019-07-24 stsp return err;
6064 0ebf8283 2019-07-24 stsp
6065 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6066 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6067 0ebf8283 2019-07-24 stsp goto done;
6068 0ebf8283 2019-07-24 stsp }
6069 0ebf8283 2019-07-24 stsp
6070 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6071 0ebf8283 2019-07-24 stsp if (f == NULL) {
6072 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6073 0ebf8283 2019-07-24 stsp goto done;
6074 0ebf8283 2019-07-24 stsp }
6075 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6076 bfce7f83 2019-07-27 stsp if (err)
6077 bfce7f83 2019-07-27 stsp goto done;
6078 bfce7f83 2019-07-27 stsp
6079 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6080 0ebf8283 2019-07-24 stsp done:
6081 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6082 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6083 0ebf8283 2019-07-24 stsp free(editor);
6084 0ebf8283 2019-07-24 stsp return err;
6085 0ebf8283 2019-07-24 stsp }
6086 0ebf8283 2019-07-24 stsp
6087 0ebf8283 2019-07-24 stsp static const struct got_error *
6088 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6089 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6090 514f2ffe 2020-01-29 stsp struct got_repository *);
6091 0ebf8283 2019-07-24 stsp
6092 0ebf8283 2019-07-24 stsp static const struct got_error *
6093 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6094 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6095 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6096 0ebf8283 2019-07-24 stsp {
6097 0ebf8283 2019-07-24 stsp const struct got_error *err;
6098 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6099 0ebf8283 2019-07-24 stsp char *path = NULL;
6100 0ebf8283 2019-07-24 stsp
6101 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6102 0ebf8283 2019-07-24 stsp if (err)
6103 0ebf8283 2019-07-24 stsp return err;
6104 0ebf8283 2019-07-24 stsp
6105 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6106 0ebf8283 2019-07-24 stsp if (err)
6107 0ebf8283 2019-07-24 stsp goto done;
6108 0ebf8283 2019-07-24 stsp
6109 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6110 0ebf8283 2019-07-24 stsp if (err)
6111 0ebf8283 2019-07-24 stsp goto done;
6112 0ebf8283 2019-07-24 stsp
6113 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6114 083957f4 2020-02-24 stsp rewind(f);
6115 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6116 083957f4 2020-02-24 stsp } else {
6117 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6118 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6119 0ebf8283 2019-07-24 stsp goto done;
6120 083957f4 2020-02-24 stsp }
6121 083957f4 2020-02-24 stsp f = NULL;
6122 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6123 083957f4 2020-02-24 stsp if (err) {
6124 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6125 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6126 083957f4 2020-02-24 stsp goto done;
6127 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6128 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6129 083957f4 2020-02-24 stsp }
6130 0ebf8283 2019-07-24 stsp }
6131 0ebf8283 2019-07-24 stsp done:
6132 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6133 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6134 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6135 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6136 0ebf8283 2019-07-24 stsp free(path);
6137 0ebf8283 2019-07-24 stsp return err;
6138 0ebf8283 2019-07-24 stsp }
6139 0ebf8283 2019-07-24 stsp
6140 0ebf8283 2019-07-24 stsp static const struct got_error *
6141 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6142 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6143 0ebf8283 2019-07-24 stsp {
6144 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6145 0ebf8283 2019-07-24 stsp char *path = NULL;
6146 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6147 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6148 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6149 0ebf8283 2019-07-24 stsp
6150 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6151 0ebf8283 2019-07-24 stsp if (err)
6152 0ebf8283 2019-07-24 stsp return err;
6153 0ebf8283 2019-07-24 stsp
6154 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6155 0ebf8283 2019-07-24 stsp if (f == NULL) {
6156 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6157 0ebf8283 2019-07-24 stsp goto done;
6158 0ebf8283 2019-07-24 stsp }
6159 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6160 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6161 0ebf8283 2019-07-24 stsp repo);
6162 0ebf8283 2019-07-24 stsp if (err)
6163 0ebf8283 2019-07-24 stsp break;
6164 0ebf8283 2019-07-24 stsp
6165 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6166 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6167 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6168 0ebf8283 2019-07-24 stsp if (n < 0) {
6169 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6170 0ebf8283 2019-07-24 stsp break;
6171 0ebf8283 2019-07-24 stsp }
6172 0ebf8283 2019-07-24 stsp }
6173 0ebf8283 2019-07-24 stsp }
6174 0ebf8283 2019-07-24 stsp done:
6175 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6176 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6177 0ebf8283 2019-07-24 stsp free(path);
6178 0ebf8283 2019-07-24 stsp if (commit)
6179 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6180 0ebf8283 2019-07-24 stsp return err;
6181 0ebf8283 2019-07-24 stsp }
6182 0ebf8283 2019-07-24 stsp
6183 bfce7f83 2019-07-27 stsp void
6184 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6185 bfce7f83 2019-07-27 stsp {
6186 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6187 bfce7f83 2019-07-27 stsp
6188 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6189 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6190 bfce7f83 2019-07-27 stsp free(hle);
6191 bfce7f83 2019-07-27 stsp }
6192 bfce7f83 2019-07-27 stsp }
6193 bfce7f83 2019-07-27 stsp
6194 0ebf8283 2019-07-24 stsp static const struct got_error *
6195 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6196 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6197 0ebf8283 2019-07-24 stsp {
6198 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6199 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6200 0ebf8283 2019-07-24 stsp
6201 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6202 0ebf8283 2019-07-24 stsp if (f == NULL) {
6203 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6204 0ebf8283 2019-07-24 stsp goto done;
6205 0ebf8283 2019-07-24 stsp }
6206 0ebf8283 2019-07-24 stsp
6207 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6208 0ebf8283 2019-07-24 stsp done:
6209 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6210 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6211 0ebf8283 2019-07-24 stsp return err;
6212 0ebf8283 2019-07-24 stsp }
6213 0ebf8283 2019-07-24 stsp
6214 0ebf8283 2019-07-24 stsp static const struct got_error *
6215 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6216 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6217 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6218 0ebf8283 2019-07-24 stsp {
6219 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6220 0ebf8283 2019-07-24 stsp int resp = ' ';
6221 0ebf8283 2019-07-24 stsp
6222 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6223 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6224 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6225 0ebf8283 2019-07-24 stsp resp = getchar();
6226 a61a4414 2019-08-07 stsp if (resp == '\n')
6227 a61a4414 2019-08-07 stsp resp = getchar();
6228 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6229 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6230 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6231 bfce7f83 2019-07-27 stsp repo);
6232 426ebf2e 2019-07-25 stsp if (err) {
6233 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6234 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6235 426ebf2e 2019-07-25 stsp break;
6236 bfce7f83 2019-07-27 stsp prev_err = err;
6237 426ebf2e 2019-07-25 stsp resp = ' ';
6238 426ebf2e 2019-07-25 stsp continue;
6239 426ebf2e 2019-07-25 stsp }
6240 bfce7f83 2019-07-27 stsp break;
6241 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6242 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6243 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6244 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6245 426ebf2e 2019-07-25 stsp if (err) {
6246 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6247 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6248 426ebf2e 2019-07-25 stsp break;
6249 bfce7f83 2019-07-27 stsp prev_err = err;
6250 426ebf2e 2019-07-25 stsp resp = ' ';
6251 426ebf2e 2019-07-25 stsp continue;
6252 426ebf2e 2019-07-25 stsp }
6253 bfce7f83 2019-07-27 stsp break;
6254 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6255 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6256 0ebf8283 2019-07-24 stsp break;
6257 426ebf2e 2019-07-25 stsp } else
6258 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6259 0ebf8283 2019-07-24 stsp }
6260 0ebf8283 2019-07-24 stsp
6261 0ebf8283 2019-07-24 stsp return err;
6262 0ebf8283 2019-07-24 stsp }
6263 0ebf8283 2019-07-24 stsp
6264 0ebf8283 2019-07-24 stsp static const struct got_error *
6265 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6266 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6267 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6268 0ebf8283 2019-07-24 stsp {
6269 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6270 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6271 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6272 3e3a69f1 2019-07-25 stsp branch, repo);
6273 0ebf8283 2019-07-24 stsp }
6274 0ebf8283 2019-07-24 stsp
6275 0ebf8283 2019-07-24 stsp static const struct got_error *
6276 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6277 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6278 0ebf8283 2019-07-24 stsp {
6279 0ebf8283 2019-07-24 stsp const struct got_error *err;
6280 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6281 0ebf8283 2019-07-24 stsp
6282 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6283 0ebf8283 2019-07-24 stsp if (err)
6284 0ebf8283 2019-07-24 stsp goto done;
6285 0ebf8283 2019-07-24 stsp
6286 0ebf8283 2019-07-24 stsp if (new_id) {
6287 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6288 0ebf8283 2019-07-24 stsp if (err)
6289 0ebf8283 2019-07-24 stsp goto done;
6290 0ebf8283 2019-07-24 stsp }
6291 0ebf8283 2019-07-24 stsp
6292 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6293 0ebf8283 2019-07-24 stsp if (new_id_str)
6294 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6295 0ebf8283 2019-07-24 stsp
6296 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6297 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6298 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6299 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6300 0ebf8283 2019-07-24 stsp goto done;
6301 0ebf8283 2019-07-24 stsp }
6302 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6303 0ebf8283 2019-07-24 stsp } else {
6304 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6305 0ebf8283 2019-07-24 stsp if (err)
6306 0ebf8283 2019-07-24 stsp goto done;
6307 0ebf8283 2019-07-24 stsp }
6308 0ebf8283 2019-07-24 stsp
6309 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6310 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6311 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6312 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6313 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6314 0ebf8283 2019-07-24 stsp break;
6315 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6316 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6317 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6318 0ebf8283 2019-07-24 stsp logmsg);
6319 0ebf8283 2019-07-24 stsp break;
6320 0ebf8283 2019-07-24 stsp default:
6321 0ebf8283 2019-07-24 stsp break;
6322 0ebf8283 2019-07-24 stsp }
6323 0ebf8283 2019-07-24 stsp done:
6324 0ebf8283 2019-07-24 stsp free(old_id_str);
6325 0ebf8283 2019-07-24 stsp free(new_id_str);
6326 0ebf8283 2019-07-24 stsp return err;
6327 0ebf8283 2019-07-24 stsp }
6328 0ebf8283 2019-07-24 stsp
6329 0ebf8283 2019-07-24 stsp static const struct got_error *
6330 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6331 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6332 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6333 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6334 0ebf8283 2019-07-24 stsp {
6335 0ebf8283 2019-07-24 stsp const struct got_error *err;
6336 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6337 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6338 0ebf8283 2019-07-24 stsp
6339 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6340 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6341 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6342 0ebf8283 2019-07-24 stsp if (err)
6343 0ebf8283 2019-07-24 stsp return err;
6344 0ebf8283 2019-07-24 stsp }
6345 0ebf8283 2019-07-24 stsp
6346 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6347 0ebf8283 2019-07-24 stsp if (err)
6348 0ebf8283 2019-07-24 stsp return err;
6349 0ebf8283 2019-07-24 stsp
6350 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6351 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6352 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6353 0ebf8283 2019-07-24 stsp if (err) {
6354 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6355 0ebf8283 2019-07-24 stsp goto done;
6356 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6357 0ebf8283 2019-07-24 stsp } else {
6358 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6359 0ebf8283 2019-07-24 stsp free(new_commit_id);
6360 0ebf8283 2019-07-24 stsp }
6361 0ebf8283 2019-07-24 stsp done:
6362 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6363 0ebf8283 2019-07-24 stsp return err;
6364 0ebf8283 2019-07-24 stsp }
6365 0ebf8283 2019-07-24 stsp
6366 0ebf8283 2019-07-24 stsp static const struct got_error *
6367 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6368 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6369 0ebf8283 2019-07-24 stsp {
6370 0ebf8283 2019-07-24 stsp const struct got_error *error;
6371 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6372 0ebf8283 2019-07-24 stsp
6373 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6374 0ebf8283 2019-07-24 stsp repo);
6375 0ebf8283 2019-07-24 stsp if (error)
6376 0ebf8283 2019-07-24 stsp return error;
6377 0ebf8283 2019-07-24 stsp
6378 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6379 0ebf8283 2019-07-24 stsp if (error)
6380 0ebf8283 2019-07-24 stsp return error;
6381 0ebf8283 2019-07-24 stsp
6382 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6383 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6384 0ebf8283 2019-07-24 stsp return error;
6385 ab20a43a 2020-01-29 stsp }
6386 ab20a43a 2020-01-29 stsp
6387 ab20a43a 2020-01-29 stsp static const struct got_error *
6388 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6389 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6390 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6391 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6392 ab20a43a 2020-01-29 stsp {
6393 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6394 ab20a43a 2020-01-29 stsp
6395 ab20a43a 2020-01-29 stsp switch (status) {
6396 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6397 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6398 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6399 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6400 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6401 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6402 ab20a43a 2020-01-29 stsp default:
6403 ab20a43a 2020-01-29 stsp break;
6404 ab20a43a 2020-01-29 stsp }
6405 ab20a43a 2020-01-29 stsp
6406 ab20a43a 2020-01-29 stsp switch (staged_status) {
6407 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6408 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6409 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6410 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6411 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6412 ab20a43a 2020-01-29 stsp default:
6413 ab20a43a 2020-01-29 stsp break;
6414 ab20a43a 2020-01-29 stsp }
6415 ab20a43a 2020-01-29 stsp
6416 ab20a43a 2020-01-29 stsp return NULL;
6417 0ebf8283 2019-07-24 stsp }
6418 0ebf8283 2019-07-24 stsp
6419 0ebf8283 2019-07-24 stsp static const struct got_error *
6420 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6421 0ebf8283 2019-07-24 stsp {
6422 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6423 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6424 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6425 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6426 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6427 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6428 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6429 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6430 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6431 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6432 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6433 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6434 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6435 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6436 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6437 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6438 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6439 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6440 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6441 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6442 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6443 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6444 0ebf8283 2019-07-24 stsp
6445 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6446 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6447 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6448 0ebf8283 2019-07-24 stsp
6449 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6450 0ebf8283 2019-07-24 stsp switch (ch) {
6451 0ebf8283 2019-07-24 stsp case 'a':
6452 0ebf8283 2019-07-24 stsp abort_edit = 1;
6453 0ebf8283 2019-07-24 stsp break;
6454 0ebf8283 2019-07-24 stsp case 'c':
6455 0ebf8283 2019-07-24 stsp continue_edit = 1;
6456 0ebf8283 2019-07-24 stsp break;
6457 0ebf8283 2019-07-24 stsp case 'F':
6458 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6459 0ebf8283 2019-07-24 stsp break;
6460 083957f4 2020-02-24 stsp case 'm':
6461 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6462 083957f4 2020-02-24 stsp break;
6463 0ebf8283 2019-07-24 stsp default:
6464 0ebf8283 2019-07-24 stsp usage_histedit();
6465 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6466 0ebf8283 2019-07-24 stsp }
6467 0ebf8283 2019-07-24 stsp }
6468 0ebf8283 2019-07-24 stsp
6469 0ebf8283 2019-07-24 stsp argc -= optind;
6470 0ebf8283 2019-07-24 stsp argv += optind;
6471 0ebf8283 2019-07-24 stsp
6472 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6473 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6474 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6475 0ebf8283 2019-07-24 stsp err(1, "pledge");
6476 0ebf8283 2019-07-24 stsp #endif
6477 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6478 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6479 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6480 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6481 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6482 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6483 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6484 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6485 0ebf8283 2019-07-24 stsp if (argc != 0)
6486 0ebf8283 2019-07-24 stsp usage_histedit();
6487 0ebf8283 2019-07-24 stsp
6488 0ebf8283 2019-07-24 stsp /*
6489 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6490 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6491 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6492 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6493 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6494 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6495 0ebf8283 2019-07-24 stsp */
6496 0ebf8283 2019-07-24 stsp
6497 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6498 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6499 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6500 0ebf8283 2019-07-24 stsp goto done;
6501 0ebf8283 2019-07-24 stsp }
6502 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6503 0ebf8283 2019-07-24 stsp if (error)
6504 0ebf8283 2019-07-24 stsp goto done;
6505 0ebf8283 2019-07-24 stsp
6506 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6507 c9956ddf 2019-09-08 stsp NULL);
6508 0ebf8283 2019-07-24 stsp if (error != NULL)
6509 0ebf8283 2019-07-24 stsp goto done;
6510 0ebf8283 2019-07-24 stsp
6511 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6512 0ebf8283 2019-07-24 stsp if (error)
6513 0ebf8283 2019-07-24 stsp goto done;
6514 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6515 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6516 0ebf8283 2019-07-24 stsp goto done;
6517 0ebf8283 2019-07-24 stsp }
6518 0ebf8283 2019-07-24 stsp
6519 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6520 0ebf8283 2019-07-24 stsp if (error)
6521 0ebf8283 2019-07-24 stsp goto done;
6522 0ebf8283 2019-07-24 stsp
6523 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6524 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6525 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6526 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6527 083957f4 2020-02-24 stsp "before the -m option can be used");
6528 083957f4 2020-02-24 stsp goto done;
6529 083957f4 2020-02-24 stsp }
6530 083957f4 2020-02-24 stsp
6531 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6532 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6533 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6534 3e3a69f1 2019-07-25 stsp worktree, repo);
6535 0ebf8283 2019-07-24 stsp if (error)
6536 0ebf8283 2019-07-24 stsp goto done;
6537 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6538 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6539 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6540 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6541 0ebf8283 2019-07-24 stsp if (error)
6542 0ebf8283 2019-07-24 stsp goto done;
6543 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6544 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6545 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6546 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6547 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6548 0ebf8283 2019-07-24 stsp goto done;
6549 0ebf8283 2019-07-24 stsp }
6550 0ebf8283 2019-07-24 stsp
6551 0ebf8283 2019-07-24 stsp if (continue_edit) {
6552 0ebf8283 2019-07-24 stsp char *path;
6553 0ebf8283 2019-07-24 stsp
6554 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6555 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6556 0ebf8283 2019-07-24 stsp goto done;
6557 0ebf8283 2019-07-24 stsp }
6558 0ebf8283 2019-07-24 stsp
6559 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6560 0ebf8283 2019-07-24 stsp if (error)
6561 0ebf8283 2019-07-24 stsp goto done;
6562 0ebf8283 2019-07-24 stsp
6563 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6564 0ebf8283 2019-07-24 stsp free(path);
6565 0ebf8283 2019-07-24 stsp if (error)
6566 0ebf8283 2019-07-24 stsp goto done;
6567 0ebf8283 2019-07-24 stsp
6568 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6569 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6570 3e3a69f1 2019-07-25 stsp worktree, repo);
6571 0ebf8283 2019-07-24 stsp if (error)
6572 0ebf8283 2019-07-24 stsp goto done;
6573 0ebf8283 2019-07-24 stsp
6574 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
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 = got_object_open_as_commit(&commit, repo,
6579 0ebf8283 2019-07-24 stsp head_commit_id);
6580 0ebf8283 2019-07-24 stsp if (error)
6581 0ebf8283 2019-07-24 stsp goto done;
6582 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6583 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6584 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6585 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6586 3aac7cf7 2019-07-25 stsp goto done;
6587 3aac7cf7 2019-07-25 stsp }
6588 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6589 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6590 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6591 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6592 0ebf8283 2019-07-24 stsp commit = NULL;
6593 0ebf8283 2019-07-24 stsp if (error)
6594 0ebf8283 2019-07-24 stsp goto done;
6595 0ebf8283 2019-07-24 stsp } else {
6596 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6597 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6598 0ebf8283 2019-07-24 stsp goto done;
6599 0ebf8283 2019-07-24 stsp }
6600 0ebf8283 2019-07-24 stsp
6601 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6602 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6603 0ebf8283 2019-07-24 stsp if (error != NULL)
6604 0ebf8283 2019-07-24 stsp goto done;
6605 0ebf8283 2019-07-24 stsp
6606 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6607 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6608 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6609 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6610 c7d20a3f 2019-07-30 stsp goto done;
6611 c7d20a3f 2019-07-30 stsp }
6612 c7d20a3f 2019-07-30 stsp
6613 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6614 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6615 bfce7f83 2019-07-27 stsp branch = NULL;
6616 0ebf8283 2019-07-24 stsp if (error)
6617 0ebf8283 2019-07-24 stsp goto done;
6618 0ebf8283 2019-07-24 stsp
6619 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6620 0ebf8283 2019-07-24 stsp head_commit_id);
6621 0ebf8283 2019-07-24 stsp if (error)
6622 0ebf8283 2019-07-24 stsp goto done;
6623 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6624 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6625 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6626 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6627 3aac7cf7 2019-07-25 stsp goto done;
6628 3aac7cf7 2019-07-25 stsp }
6629 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6630 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6631 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6632 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6633 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6634 0ebf8283 2019-07-24 stsp commit = NULL;
6635 0ebf8283 2019-07-24 stsp if (error)
6636 0ebf8283 2019-07-24 stsp goto done;
6637 0ebf8283 2019-07-24 stsp
6638 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
6639 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6640 c5996fff 2020-01-29 stsp goto done;
6641 c5996fff 2020-01-29 stsp }
6642 c5996fff 2020-01-29 stsp
6643 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6644 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6645 bfce7f83 2019-07-27 stsp if (error)
6646 bfce7f83 2019-07-27 stsp goto done;
6647 bfce7f83 2019-07-27 stsp
6648 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6649 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6650 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6651 6ba2bea2 2019-07-27 stsp if (error) {
6652 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6653 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6654 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6655 0ebf8283 2019-07-24 stsp goto done;
6656 6ba2bea2 2019-07-27 stsp }
6657 0ebf8283 2019-07-24 stsp } else {
6658 514f2ffe 2020-01-29 stsp const char *branch_name;
6659 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
6660 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6661 514f2ffe 2020-01-29 stsp branch_name += 11;
6662 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6663 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
6664 6ba2bea2 2019-07-27 stsp if (error) {
6665 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6666 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6667 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6668 0ebf8283 2019-07-24 stsp goto done;
6669 6ba2bea2 2019-07-27 stsp }
6670 0ebf8283 2019-07-24 stsp
6671 0ebf8283 2019-07-24 stsp }
6672 0ebf8283 2019-07-24 stsp
6673 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6674 0ebf8283 2019-07-24 stsp repo);
6675 6ba2bea2 2019-07-27 stsp if (error) {
6676 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6677 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6678 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6679 0ebf8283 2019-07-24 stsp goto done;
6680 6ba2bea2 2019-07-27 stsp }
6681 0ebf8283 2019-07-24 stsp
6682 0ebf8283 2019-07-24 stsp }
6683 0ebf8283 2019-07-24 stsp
6684 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6685 4ec14e60 2019-08-28 hiltjo if (error)
6686 0ebf8283 2019-07-24 stsp goto done;
6687 0ebf8283 2019-07-24 stsp
6688 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6689 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6690 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6691 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6692 0ebf8283 2019-07-24 stsp continue;
6693 0ebf8283 2019-07-24 stsp
6694 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6695 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6696 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6697 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6698 0ebf8283 2019-07-24 stsp repo);
6699 ab20a43a 2020-01-29 stsp if (error)
6700 ab20a43a 2020-01-29 stsp goto done;
6701 0ebf8283 2019-07-24 stsp } else {
6702 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
6703 ab20a43a 2020-01-29 stsp int have_changes = 0;
6704 ab20a43a 2020-01-29 stsp
6705 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
6706 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
6707 ab20a43a 2020-01-29 stsp if (error)
6708 ab20a43a 2020-01-29 stsp goto done;
6709 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
6710 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
6711 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
6712 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
6713 ab20a43a 2020-01-29 stsp if (error) {
6714 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
6715 ab20a43a 2020-01-29 stsp goto done;
6716 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
6717 ab20a43a 2020-01-29 stsp goto done;
6718 ab20a43a 2020-01-29 stsp }
6719 ab20a43a 2020-01-29 stsp if (have_changes) {
6720 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
6721 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
6722 ab20a43a 2020-01-29 stsp if (error)
6723 ab20a43a 2020-01-29 stsp goto done;
6724 ab20a43a 2020-01-29 stsp } else {
6725 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
6726 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
6727 ab20a43a 2020-01-29 stsp if (error)
6728 ab20a43a 2020-01-29 stsp goto done;
6729 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
6730 ab20a43a 2020-01-29 stsp hle, NULL);
6731 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
6732 ab20a43a 2020-01-29 stsp commit = NULL;
6733 ab20a43a 2020-01-29 stsp if (error)
6734 ab20a43a 2020-01-29 stsp goto done;
6735 ab20a43a 2020-01-29 stsp }
6736 0ebf8283 2019-07-24 stsp }
6737 0ebf8283 2019-07-24 stsp continue;
6738 0ebf8283 2019-07-24 stsp }
6739 0ebf8283 2019-07-24 stsp
6740 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6741 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6742 0ebf8283 2019-07-24 stsp if (error)
6743 0ebf8283 2019-07-24 stsp goto done;
6744 0ebf8283 2019-07-24 stsp continue;
6745 0ebf8283 2019-07-24 stsp }
6746 0ebf8283 2019-07-24 stsp
6747 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6748 0ebf8283 2019-07-24 stsp hle->commit_id);
6749 0ebf8283 2019-07-24 stsp if (error)
6750 0ebf8283 2019-07-24 stsp goto done;
6751 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6752 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6753 0ebf8283 2019-07-24 stsp
6754 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6755 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6756 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6757 0ebf8283 2019-07-24 stsp if (error)
6758 0ebf8283 2019-07-24 stsp goto done;
6759 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6760 0ebf8283 2019-07-24 stsp commit = NULL;
6761 0ebf8283 2019-07-24 stsp
6762 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6763 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6764 0ebf8283 2019-07-24 stsp break;
6765 0ebf8283 2019-07-24 stsp }
6766 0ebf8283 2019-07-24 stsp
6767 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6768 0ebf8283 2019-07-24 stsp char *id_str;
6769 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6770 0ebf8283 2019-07-24 stsp if (error)
6771 0ebf8283 2019-07-24 stsp goto done;
6772 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6773 0ebf8283 2019-07-24 stsp id_str);
6774 0ebf8283 2019-07-24 stsp free(id_str);
6775 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6776 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6777 3e3a69f1 2019-07-25 stsp fileindex);
6778 0ebf8283 2019-07-24 stsp goto done;
6779 0ebf8283 2019-07-24 stsp }
6780 0ebf8283 2019-07-24 stsp
6781 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6782 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6783 0ebf8283 2019-07-24 stsp if (error)
6784 0ebf8283 2019-07-24 stsp goto done;
6785 0ebf8283 2019-07-24 stsp continue;
6786 0ebf8283 2019-07-24 stsp }
6787 0ebf8283 2019-07-24 stsp
6788 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6789 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6790 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6791 0ebf8283 2019-07-24 stsp if (error)
6792 0ebf8283 2019-07-24 stsp goto done;
6793 0ebf8283 2019-07-24 stsp }
6794 0ebf8283 2019-07-24 stsp
6795 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6796 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6797 0ebf8283 2019-07-24 stsp if (error)
6798 0ebf8283 2019-07-24 stsp goto done;
6799 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6800 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6801 0ebf8283 2019-07-24 stsp } else
6802 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6803 3e3a69f1 2019-07-25 stsp branch, repo);
6804 0ebf8283 2019-07-24 stsp done:
6805 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6806 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6807 0ebf8283 2019-07-24 stsp free(head_commit_id);
6808 0ebf8283 2019-07-24 stsp free(base_commit_id);
6809 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6810 0ebf8283 2019-07-24 stsp if (commit)
6811 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6812 0ebf8283 2019-07-24 stsp if (branch)
6813 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6814 0ebf8283 2019-07-24 stsp if (tmp_branch)
6815 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6816 0ebf8283 2019-07-24 stsp if (worktree)
6817 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6818 2822a352 2019-10-15 stsp if (repo)
6819 2822a352 2019-10-15 stsp got_repo_close(repo);
6820 2822a352 2019-10-15 stsp return error;
6821 2822a352 2019-10-15 stsp }
6822 2822a352 2019-10-15 stsp
6823 2822a352 2019-10-15 stsp __dead static void
6824 2822a352 2019-10-15 stsp usage_integrate(void)
6825 2822a352 2019-10-15 stsp {
6826 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6827 2822a352 2019-10-15 stsp exit(1);
6828 2822a352 2019-10-15 stsp }
6829 2822a352 2019-10-15 stsp
6830 2822a352 2019-10-15 stsp static const struct got_error *
6831 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6832 2822a352 2019-10-15 stsp {
6833 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6834 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6835 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6836 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6837 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6838 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6839 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6840 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6841 2822a352 2019-10-15 stsp int ch, did_something = 0;
6842 2822a352 2019-10-15 stsp
6843 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6844 2822a352 2019-10-15 stsp switch (ch) {
6845 2822a352 2019-10-15 stsp default:
6846 2822a352 2019-10-15 stsp usage_integrate();
6847 2822a352 2019-10-15 stsp /* NOTREACHED */
6848 2822a352 2019-10-15 stsp }
6849 2822a352 2019-10-15 stsp }
6850 2822a352 2019-10-15 stsp
6851 2822a352 2019-10-15 stsp argc -= optind;
6852 2822a352 2019-10-15 stsp argv += optind;
6853 2822a352 2019-10-15 stsp
6854 2822a352 2019-10-15 stsp if (argc != 1)
6855 2822a352 2019-10-15 stsp usage_integrate();
6856 2822a352 2019-10-15 stsp branch_arg = argv[0];
6857 2822a352 2019-10-15 stsp
6858 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6859 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6860 2822a352 2019-10-15 stsp err(1, "pledge");
6861 2822a352 2019-10-15 stsp
6862 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6863 2822a352 2019-10-15 stsp if (cwd == NULL) {
6864 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6865 2822a352 2019-10-15 stsp goto done;
6866 2822a352 2019-10-15 stsp }
6867 2822a352 2019-10-15 stsp
6868 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6869 2822a352 2019-10-15 stsp if (error)
6870 2822a352 2019-10-15 stsp goto done;
6871 2822a352 2019-10-15 stsp
6872 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6873 2822a352 2019-10-15 stsp if (error)
6874 2822a352 2019-10-15 stsp goto done;
6875 2822a352 2019-10-15 stsp
6876 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6877 2822a352 2019-10-15 stsp NULL);
6878 2822a352 2019-10-15 stsp if (error != NULL)
6879 2822a352 2019-10-15 stsp goto done;
6880 2822a352 2019-10-15 stsp
6881 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6882 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6883 2822a352 2019-10-15 stsp if (error)
6884 2822a352 2019-10-15 stsp goto done;
6885 2822a352 2019-10-15 stsp
6886 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6887 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6888 2822a352 2019-10-15 stsp goto done;
6889 2822a352 2019-10-15 stsp }
6890 2822a352 2019-10-15 stsp
6891 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6892 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6893 2822a352 2019-10-15 stsp if (error)
6894 2822a352 2019-10-15 stsp goto done;
6895 2822a352 2019-10-15 stsp
6896 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6897 2822a352 2019-10-15 stsp if (refname == NULL) {
6898 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6899 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6900 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6901 2822a352 2019-10-15 stsp goto done;
6902 2822a352 2019-10-15 stsp }
6903 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6904 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6905 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6906 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6907 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6908 2822a352 2019-10-15 stsp goto done;
6909 2822a352 2019-10-15 stsp }
6910 2822a352 2019-10-15 stsp
6911 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6912 2822a352 2019-10-15 stsp if (error)
6913 2822a352 2019-10-15 stsp goto done;
6914 2822a352 2019-10-15 stsp
6915 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6916 2822a352 2019-10-15 stsp if (error)
6917 2822a352 2019-10-15 stsp goto done;
6918 2822a352 2019-10-15 stsp
6919 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6920 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6921 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6922 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6923 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6924 2822a352 2019-10-15 stsp goto done;
6925 2822a352 2019-10-15 stsp }
6926 2822a352 2019-10-15 stsp
6927 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6928 2822a352 2019-10-15 stsp if (error) {
6929 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
6930 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
6931 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6932 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6933 2822a352 2019-10-15 stsp goto done;
6934 2822a352 2019-10-15 stsp }
6935 2822a352 2019-10-15 stsp
6936 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
6937 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
6938 2822a352 2019-10-15 stsp check_cancelled, NULL);
6939 2822a352 2019-10-15 stsp if (error)
6940 2822a352 2019-10-15 stsp goto done;
6941 2822a352 2019-10-15 stsp
6942 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
6943 2822a352 2019-10-15 stsp done:
6944 715dc77e 2019-08-03 stsp if (repo)
6945 715dc77e 2019-08-03 stsp got_repo_close(repo);
6946 2822a352 2019-10-15 stsp if (worktree)
6947 2822a352 2019-10-15 stsp got_worktree_close(worktree);
6948 2822a352 2019-10-15 stsp free(cwd);
6949 2822a352 2019-10-15 stsp free(base_commit_id);
6950 2822a352 2019-10-15 stsp free(commit_id);
6951 2822a352 2019-10-15 stsp free(refname);
6952 2822a352 2019-10-15 stsp free(base_refname);
6953 715dc77e 2019-08-03 stsp return error;
6954 715dc77e 2019-08-03 stsp }
6955 715dc77e 2019-08-03 stsp
6956 715dc77e 2019-08-03 stsp __dead static void
6957 715dc77e 2019-08-03 stsp usage_stage(void)
6958 715dc77e 2019-08-03 stsp {
6959 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6960 f3055044 2019-08-07 stsp "[file-path ...]\n",
6961 715dc77e 2019-08-03 stsp getprogname());
6962 715dc77e 2019-08-03 stsp exit(1);
6963 715dc77e 2019-08-03 stsp }
6964 715dc77e 2019-08-03 stsp
6965 715dc77e 2019-08-03 stsp static const struct got_error *
6966 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6967 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6968 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6969 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6970 408b4ebc 2019-08-03 stsp {
6971 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6972 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6973 408b4ebc 2019-08-03 stsp
6974 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6975 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6976 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6977 408b4ebc 2019-08-03 stsp return NULL;
6978 408b4ebc 2019-08-03 stsp
6979 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6980 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6981 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6982 408b4ebc 2019-08-03 stsp else
6983 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6984 408b4ebc 2019-08-03 stsp if (err)
6985 408b4ebc 2019-08-03 stsp return err;
6986 408b4ebc 2019-08-03 stsp
6987 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6988 408b4ebc 2019-08-03 stsp free(id_str);
6989 dc424a06 2019-08-07 stsp return NULL;
6990 dc424a06 2019-08-07 stsp }
6991 2e1f37b0 2019-08-08 stsp
6992 dc424a06 2019-08-07 stsp static const struct got_error *
6993 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6994 715dc77e 2019-08-03 stsp {
6995 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6996 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6997 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6998 715dc77e 2019-08-03 stsp char *cwd = NULL;
6999 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7000 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7001 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7002 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7003 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7004 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7005 715dc77e 2019-08-03 stsp
7006 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7007 715dc77e 2019-08-03 stsp
7008 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7009 715dc77e 2019-08-03 stsp switch (ch) {
7010 408b4ebc 2019-08-03 stsp case 'l':
7011 408b4ebc 2019-08-03 stsp list_stage = 1;
7012 408b4ebc 2019-08-03 stsp break;
7013 dc424a06 2019-08-07 stsp case 'p':
7014 dc424a06 2019-08-07 stsp pflag = 1;
7015 dc424a06 2019-08-07 stsp break;
7016 dc424a06 2019-08-07 stsp case 'F':
7017 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7018 dc424a06 2019-08-07 stsp break;
7019 715dc77e 2019-08-03 stsp default:
7020 715dc77e 2019-08-03 stsp usage_stage();
7021 715dc77e 2019-08-03 stsp /* NOTREACHED */
7022 715dc77e 2019-08-03 stsp }
7023 715dc77e 2019-08-03 stsp }
7024 715dc77e 2019-08-03 stsp
7025 715dc77e 2019-08-03 stsp argc -= optind;
7026 715dc77e 2019-08-03 stsp argv += optind;
7027 715dc77e 2019-08-03 stsp
7028 715dc77e 2019-08-03 stsp #ifndef PROFILE
7029 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7030 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7031 715dc77e 2019-08-03 stsp err(1, "pledge");
7032 715dc77e 2019-08-03 stsp #endif
7033 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7034 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7035 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7036 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7037 715dc77e 2019-08-03 stsp
7038 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7039 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7040 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7041 715dc77e 2019-08-03 stsp goto done;
7042 715dc77e 2019-08-03 stsp }
7043 715dc77e 2019-08-03 stsp
7044 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7045 715dc77e 2019-08-03 stsp if (error)
7046 715dc77e 2019-08-03 stsp goto done;
7047 715dc77e 2019-08-03 stsp
7048 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7049 c9956ddf 2019-09-08 stsp NULL);
7050 715dc77e 2019-08-03 stsp if (error != NULL)
7051 715dc77e 2019-08-03 stsp goto done;
7052 715dc77e 2019-08-03 stsp
7053 dc424a06 2019-08-07 stsp if (patch_script_path) {
7054 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7055 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7056 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7057 dc424a06 2019-08-07 stsp patch_script_path);
7058 dc424a06 2019-08-07 stsp goto done;
7059 dc424a06 2019-08-07 stsp }
7060 dc424a06 2019-08-07 stsp }
7061 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7062 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7063 715dc77e 2019-08-03 stsp if (error)
7064 715dc77e 2019-08-03 stsp goto done;
7065 715dc77e 2019-08-03 stsp
7066 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7067 6d022e97 2019-08-04 stsp if (error)
7068 6d022e97 2019-08-04 stsp goto done;
7069 715dc77e 2019-08-03 stsp
7070 6d022e97 2019-08-04 stsp if (list_stage)
7071 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7072 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7073 2e1f37b0 2019-08-08 stsp else {
7074 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7075 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7076 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7077 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7078 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7079 2e1f37b0 2019-08-08 stsp }
7080 ad493afc 2019-08-03 stsp done:
7081 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7082 2e1f37b0 2019-08-08 stsp error == NULL)
7083 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7084 ad493afc 2019-08-03 stsp if (repo)
7085 ad493afc 2019-08-03 stsp got_repo_close(repo);
7086 ad493afc 2019-08-03 stsp if (worktree)
7087 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7088 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7089 ad493afc 2019-08-03 stsp free((char *)pe->path);
7090 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7091 ad493afc 2019-08-03 stsp free(cwd);
7092 ad493afc 2019-08-03 stsp return error;
7093 ad493afc 2019-08-03 stsp }
7094 ad493afc 2019-08-03 stsp
7095 ad493afc 2019-08-03 stsp __dead static void
7096 ad493afc 2019-08-03 stsp usage_unstage(void)
7097 ad493afc 2019-08-03 stsp {
7098 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7099 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7100 ad493afc 2019-08-03 stsp getprogname());
7101 ad493afc 2019-08-03 stsp exit(1);
7102 ad493afc 2019-08-03 stsp }
7103 ad493afc 2019-08-03 stsp
7104 ad493afc 2019-08-03 stsp
7105 ad493afc 2019-08-03 stsp static const struct got_error *
7106 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7107 ad493afc 2019-08-03 stsp {
7108 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7109 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7110 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7111 ad493afc 2019-08-03 stsp char *cwd = NULL;
7112 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7113 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7114 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7115 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7116 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7117 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7118 ad493afc 2019-08-03 stsp
7119 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7120 ad493afc 2019-08-03 stsp
7121 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7122 ad493afc 2019-08-03 stsp switch (ch) {
7123 2e1f37b0 2019-08-08 stsp case 'p':
7124 2e1f37b0 2019-08-08 stsp pflag = 1;
7125 2e1f37b0 2019-08-08 stsp break;
7126 2e1f37b0 2019-08-08 stsp case 'F':
7127 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7128 2e1f37b0 2019-08-08 stsp break;
7129 ad493afc 2019-08-03 stsp default:
7130 ad493afc 2019-08-03 stsp usage_unstage();
7131 ad493afc 2019-08-03 stsp /* NOTREACHED */
7132 ad493afc 2019-08-03 stsp }
7133 ad493afc 2019-08-03 stsp }
7134 ad493afc 2019-08-03 stsp
7135 ad493afc 2019-08-03 stsp argc -= optind;
7136 ad493afc 2019-08-03 stsp argv += optind;
7137 ad493afc 2019-08-03 stsp
7138 ad493afc 2019-08-03 stsp #ifndef PROFILE
7139 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7140 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7141 ad493afc 2019-08-03 stsp err(1, "pledge");
7142 ad493afc 2019-08-03 stsp #endif
7143 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7144 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7145 2e1f37b0 2019-08-08 stsp
7146 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7147 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7148 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7149 ad493afc 2019-08-03 stsp goto done;
7150 ad493afc 2019-08-03 stsp }
7151 ad493afc 2019-08-03 stsp
7152 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7153 ad493afc 2019-08-03 stsp if (error)
7154 ad493afc 2019-08-03 stsp goto done;
7155 ad493afc 2019-08-03 stsp
7156 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7157 c9956ddf 2019-09-08 stsp NULL);
7158 ad493afc 2019-08-03 stsp if (error != NULL)
7159 ad493afc 2019-08-03 stsp goto done;
7160 ad493afc 2019-08-03 stsp
7161 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7162 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7163 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7164 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7165 2e1f37b0 2019-08-08 stsp patch_script_path);
7166 2e1f37b0 2019-08-08 stsp goto done;
7167 2e1f37b0 2019-08-08 stsp }
7168 2e1f37b0 2019-08-08 stsp }
7169 2e1f37b0 2019-08-08 stsp
7170 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7171 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7172 ad493afc 2019-08-03 stsp if (error)
7173 ad493afc 2019-08-03 stsp goto done;
7174 ad493afc 2019-08-03 stsp
7175 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7176 ad493afc 2019-08-03 stsp if (error)
7177 ad493afc 2019-08-03 stsp goto done;
7178 ad493afc 2019-08-03 stsp
7179 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7180 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7181 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7182 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7183 715dc77e 2019-08-03 stsp done:
7184 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7185 2e1f37b0 2019-08-08 stsp error == NULL)
7186 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7187 0ebf8283 2019-07-24 stsp if (repo)
7188 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7189 715dc77e 2019-08-03 stsp if (worktree)
7190 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7191 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7192 715dc77e 2019-08-03 stsp free((char *)pe->path);
7193 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7194 715dc77e 2019-08-03 stsp free(cwd);
7195 01073a5d 2019-08-22 stsp return error;
7196 01073a5d 2019-08-22 stsp }
7197 01073a5d 2019-08-22 stsp
7198 01073a5d 2019-08-22 stsp __dead static void
7199 01073a5d 2019-08-22 stsp usage_cat(void)
7200 01073a5d 2019-08-22 stsp {
7201 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7202 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7203 01073a5d 2019-08-22 stsp exit(1);
7204 01073a5d 2019-08-22 stsp }
7205 01073a5d 2019-08-22 stsp
7206 01073a5d 2019-08-22 stsp static const struct got_error *
7207 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7208 01073a5d 2019-08-22 stsp {
7209 01073a5d 2019-08-22 stsp const struct got_error *err;
7210 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7211 01073a5d 2019-08-22 stsp
7212 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7213 01073a5d 2019-08-22 stsp if (err)
7214 01073a5d 2019-08-22 stsp return err;
7215 01073a5d 2019-08-22 stsp
7216 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7217 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7218 01073a5d 2019-08-22 stsp return err;
7219 01073a5d 2019-08-22 stsp }
7220 01073a5d 2019-08-22 stsp
7221 01073a5d 2019-08-22 stsp static const struct got_error *
7222 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7223 01073a5d 2019-08-22 stsp {
7224 01073a5d 2019-08-22 stsp const struct got_error *err;
7225 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7226 56e0773d 2019-11-28 stsp int nentries, i;
7227 01073a5d 2019-08-22 stsp
7228 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7229 01073a5d 2019-08-22 stsp if (err)
7230 01073a5d 2019-08-22 stsp return err;
7231 01073a5d 2019-08-22 stsp
7232 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7233 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7234 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7235 01073a5d 2019-08-22 stsp char *id_str;
7236 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7237 01073a5d 2019-08-22 stsp break;
7238 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7239 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7240 01073a5d 2019-08-22 stsp if (err)
7241 01073a5d 2019-08-22 stsp break;
7242 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7243 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7244 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7245 01073a5d 2019-08-22 stsp free(id_str);
7246 01073a5d 2019-08-22 stsp }
7247 01073a5d 2019-08-22 stsp
7248 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7249 01073a5d 2019-08-22 stsp return err;
7250 01073a5d 2019-08-22 stsp }
7251 01073a5d 2019-08-22 stsp
7252 01073a5d 2019-08-22 stsp static const struct got_error *
7253 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7254 01073a5d 2019-08-22 stsp {
7255 01073a5d 2019-08-22 stsp const struct got_error *err;
7256 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7257 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7258 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7259 01073a5d 2019-08-22 stsp char *id_str = NULL;
7260 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7261 01073a5d 2019-08-22 stsp
7262 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7263 01073a5d 2019-08-22 stsp if (err)
7264 01073a5d 2019-08-22 stsp return err;
7265 01073a5d 2019-08-22 stsp
7266 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7267 01073a5d 2019-08-22 stsp if (err)
7268 01073a5d 2019-08-22 stsp goto done;
7269 01073a5d 2019-08-22 stsp
7270 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7271 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7272 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7273 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7274 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7275 01073a5d 2019-08-22 stsp char *pid_str;
7276 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7277 01073a5d 2019-08-22 stsp if (err)
7278 01073a5d 2019-08-22 stsp goto done;
7279 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7280 01073a5d 2019-08-22 stsp free(pid_str);
7281 01073a5d 2019-08-22 stsp }
7282 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7283 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7284 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7285 01073a5d 2019-08-22 stsp
7286 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7287 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7288 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7289 01073a5d 2019-08-22 stsp
7290 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7291 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7292 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7293 01073a5d 2019-08-22 stsp done:
7294 01073a5d 2019-08-22 stsp free(id_str);
7295 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7296 01073a5d 2019-08-22 stsp return err;
7297 01073a5d 2019-08-22 stsp }
7298 01073a5d 2019-08-22 stsp
7299 01073a5d 2019-08-22 stsp static const struct got_error *
7300 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7301 01073a5d 2019-08-22 stsp {
7302 01073a5d 2019-08-22 stsp const struct got_error *err;
7303 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7304 01073a5d 2019-08-22 stsp char *id_str = NULL;
7305 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7306 01073a5d 2019-08-22 stsp
7307 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7308 01073a5d 2019-08-22 stsp if (err)
7309 01073a5d 2019-08-22 stsp return err;
7310 01073a5d 2019-08-22 stsp
7311 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7312 01073a5d 2019-08-22 stsp if (err)
7313 01073a5d 2019-08-22 stsp goto done;
7314 01073a5d 2019-08-22 stsp
7315 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7316 e15d5241 2019-08-22 stsp
7317 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7318 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7319 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7320 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7321 01073a5d 2019-08-22 stsp break;
7322 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7323 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7324 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7325 01073a5d 2019-08-22 stsp break;
7326 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7327 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7328 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7329 01073a5d 2019-08-22 stsp break;
7330 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7331 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7332 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7333 01073a5d 2019-08-22 stsp break;
7334 01073a5d 2019-08-22 stsp default:
7335 01073a5d 2019-08-22 stsp break;
7336 01073a5d 2019-08-22 stsp }
7337 01073a5d 2019-08-22 stsp
7338 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7339 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7340 e15d5241 2019-08-22 stsp
7341 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7342 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7343 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7344 01073a5d 2019-08-22 stsp
7345 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7346 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7347 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7348 01073a5d 2019-08-22 stsp done:
7349 01073a5d 2019-08-22 stsp free(id_str);
7350 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7351 01073a5d 2019-08-22 stsp return err;
7352 01073a5d 2019-08-22 stsp }
7353 01073a5d 2019-08-22 stsp
7354 01073a5d 2019-08-22 stsp static const struct got_error *
7355 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7356 01073a5d 2019-08-22 stsp {
7357 01073a5d 2019-08-22 stsp const struct got_error *error;
7358 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7359 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7360 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7361 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7362 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7363 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7364 01073a5d 2019-08-22 stsp
7365 01073a5d 2019-08-22 stsp #ifndef PROFILE
7366 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7367 01073a5d 2019-08-22 stsp NULL) == -1)
7368 01073a5d 2019-08-22 stsp err(1, "pledge");
7369 01073a5d 2019-08-22 stsp #endif
7370 01073a5d 2019-08-22 stsp
7371 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7372 01073a5d 2019-08-22 stsp switch (ch) {
7373 896e9b6f 2019-08-26 stsp case 'c':
7374 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7375 896e9b6f 2019-08-26 stsp break;
7376 01073a5d 2019-08-22 stsp case 'r':
7377 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7378 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7379 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7380 9ba1d308 2019-10-21 stsp optarg);
7381 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7382 01073a5d 2019-08-22 stsp break;
7383 896e9b6f 2019-08-26 stsp case 'P':
7384 896e9b6f 2019-08-26 stsp force_path = 1;
7385 896e9b6f 2019-08-26 stsp break;
7386 01073a5d 2019-08-22 stsp default:
7387 01073a5d 2019-08-22 stsp usage_cat();
7388 01073a5d 2019-08-22 stsp /* NOTREACHED */
7389 01073a5d 2019-08-22 stsp }
7390 01073a5d 2019-08-22 stsp }
7391 01073a5d 2019-08-22 stsp
7392 01073a5d 2019-08-22 stsp argc -= optind;
7393 01073a5d 2019-08-22 stsp argv += optind;
7394 01073a5d 2019-08-22 stsp
7395 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7396 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7397 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7398 01073a5d 2019-08-22 stsp goto done;
7399 01073a5d 2019-08-22 stsp }
7400 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7401 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7402 01073a5d 2019-08-22 stsp goto done;
7403 01073a5d 2019-08-22 stsp if (worktree) {
7404 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7405 01073a5d 2019-08-22 stsp repo_path = strdup(
7406 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7407 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7408 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7409 01073a5d 2019-08-22 stsp goto done;
7410 01073a5d 2019-08-22 stsp }
7411 01073a5d 2019-08-22 stsp }
7412 01073a5d 2019-08-22 stsp }
7413 01073a5d 2019-08-22 stsp
7414 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7415 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7416 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7417 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7418 01073a5d 2019-08-22 stsp }
7419 01073a5d 2019-08-22 stsp
7420 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7421 01073a5d 2019-08-22 stsp free(repo_path);
7422 01073a5d 2019-08-22 stsp if (error != NULL)
7423 01073a5d 2019-08-22 stsp goto done;
7424 01073a5d 2019-08-22 stsp
7425 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7426 896e9b6f 2019-08-26 stsp if (error)
7427 896e9b6f 2019-08-26 stsp goto done;
7428 896e9b6f 2019-08-26 stsp
7429 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7430 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7431 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7432 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7433 01073a5d 2019-08-22 stsp if (error)
7434 01073a5d 2019-08-22 stsp goto done;
7435 01073a5d 2019-08-22 stsp
7436 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7437 896e9b6f 2019-08-26 stsp if (force_path) {
7438 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7439 896e9b6f 2019-08-26 stsp argv[i]);
7440 896e9b6f 2019-08-26 stsp if (error)
7441 896e9b6f 2019-08-26 stsp break;
7442 896e9b6f 2019-08-26 stsp } else {
7443 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7444 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7445 896e9b6f 2019-08-26 stsp if (error) {
7446 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7447 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7448 896e9b6f 2019-08-26 stsp break;
7449 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7450 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7451 896e9b6f 2019-08-26 stsp if (error)
7452 896e9b6f 2019-08-26 stsp break;
7453 896e9b6f 2019-08-26 stsp }
7454 896e9b6f 2019-08-26 stsp }
7455 01073a5d 2019-08-22 stsp
7456 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7457 01073a5d 2019-08-22 stsp if (error)
7458 01073a5d 2019-08-22 stsp break;
7459 01073a5d 2019-08-22 stsp
7460 01073a5d 2019-08-22 stsp switch (obj_type) {
7461 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7462 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7463 01073a5d 2019-08-22 stsp break;
7464 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7465 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7466 01073a5d 2019-08-22 stsp break;
7467 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7468 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7469 01073a5d 2019-08-22 stsp break;
7470 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7471 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7472 01073a5d 2019-08-22 stsp break;
7473 01073a5d 2019-08-22 stsp default:
7474 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7475 01073a5d 2019-08-22 stsp break;
7476 01073a5d 2019-08-22 stsp }
7477 01073a5d 2019-08-22 stsp if (error)
7478 01073a5d 2019-08-22 stsp break;
7479 01073a5d 2019-08-22 stsp free(label);
7480 01073a5d 2019-08-22 stsp label = NULL;
7481 01073a5d 2019-08-22 stsp free(id);
7482 01073a5d 2019-08-22 stsp id = NULL;
7483 01073a5d 2019-08-22 stsp }
7484 01073a5d 2019-08-22 stsp done:
7485 01073a5d 2019-08-22 stsp free(label);
7486 01073a5d 2019-08-22 stsp free(id);
7487 896e9b6f 2019-08-26 stsp free(commit_id);
7488 01073a5d 2019-08-22 stsp if (worktree)
7489 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7490 01073a5d 2019-08-22 stsp if (repo) {
7491 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7492 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7493 01073a5d 2019-08-22 stsp if (error == NULL)
7494 01073a5d 2019-08-22 stsp error = repo_error;
7495 01073a5d 2019-08-22 stsp }
7496 0ebf8283 2019-07-24 stsp return error;
7497 0ebf8283 2019-07-24 stsp }