Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 f42b1b34 2018-03-12 stsp * Copyright (c) 2018 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 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 f42b1b34 2018-03-12 stsp
21 5c860e29 2018-03-12 stsp #include <err.h>
22 5c860e29 2018-03-12 stsp #include <errno.h>
23 5c860e29 2018-03-12 stsp #include <locale.h>
24 5c860e29 2018-03-12 stsp #include <stdio.h>
25 5c860e29 2018-03-12 stsp #include <stdlib.h>
26 5c860e29 2018-03-12 stsp #include <string.h>
27 5c860e29 2018-03-12 stsp #include <unistd.h>
28 c09a553d 2018-03-12 stsp #include <libgen.h>
29 5c860e29 2018-03-12 stsp
30 f42b1b34 2018-03-12 stsp #include "got_error.h"
31 f42b1b34 2018-03-12 stsp #include "got_object.h"
32 5261c201 2018-04-01 stsp #include "got_reference.h"
33 f42b1b34 2018-03-12 stsp #include "got_repository.h"
34 c09a553d 2018-03-12 stsp #include "got_worktree.h"
35 79109fed 2018-03-27 stsp #include "got_diff.h"
36 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
37 5c860e29 2018-03-12 stsp
38 5c860e29 2018-03-12 stsp #ifndef nitems
39 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
40 5c860e29 2018-03-12 stsp #endif
41 5c860e29 2018-03-12 stsp
42 5c860e29 2018-03-12 stsp struct cmd {
43 5c860e29 2018-03-12 stsp const char *cmd_name;
44 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
45 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
46 46a0db7d 2018-03-12 stsp const char *cmd_descr;
47 5c860e29 2018-03-12 stsp };
48 5c860e29 2018-03-12 stsp
49 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
50 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
51 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
52 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
53 5c860e29 2018-03-12 stsp
54 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
55 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
56 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
57 4ed7e80c 2018-05-20 stsp #ifdef notyet
58 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
59 4ed7e80c 2018-05-20 stsp #endif
60 5c860e29 2018-03-12 stsp
61 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
62 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
63 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
64 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
65 1b6b95a8 2018-03-12 stsp "show repository history" },
66 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
67 b00d56cd 2018-04-01 stsp "compare files and directories" },
68 f42b1b34 2018-03-12 stsp #ifdef notyet
69 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
70 1b6b95a8 2018-03-12 stsp "show modification status of files" },
71 f42b1b34 2018-03-12 stsp #endif
72 5c860e29 2018-03-12 stsp };
73 5c860e29 2018-03-12 stsp
74 5c860e29 2018-03-12 stsp int
75 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
76 5c860e29 2018-03-12 stsp {
77 5c860e29 2018-03-12 stsp struct cmd *cmd;
78 5c860e29 2018-03-12 stsp unsigned int i;
79 5c860e29 2018-03-12 stsp int ch;
80 1b6b95a8 2018-03-12 stsp int hflag = 0;
81 5c860e29 2018-03-12 stsp
82 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
83 5c860e29 2018-03-12 stsp
84 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
85 5c860e29 2018-03-12 stsp switch (ch) {
86 1b6b95a8 2018-03-12 stsp case 'h':
87 1b6b95a8 2018-03-12 stsp hflag = 1;
88 1b6b95a8 2018-03-12 stsp break;
89 5c860e29 2018-03-12 stsp default:
90 5c860e29 2018-03-12 stsp usage();
91 5c860e29 2018-03-12 stsp /* NOTREACHED */
92 5c860e29 2018-03-12 stsp }
93 5c860e29 2018-03-12 stsp }
94 5c860e29 2018-03-12 stsp
95 5c860e29 2018-03-12 stsp argc -= optind;
96 5c860e29 2018-03-12 stsp argv += optind;
97 1e70621d 2018-03-27 stsp optind = 0;
98 5c860e29 2018-03-12 stsp
99 5c860e29 2018-03-12 stsp if (argc <= 0)
100 5c860e29 2018-03-12 stsp usage();
101 5c860e29 2018-03-12 stsp
102 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
103 d7d4f210 2018-03-12 stsp const struct got_error *error;
104 d7d4f210 2018-03-12 stsp
105 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
106 5c860e29 2018-03-12 stsp
107 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
108 5c860e29 2018-03-12 stsp continue;
109 5c860e29 2018-03-12 stsp
110 1b6b95a8 2018-03-12 stsp if (hflag)
111 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
112 1b6b95a8 2018-03-12 stsp
113 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
114 d7d4f210 2018-03-12 stsp if (error) {
115 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
116 d7d4f210 2018-03-12 stsp return 1;
117 d7d4f210 2018-03-12 stsp }
118 d7d4f210 2018-03-12 stsp
119 d7d4f210 2018-03-12 stsp return 0;
120 5c860e29 2018-03-12 stsp }
121 5c860e29 2018-03-12 stsp
122 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
123 5c860e29 2018-03-12 stsp return 1;
124 5c860e29 2018-03-12 stsp }
125 5c860e29 2018-03-12 stsp
126 4ed7e80c 2018-05-20 stsp __dead static void
127 5c860e29 2018-03-12 stsp usage(void)
128 5c860e29 2018-03-12 stsp {
129 46a0db7d 2018-03-12 stsp int i;
130 46a0db7d 2018-03-12 stsp
131 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
132 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
133 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
134 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
135 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
136 46a0db7d 2018-03-12 stsp }
137 5c860e29 2018-03-12 stsp exit(1);
138 5c860e29 2018-03-12 stsp }
139 5c860e29 2018-03-12 stsp
140 4ed7e80c 2018-05-20 stsp __dead static void
141 c09a553d 2018-03-12 stsp usage_checkout(void)
142 c09a553d 2018-03-12 stsp {
143 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
144 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
145 c09a553d 2018-03-12 stsp exit(1);
146 92a684f4 2018-03-12 stsp }
147 92a684f4 2018-03-12 stsp
148 92a684f4 2018-03-12 stsp static void
149 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
150 92a684f4 2018-03-12 stsp {
151 92a684f4 2018-03-12 stsp char *worktree_path = arg;
152 92a684f4 2018-03-12 stsp
153 92a684f4 2018-03-12 stsp while (path[0] == '/')
154 92a684f4 2018-03-12 stsp path++;
155 92a684f4 2018-03-12 stsp
156 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
157 c09a553d 2018-03-12 stsp }
158 c09a553d 2018-03-12 stsp
159 4ed7e80c 2018-05-20 stsp static const struct got_error *
160 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
161 c09a553d 2018-03-12 stsp {
162 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
163 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
164 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
165 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
166 c09a553d 2018-03-12 stsp char *repo_path = NULL;
167 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
168 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
169 0bb8a95e 2018-03-12 stsp int ch;
170 c09a553d 2018-03-12 stsp
171 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
172 0bb8a95e 2018-03-12 stsp switch (ch) {
173 0bb8a95e 2018-03-12 stsp case 'p':
174 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
175 0bb8a95e 2018-03-12 stsp break;
176 0bb8a95e 2018-03-12 stsp default:
177 0bb8a95e 2018-03-12 stsp usage();
178 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
179 0bb8a95e 2018-03-12 stsp }
180 0bb8a95e 2018-03-12 stsp }
181 0bb8a95e 2018-03-12 stsp
182 0bb8a95e 2018-03-12 stsp argc -= optind;
183 0bb8a95e 2018-03-12 stsp argv += optind;
184 0bb8a95e 2018-03-12 stsp
185 6715a751 2018-03-16 stsp #ifndef PROFILE
186 2178c42e 2018-04-22 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
187 c09a553d 2018-03-12 stsp err(1, "pledge");
188 6715a751 2018-03-16 stsp #endif
189 0bb8a95e 2018-03-12 stsp if (argc == 1) {
190 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
191 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
192 76089277 2018-04-01 stsp if (repo_path == NULL)
193 76089277 2018-04-01 stsp return got_error_from_errno();
194 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
195 76089277 2018-04-01 stsp if (cwd == NULL) {
196 76089277 2018-04-01 stsp error = got_error_from_errno();
197 76089277 2018-04-01 stsp goto done;
198 76089277 2018-04-01 stsp }
199 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
200 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
201 5d7c1dab 2018-04-01 stsp else
202 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
203 76089277 2018-04-01 stsp if (base == NULL) {
204 76089277 2018-04-01 stsp error = got_error_from_errno();
205 76089277 2018-04-01 stsp goto done;
206 76089277 2018-04-01 stsp }
207 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
208 c09a553d 2018-03-12 stsp if (dotgit)
209 c09a553d 2018-03-12 stsp *dotgit = '\0';
210 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
211 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
212 c09a553d 2018-03-12 stsp free(cwd);
213 76089277 2018-04-01 stsp goto done;
214 c09a553d 2018-03-12 stsp }
215 c09a553d 2018-03-12 stsp free(cwd);
216 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
217 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
218 76089277 2018-04-01 stsp if (repo_path == NULL) {
219 76089277 2018-04-01 stsp error = got_error_from_errno();
220 76089277 2018-04-01 stsp goto done;
221 76089277 2018-04-01 stsp }
222 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
223 76089277 2018-04-01 stsp if (worktree_path == NULL) {
224 76089277 2018-04-01 stsp error = got_error_from_errno();
225 76089277 2018-04-01 stsp goto done;
226 76089277 2018-04-01 stsp }
227 c09a553d 2018-03-12 stsp } else
228 c09a553d 2018-03-12 stsp usage_checkout();
229 c09a553d 2018-03-12 stsp
230 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
231 c09a553d 2018-03-12 stsp if (error != NULL)
232 c09a553d 2018-03-12 stsp goto done;
233 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
234 c09a553d 2018-03-12 stsp if (error != NULL)
235 c09a553d 2018-03-12 stsp goto done;
236 c09a553d 2018-03-12 stsp
237 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
238 c09a553d 2018-03-12 stsp if (error != NULL)
239 c09a553d 2018-03-12 stsp goto done;
240 c09a553d 2018-03-12 stsp
241 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
242 c09a553d 2018-03-12 stsp if (error != NULL)
243 c09a553d 2018-03-12 stsp goto done;
244 c09a553d 2018-03-12 stsp
245 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
246 92a684f4 2018-03-12 stsp checkout_progress, worktree_path);
247 c09a553d 2018-03-12 stsp if (error != NULL)
248 c09a553d 2018-03-12 stsp goto done;
249 c09a553d 2018-03-12 stsp
250 b65ae19a 2018-04-24 stsp printf("Checked out %s\n", worktree_path);
251 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
252 c09a553d 2018-03-12 stsp
253 c09a553d 2018-03-12 stsp done:
254 76089277 2018-04-01 stsp free(repo_path);
255 c09a553d 2018-03-12 stsp free(worktree_path);
256 c09a553d 2018-03-12 stsp return error;
257 c09a553d 2018-03-12 stsp }
258 c09a553d 2018-03-12 stsp
259 f42b1b34 2018-03-12 stsp static const struct got_error *
260 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
261 f42b1b34 2018-03-12 stsp struct got_repository *repo)
262 5c860e29 2018-03-12 stsp {
263 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
264 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
265 79109fed 2018-03-27 stsp struct got_object *obj;
266 79109fed 2018-03-27 stsp struct got_parent_id *pid;
267 79109fed 2018-03-27 stsp
268 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, commit->tree_id);
269 79109fed 2018-03-27 stsp if (err)
270 79109fed 2018-03-27 stsp return err;
271 79109fed 2018-03-27 stsp
272 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree2, repo, obj);
273 79109fed 2018-03-27 stsp got_object_close(obj);
274 79109fed 2018-03-27 stsp if (err)
275 79109fed 2018-03-27 stsp return err;
276 79109fed 2018-03-27 stsp
277 79109fed 2018-03-27 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
278 79109fed 2018-03-27 stsp if (pid != NULL) {
279 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
280 79109fed 2018-03-27 stsp
281 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pid->id);
282 79109fed 2018-03-27 stsp if (err)
283 79109fed 2018-03-27 stsp return err;
284 79109fed 2018-03-27 stsp
285 79109fed 2018-03-27 stsp err = got_object_commit_open(&pcommit, repo, obj);
286 79109fed 2018-03-27 stsp got_object_close(obj);
287 79109fed 2018-03-27 stsp if (err)
288 79109fed 2018-03-27 stsp return err;
289 79109fed 2018-03-27 stsp
290 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pcommit->tree_id);
291 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
292 79109fed 2018-03-27 stsp if (err)
293 79109fed 2018-03-27 stsp return err;
294 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree1, repo, obj);
295 79109fed 2018-03-27 stsp got_object_close(obj);
296 79109fed 2018-03-27 stsp if (err)
297 79109fed 2018-03-27 stsp return err;
298 79109fed 2018-03-27 stsp }
299 79109fed 2018-03-27 stsp
300 79109fed 2018-03-27 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
301 79109fed 2018-03-27 stsp if (tree1)
302 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
303 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
304 79109fed 2018-03-27 stsp return err;
305 79109fed 2018-03-27 stsp }
306 79109fed 2018-03-27 stsp
307 79109fed 2018-03-27 stsp static const struct got_error *
308 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
309 79109fed 2018-03-27 stsp struct got_repository *repo, int show_patch)
310 79109fed 2018-03-27 stsp {
311 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
312 832c249c 2018-06-10 stsp char *id_str, *logmsg, *line;
313 5c860e29 2018-03-12 stsp
314 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
315 8bf5b3c9 2018-03-17 stsp if (err)
316 8bf5b3c9 2018-03-17 stsp return err;
317 5c860e29 2018-03-12 stsp
318 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
319 832c249c 2018-06-10 stsp printf("commit %s\n", id_str);
320 832c249c 2018-06-10 stsp free(id_str);
321 b65ae19a 2018-04-24 stsp printf("author: %s\n", commit->author);
322 f3d135e1 2018-04-01 stsp if (strcmp(commit->author, commit->committer) != 0)
323 b65ae19a 2018-04-24 stsp printf("committer: %s\n", commit->committer);
324 832c249c 2018-06-10 stsp
325 832c249c 2018-06-10 stsp logmsg = strdup(commit->logmsg);
326 832c249c 2018-06-10 stsp if (logmsg == NULL)
327 832c249c 2018-06-10 stsp return got_error_from_errno();
328 8bf5b3c9 2018-03-17 stsp
329 832c249c 2018-06-10 stsp do {
330 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
331 832c249c 2018-06-10 stsp if (line)
332 832c249c 2018-06-10 stsp printf(" %s\n", line);
333 832c249c 2018-06-10 stsp } while (line);
334 832c249c 2018-06-10 stsp free(logmsg);
335 832c249c 2018-06-10 stsp
336 971751ac 2018-03-27 stsp if (show_patch) {
337 79109fed 2018-03-27 stsp err = print_patch(commit, id, repo);
338 971751ac 2018-03-27 stsp if (err == 0)
339 971751ac 2018-03-27 stsp printf("\n");
340 971751ac 2018-03-27 stsp }
341 79109fed 2018-03-27 stsp
342 79109fed 2018-03-27 stsp return err;
343 f42b1b34 2018-03-12 stsp }
344 5c860e29 2018-03-12 stsp
345 f42b1b34 2018-03-12 stsp static const struct got_error *
346 8bf5b3c9 2018-03-17 stsp print_commits(struct got_object *root_obj, struct got_object_id *root_id,
347 64a96a6d 2018-04-01 stsp struct got_repository *repo, int show_patch, int limit)
348 f42b1b34 2018-03-12 stsp {
349 f42b1b34 2018-03-12 stsp const struct got_error *err;
350 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
351 372ccdbb 2018-06-10 stsp int ncommits;
352 372ccdbb 2018-06-10 stsp
353 372ccdbb 2018-06-10 stsp err = got_commit_graph_open(&graph, root_id, repo);
354 f42b1b34 2018-03-12 stsp if (err)
355 f42b1b34 2018-03-12 stsp return err;
356 372ccdbb 2018-06-10 stsp err = got_commit_graph_iter_start(graph, root_id);
357 372ccdbb 2018-06-10 stsp if (err)
358 0a585a0d 2018-03-17 stsp return err;
359 372ccdbb 2018-06-10 stsp do {
360 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
361 372ccdbb 2018-06-10 stsp struct got_object_id *id;
362 8bf5b3c9 2018-03-17 stsp
363 372ccdbb 2018-06-10 stsp err = got_commit_graph_iter_next(&commit, &id, graph);
364 372ccdbb 2018-06-10 stsp if (err) {
365 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
366 372ccdbb 2018-06-10 stsp break;
367 372ccdbb 2018-06-10 stsp err = got_commit_graph_fetch_commits(&ncommits,
368 372ccdbb 2018-06-10 stsp graph, 1, repo);
369 372ccdbb 2018-06-10 stsp if (err)
370 372ccdbb 2018-06-10 stsp break;
371 372ccdbb 2018-06-10 stsp else
372 372ccdbb 2018-06-10 stsp continue;
373 7e665116 2018-04-02 stsp }
374 372ccdbb 2018-06-10 stsp if (commit == NULL)
375 7e665116 2018-04-02 stsp break;
376 372ccdbb 2018-06-10 stsp err = print_commit(commit, id, repo, show_patch);
377 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
378 7e665116 2018-04-02 stsp break;
379 372ccdbb 2018-06-10 stsp } while (ncommits > 0);
380 8bf5b3c9 2018-03-17 stsp
381 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
382 f42b1b34 2018-03-12 stsp return err;
383 f42b1b34 2018-03-12 stsp }
384 5c860e29 2018-03-12 stsp
385 4ed7e80c 2018-05-20 stsp __dead static void
386 6f3d1eb0 2018-03-12 stsp usage_log(void)
387 6f3d1eb0 2018-03-12 stsp {
388 64a96a6d 2018-04-01 stsp fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
389 64a96a6d 2018-04-01 stsp "[repository-path]\n", getprogname());
390 6f3d1eb0 2018-03-12 stsp exit(1);
391 6f3d1eb0 2018-03-12 stsp }
392 6f3d1eb0 2018-03-12 stsp
393 4ed7e80c 2018-05-20 stsp static const struct got_error *
394 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
395 f42b1b34 2018-03-12 stsp {
396 f42b1b34 2018-03-12 stsp const struct got_error *error;
397 f42b1b34 2018-03-12 stsp struct got_repository *repo;
398 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
399 f42b1b34 2018-03-12 stsp struct got_object *obj;
400 6f3d1eb0 2018-03-12 stsp char *repo_path = NULL;
401 d142fc45 2018-04-01 stsp char *start_commit = NULL;
402 79109fed 2018-03-27 stsp int ch;
403 64a96a6d 2018-04-01 stsp int show_patch = 0, limit = 0;
404 64a96a6d 2018-04-01 stsp const char *errstr;
405 5c860e29 2018-03-12 stsp
406 6715a751 2018-03-16 stsp #ifndef PROFILE
407 442a3ddc 2018-04-23 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
408 f42b1b34 2018-03-12 stsp err(1, "pledge");
409 6715a751 2018-03-16 stsp #endif
410 79109fed 2018-03-27 stsp
411 64a96a6d 2018-04-01 stsp while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
412 79109fed 2018-03-27 stsp switch (ch) {
413 79109fed 2018-03-27 stsp case 'p':
414 79109fed 2018-03-27 stsp show_patch = 1;
415 d142fc45 2018-04-01 stsp break;
416 d142fc45 2018-04-01 stsp case 'c':
417 d142fc45 2018-04-01 stsp start_commit = optarg;
418 64a96a6d 2018-04-01 stsp break;
419 64a96a6d 2018-04-01 stsp case 'l':
420 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
421 64a96a6d 2018-04-01 stsp if (errstr != NULL)
422 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
423 79109fed 2018-03-27 stsp break;
424 79109fed 2018-03-27 stsp default:
425 79109fed 2018-03-27 stsp usage();
426 79109fed 2018-03-27 stsp /* NOTREACHED */
427 79109fed 2018-03-27 stsp }
428 79109fed 2018-03-27 stsp }
429 79109fed 2018-03-27 stsp
430 79109fed 2018-03-27 stsp argc -= optind;
431 79109fed 2018-03-27 stsp argv += optind;
432 79109fed 2018-03-27 stsp
433 79109fed 2018-03-27 stsp if (argc == 0) {
434 6f3d1eb0 2018-03-12 stsp repo_path = getcwd(NULL, 0);
435 6f3d1eb0 2018-03-12 stsp if (repo_path == NULL)
436 e1e3f570 2018-04-01 stsp return got_error_from_errno();
437 3235492e 2018-04-01 stsp } else if (argc == 1) {
438 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
439 76089277 2018-04-01 stsp if (repo_path == NULL)
440 76089277 2018-04-01 stsp return got_error_from_errno();
441 3235492e 2018-04-01 stsp } else
442 6f3d1eb0 2018-03-12 stsp usage_log();
443 f42b1b34 2018-03-12 stsp
444 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
445 76089277 2018-04-01 stsp free(repo_path);
446 d7d4f210 2018-03-12 stsp if (error != NULL)
447 d7d4f210 2018-03-12 stsp return error;
448 f42b1b34 2018-03-12 stsp
449 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
450 3235492e 2018-04-01 stsp struct got_reference *head_ref;
451 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
452 3235492e 2018-04-01 stsp if (error != NULL)
453 3235492e 2018-04-01 stsp return error;
454 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
455 3235492e 2018-04-01 stsp got_ref_close(head_ref);
456 3235492e 2018-04-01 stsp if (error != NULL)
457 3235492e 2018-04-01 stsp return error;
458 3235492e 2018-04-01 stsp error = got_object_open(&obj, repo, id);
459 3235492e 2018-04-01 stsp } else {
460 d142fc45 2018-04-01 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
461 3235492e 2018-04-01 stsp if (error == NULL) {
462 3235492e 2018-04-01 stsp id = got_object_get_id(obj);
463 3235492e 2018-04-01 stsp if (id == NULL)
464 3235492e 2018-04-01 stsp error = got_error_from_errno();
465 3235492e 2018-04-01 stsp }
466 3235492e 2018-04-01 stsp }
467 d7d4f210 2018-03-12 stsp if (error != NULL)
468 d7d4f210 2018-03-12 stsp return error;
469 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
470 64a96a6d 2018-04-01 stsp error = print_commits(obj, id, repo, show_patch, limit);
471 8bf5b3c9 2018-03-17 stsp else
472 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
473 f42b1b34 2018-03-12 stsp got_object_close(obj);
474 f42b1b34 2018-03-12 stsp free(id);
475 f42b1b34 2018-03-12 stsp got_repo_close(repo);
476 8bf5b3c9 2018-03-17 stsp return error;
477 5c860e29 2018-03-12 stsp }
478 5c860e29 2018-03-12 stsp
479 4ed7e80c 2018-05-20 stsp __dead static void
480 3f8b7d6a 2018-04-01 stsp usage_diff(void)
481 3f8b7d6a 2018-04-01 stsp {
482 3f8b7d6a 2018-04-01 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
483 3f8b7d6a 2018-04-01 stsp getprogname());
484 3f8b7d6a 2018-04-01 stsp exit(1);
485 b00d56cd 2018-04-01 stsp }
486 b00d56cd 2018-04-01 stsp
487 4ed7e80c 2018-05-20 stsp static const struct got_error *
488 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
489 b00d56cd 2018-04-01 stsp {
490 b00d56cd 2018-04-01 stsp const struct got_error *error;
491 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
492 b00d56cd 2018-04-01 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
493 b00d56cd 2018-04-01 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
494 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
495 b00d56cd 2018-04-01 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
496 b00d56cd 2018-04-01 stsp int ch;
497 b00d56cd 2018-04-01 stsp
498 b00d56cd 2018-04-01 stsp #ifndef PROFILE
499 442a3ddc 2018-04-23 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
500 b00d56cd 2018-04-01 stsp err(1, "pledge");
501 b00d56cd 2018-04-01 stsp #endif
502 b00d56cd 2018-04-01 stsp
503 b00d56cd 2018-04-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
504 b00d56cd 2018-04-01 stsp switch (ch) {
505 b00d56cd 2018-04-01 stsp default:
506 b00d56cd 2018-04-01 stsp usage();
507 b00d56cd 2018-04-01 stsp /* NOTREACHED */
508 b00d56cd 2018-04-01 stsp }
509 b00d56cd 2018-04-01 stsp }
510 b00d56cd 2018-04-01 stsp
511 b00d56cd 2018-04-01 stsp argc -= optind;
512 b00d56cd 2018-04-01 stsp argv += optind;
513 b00d56cd 2018-04-01 stsp
514 b00d56cd 2018-04-01 stsp if (argc == 0) {
515 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
516 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
517 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
518 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
519 e1e3f570 2018-04-01 stsp return got_error_from_errno();
520 3f8b7d6a 2018-04-01 stsp obj_id_str1 = argv[0];
521 3f8b7d6a 2018-04-01 stsp obj_id_str2 = argv[1];
522 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
523 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
524 76089277 2018-04-01 stsp if (repo_path == NULL)
525 76089277 2018-04-01 stsp return got_error_from_errno();
526 b00d56cd 2018-04-01 stsp obj_id_str1 = argv[1];
527 b00d56cd 2018-04-01 stsp obj_id_str2 = argv[2];
528 b00d56cd 2018-04-01 stsp } else
529 b00d56cd 2018-04-01 stsp usage_diff();
530 b00d56cd 2018-04-01 stsp
531 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
532 76089277 2018-04-01 stsp free(repo_path);
533 b00d56cd 2018-04-01 stsp if (error != NULL)
534 b00d56cd 2018-04-01 stsp goto done;
535 b00d56cd 2018-04-01 stsp
536 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
537 b00d56cd 2018-04-01 stsp if (error == NULL) {
538 b00d56cd 2018-04-01 stsp id1 = got_object_get_id(obj1);
539 b00d56cd 2018-04-01 stsp if (id1 == NULL)
540 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
541 b00d56cd 2018-04-01 stsp }
542 b00d56cd 2018-04-01 stsp if (error != NULL)
543 b00d56cd 2018-04-01 stsp goto done;
544 b00d56cd 2018-04-01 stsp
545 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
546 b00d56cd 2018-04-01 stsp if (error == NULL) {
547 b00d56cd 2018-04-01 stsp id2 = got_object_get_id(obj2);
548 b00d56cd 2018-04-01 stsp if (id2 == NULL)
549 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
550 b00d56cd 2018-04-01 stsp }
551 b00d56cd 2018-04-01 stsp if (error != NULL)
552 b00d56cd 2018-04-01 stsp goto done;
553 b00d56cd 2018-04-01 stsp
554 b00d56cd 2018-04-01 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
555 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
556 b00d56cd 2018-04-01 stsp goto done;
557 b00d56cd 2018-04-01 stsp }
558 b00d56cd 2018-04-01 stsp
559 b00d56cd 2018-04-01 stsp switch (got_object_get_type(obj1)) {
560 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
561 11528a82 2018-05-19 stsp error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
562 b00d56cd 2018-04-01 stsp break;
563 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
564 11528a82 2018-05-19 stsp error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
565 b00d56cd 2018-04-01 stsp break;
566 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
567 11528a82 2018-05-19 stsp error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
568 b00d56cd 2018-04-01 stsp break;
569 b00d56cd 2018-04-01 stsp default:
570 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
571 b00d56cd 2018-04-01 stsp }
572 b00d56cd 2018-04-01 stsp
573 b00d56cd 2018-04-01 stsp done:
574 b00d56cd 2018-04-01 stsp if (obj1)
575 b00d56cd 2018-04-01 stsp got_object_close(obj1);
576 b00d56cd 2018-04-01 stsp if (obj2)
577 b00d56cd 2018-04-01 stsp got_object_close(obj2);
578 b00d56cd 2018-04-01 stsp if (id1)
579 b00d56cd 2018-04-01 stsp free(id1);
580 b00d56cd 2018-04-01 stsp if (id2)
581 b00d56cd 2018-04-01 stsp free(id2);
582 b00d56cd 2018-04-01 stsp if (repo)
583 b00d56cd 2018-04-01 stsp got_repo_close(repo);
584 b00d56cd 2018-04-01 stsp return error;
585 b00d56cd 2018-04-01 stsp }
586 b00d56cd 2018-04-01 stsp
587 f42b1b34 2018-03-12 stsp #ifdef notyet
588 4ed7e80c 2018-05-20 stsp static const struct got_error *
589 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
590 5c860e29 2018-03-12 stsp {
591 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
592 5c860e29 2018-03-12 stsp git_status_list *status;
593 5c860e29 2018-03-12 stsp git_status_options statusopts;
594 5c860e29 2018-03-12 stsp size_t i;
595 5c860e29 2018-03-12 stsp
596 5c860e29 2018-03-12 stsp git_libgit2_init();
597 5c860e29 2018-03-12 stsp
598 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
599 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
600 5c860e29 2018-03-12 stsp
601 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
602 5c860e29 2018-03-12 stsp errx(1, "bar repository");
603 5c860e29 2018-03-12 stsp
604 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
605 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
606 5c860e29 2018-03-12 stsp
607 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
608 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
609 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
610 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
611 5c860e29 2018-03-12 stsp
612 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
613 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
614 5c860e29 2018-03-12 stsp
615 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
616 5c860e29 2018-03-12 stsp const git_status_entry *se;
617 5c860e29 2018-03-12 stsp
618 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
619 5c860e29 2018-03-12 stsp switch (se->status) {
620 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
621 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
622 5c860e29 2018-03-12 stsp break;
623 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
624 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
625 5c860e29 2018-03-12 stsp break;
626 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
627 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
628 5c860e29 2018-03-12 stsp break;
629 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
630 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
631 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
632 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
633 5c860e29 2018-03-12 stsp break;
634 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
635 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
636 5c860e29 2018-03-12 stsp break;
637 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
638 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
639 5c860e29 2018-03-12 stsp break;
640 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
641 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
642 5c860e29 2018-03-12 stsp break;
643 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
644 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
645 5c860e29 2018-03-12 stsp break;
646 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
647 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
648 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
649 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
650 5c860e29 2018-03-12 stsp break;
651 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
652 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
653 5c860e29 2018-03-12 stsp break;
654 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
655 5c860e29 2018-03-12 stsp default:
656 5c860e29 2018-03-12 stsp break;
657 5c860e29 2018-03-12 stsp }
658 5c860e29 2018-03-12 stsp }
659 5c860e29 2018-03-12 stsp
660 5c860e29 2018-03-12 stsp git_status_list_free(status);
661 5c860e29 2018-03-12 stsp git_repository_free(repo);
662 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
663 5c860e29 2018-03-12 stsp
664 5c860e29 2018-03-12 stsp return 0;
665 5c860e29 2018-03-12 stsp }
666 f42b1b34 2018-03-12 stsp #endif