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 f42b1b34 2018-03-12 stsp
20 5c860e29 2018-03-12 stsp #include <err.h>
21 5c860e29 2018-03-12 stsp #include <errno.h>
22 5c860e29 2018-03-12 stsp #include <locale.h>
23 5c860e29 2018-03-12 stsp #include <stdio.h>
24 5c860e29 2018-03-12 stsp #include <stdlib.h>
25 5c860e29 2018-03-12 stsp #include <string.h>
26 5c860e29 2018-03-12 stsp #include <unistd.h>
27 c09a553d 2018-03-12 stsp #include <libgen.h>
28 5c860e29 2018-03-12 stsp
29 f42b1b34 2018-03-12 stsp #include "got_error.h"
30 f42b1b34 2018-03-12 stsp #include "got_object.h"
31 f42b1b34 2018-03-12 stsp #include "got_refs.h"
32 f42b1b34 2018-03-12 stsp #include "got_repository.h"
33 c09a553d 2018-03-12 stsp #include "got_worktree.h"
34 79109fed 2018-03-27 stsp #include "got_diff.h"
35 5c860e29 2018-03-12 stsp
36 5c860e29 2018-03-12 stsp #ifndef nitems
37 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
38 5c860e29 2018-03-12 stsp #endif
39 5c860e29 2018-03-12 stsp
40 5c860e29 2018-03-12 stsp struct cmd {
41 5c860e29 2018-03-12 stsp const char *cmd_name;
42 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
43 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
44 46a0db7d 2018-03-12 stsp const char *cmd_descr;
45 5c860e29 2018-03-12 stsp };
46 5c860e29 2018-03-12 stsp
47 5c860e29 2018-03-12 stsp __dead void usage(void);
48 c09a553d 2018-03-12 stsp __dead void usage_checkout(void);
49 6f3d1eb0 2018-03-12 stsp __dead void usage_log(void);
50 5c860e29 2018-03-12 stsp
51 c09a553d 2018-03-12 stsp const struct got_error* cmd_checkout(int, char *[]);
52 d7d4f210 2018-03-12 stsp const struct got_error* cmd_log(int, char *[]);
53 d7d4f210 2018-03-12 stsp const struct got_error* cmd_status(int, char *[]);
54 5c860e29 2018-03-12 stsp
55 5c860e29 2018-03-12 stsp struct cmd got_commands[] = {
56 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
57 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
58 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
59 1b6b95a8 2018-03-12 stsp "show repository history" },
60 f42b1b34 2018-03-12 stsp #ifdef notyet
61 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
62 1b6b95a8 2018-03-12 stsp "show modification status of files" },
63 f42b1b34 2018-03-12 stsp #endif
64 5c860e29 2018-03-12 stsp };
65 5c860e29 2018-03-12 stsp
66 5c860e29 2018-03-12 stsp int
67 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
68 5c860e29 2018-03-12 stsp {
69 5c860e29 2018-03-12 stsp struct cmd *cmd;
70 5c860e29 2018-03-12 stsp unsigned int i;
71 5c860e29 2018-03-12 stsp int ch;
72 1b6b95a8 2018-03-12 stsp int hflag = 0;
73 5c860e29 2018-03-12 stsp
74 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
75 5c860e29 2018-03-12 stsp
76 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
77 5c860e29 2018-03-12 stsp switch (ch) {
78 1b6b95a8 2018-03-12 stsp case 'h':
79 1b6b95a8 2018-03-12 stsp hflag = 1;
80 1b6b95a8 2018-03-12 stsp break;
81 5c860e29 2018-03-12 stsp default:
82 5c860e29 2018-03-12 stsp usage();
83 5c860e29 2018-03-12 stsp /* NOTREACHED */
84 5c860e29 2018-03-12 stsp }
85 5c860e29 2018-03-12 stsp }
86 5c860e29 2018-03-12 stsp
87 5c860e29 2018-03-12 stsp argc -= optind;
88 5c860e29 2018-03-12 stsp argv += optind;
89 1e70621d 2018-03-27 stsp optind = 0;
90 5c860e29 2018-03-12 stsp
91 5c860e29 2018-03-12 stsp if (argc <= 0)
92 5c860e29 2018-03-12 stsp usage();
93 5c860e29 2018-03-12 stsp
94 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
95 d7d4f210 2018-03-12 stsp const struct got_error *error;
96 d7d4f210 2018-03-12 stsp
97 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
98 5c860e29 2018-03-12 stsp
99 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
100 5c860e29 2018-03-12 stsp continue;
101 5c860e29 2018-03-12 stsp
102 1b6b95a8 2018-03-12 stsp if (hflag)
103 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
104 1b6b95a8 2018-03-12 stsp
105 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
106 d7d4f210 2018-03-12 stsp if (error) {
107 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
108 d7d4f210 2018-03-12 stsp return 1;
109 d7d4f210 2018-03-12 stsp }
110 d7d4f210 2018-03-12 stsp
111 d7d4f210 2018-03-12 stsp return 0;
112 5c860e29 2018-03-12 stsp }
113 5c860e29 2018-03-12 stsp
114 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
115 5c860e29 2018-03-12 stsp return 1;
116 5c860e29 2018-03-12 stsp }
117 5c860e29 2018-03-12 stsp
118 5c860e29 2018-03-12 stsp __dead void
119 5c860e29 2018-03-12 stsp usage(void)
120 5c860e29 2018-03-12 stsp {
121 46a0db7d 2018-03-12 stsp int i;
122 46a0db7d 2018-03-12 stsp
123 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
124 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
125 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
126 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
127 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
128 46a0db7d 2018-03-12 stsp }
129 5c860e29 2018-03-12 stsp exit(1);
130 5c860e29 2018-03-12 stsp }
131 5c860e29 2018-03-12 stsp
132 c09a553d 2018-03-12 stsp __dead void
133 c09a553d 2018-03-12 stsp usage_checkout(void)
134 c09a553d 2018-03-12 stsp {
135 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
136 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
137 c09a553d 2018-03-12 stsp exit(1);
138 92a684f4 2018-03-12 stsp }
139 92a684f4 2018-03-12 stsp
140 92a684f4 2018-03-12 stsp static void
141 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
142 92a684f4 2018-03-12 stsp {
143 92a684f4 2018-03-12 stsp char *worktree_path = arg;
144 92a684f4 2018-03-12 stsp
145 92a684f4 2018-03-12 stsp while (path[0] == '/')
146 92a684f4 2018-03-12 stsp path++;
147 92a684f4 2018-03-12 stsp
148 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
149 c09a553d 2018-03-12 stsp }
150 c09a553d 2018-03-12 stsp
151 c09a553d 2018-03-12 stsp const struct got_error *
152 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
153 c09a553d 2018-03-12 stsp {
154 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
155 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
156 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
157 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
158 c09a553d 2018-03-12 stsp char *repo_path = NULL;
159 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
160 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
161 0bb8a95e 2018-03-12 stsp int ch;
162 c09a553d 2018-03-12 stsp
163 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
164 0bb8a95e 2018-03-12 stsp switch (ch) {
165 0bb8a95e 2018-03-12 stsp case 'p':
166 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
167 0bb8a95e 2018-03-12 stsp break;
168 0bb8a95e 2018-03-12 stsp default:
169 0bb8a95e 2018-03-12 stsp usage();
170 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
171 0bb8a95e 2018-03-12 stsp }
172 0bb8a95e 2018-03-12 stsp }
173 0bb8a95e 2018-03-12 stsp
174 0bb8a95e 2018-03-12 stsp argc -= optind;
175 0bb8a95e 2018-03-12 stsp argv += optind;
176 0bb8a95e 2018-03-12 stsp
177 6715a751 2018-03-16 stsp #ifndef PROFILE
178 c09a553d 2018-03-12 stsp if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
179 c09a553d 2018-03-12 stsp err(1, "pledge");
180 6715a751 2018-03-16 stsp #endif
181 0bb8a95e 2018-03-12 stsp if (argc == 1) {
182 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
183 0bb8a95e 2018-03-12 stsp repo_path = argv[0];
184 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
185 c09a553d 2018-03-12 stsp if (cwd == NULL)
186 c09a553d 2018-03-12 stsp err(1, "getcwd");
187 c09a553d 2018-03-12 stsp base = basename(repo_path);
188 c09a553d 2018-03-12 stsp if (base == NULL)
189 c09a553d 2018-03-12 stsp err(1, "basename");
190 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
191 c09a553d 2018-03-12 stsp if (dotgit)
192 c09a553d 2018-03-12 stsp *dotgit = '\0';
193 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
194 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
195 c09a553d 2018-03-12 stsp free(cwd);
196 0a585a0d 2018-03-17 stsp return error;
197 c09a553d 2018-03-12 stsp }
198 c09a553d 2018-03-12 stsp free(cwd);
199 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
200 0bb8a95e 2018-03-12 stsp repo_path = argv[0];
201 0bb8a95e 2018-03-12 stsp worktree_path = strdup(argv[1]);
202 c09a553d 2018-03-12 stsp if (worktree_path == NULL)
203 0a585a0d 2018-03-17 stsp return got_error_from_errno();
204 c09a553d 2018-03-12 stsp } else
205 c09a553d 2018-03-12 stsp usage_checkout();
206 c09a553d 2018-03-12 stsp
207 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
208 c09a553d 2018-03-12 stsp if (error != NULL)
209 c09a553d 2018-03-12 stsp goto done;
210 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
211 c09a553d 2018-03-12 stsp if (error != NULL)
212 c09a553d 2018-03-12 stsp goto done;
213 c09a553d 2018-03-12 stsp
214 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
215 c09a553d 2018-03-12 stsp if (error != NULL)
216 c09a553d 2018-03-12 stsp goto done;
217 c09a553d 2018-03-12 stsp
218 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
219 c09a553d 2018-03-12 stsp if (error != NULL)
220 c09a553d 2018-03-12 stsp goto done;
221 c09a553d 2018-03-12 stsp
222 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
223 92a684f4 2018-03-12 stsp checkout_progress, worktree_path);
224 c09a553d 2018-03-12 stsp if (error != NULL)
225 c09a553d 2018-03-12 stsp goto done;
226 c09a553d 2018-03-12 stsp
227 c09a553d 2018-03-12 stsp printf("checked out %s\n", worktree_path);
228 c09a553d 2018-03-12 stsp
229 c09a553d 2018-03-12 stsp done:
230 c09a553d 2018-03-12 stsp free(worktree_path);
231 c09a553d 2018-03-12 stsp return error;
232 c09a553d 2018-03-12 stsp }
233 c09a553d 2018-03-12 stsp
234 f42b1b34 2018-03-12 stsp static const struct got_error *
235 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
236 f42b1b34 2018-03-12 stsp struct got_repository *repo)
237 5c860e29 2018-03-12 stsp {
238 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
239 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
240 79109fed 2018-03-27 stsp struct got_object *obj;
241 79109fed 2018-03-27 stsp struct got_parent_id *pid;
242 79109fed 2018-03-27 stsp
243 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, commit->tree_id);
244 79109fed 2018-03-27 stsp if (err)
245 79109fed 2018-03-27 stsp return err;
246 79109fed 2018-03-27 stsp
247 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree2, repo, obj);
248 79109fed 2018-03-27 stsp got_object_close(obj);
249 79109fed 2018-03-27 stsp if (err)
250 79109fed 2018-03-27 stsp return err;
251 79109fed 2018-03-27 stsp
252 79109fed 2018-03-27 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
253 79109fed 2018-03-27 stsp if (pid != NULL) {
254 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
255 79109fed 2018-03-27 stsp
256 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pid->id);
257 79109fed 2018-03-27 stsp if (err)
258 79109fed 2018-03-27 stsp return err;
259 79109fed 2018-03-27 stsp
260 79109fed 2018-03-27 stsp err = got_object_commit_open(&pcommit, repo, obj);
261 79109fed 2018-03-27 stsp got_object_close(obj);
262 79109fed 2018-03-27 stsp if (err)
263 79109fed 2018-03-27 stsp return err;
264 79109fed 2018-03-27 stsp
265 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pcommit->tree_id);
266 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
267 79109fed 2018-03-27 stsp if (err)
268 79109fed 2018-03-27 stsp return err;
269 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree1, repo, obj);
270 79109fed 2018-03-27 stsp got_object_close(obj);
271 79109fed 2018-03-27 stsp if (err)
272 79109fed 2018-03-27 stsp return err;
273 79109fed 2018-03-27 stsp }
274 79109fed 2018-03-27 stsp
275 79109fed 2018-03-27 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
276 79109fed 2018-03-27 stsp if (tree1)
277 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
278 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
279 79109fed 2018-03-27 stsp return err;
280 79109fed 2018-03-27 stsp }
281 79109fed 2018-03-27 stsp
282 79109fed 2018-03-27 stsp static const struct got_error *
283 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
284 79109fed 2018-03-27 stsp struct got_repository *repo, int show_patch)
285 79109fed 2018-03-27 stsp {
286 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
287 8bf5b3c9 2018-03-17 stsp char *buf;
288 5c860e29 2018-03-12 stsp
289 8bf5b3c9 2018-03-17 stsp err = got_object_id_str(&buf, id);
290 8bf5b3c9 2018-03-17 stsp if (err)
291 8bf5b3c9 2018-03-17 stsp return err;
292 5c860e29 2018-03-12 stsp
293 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
294 8bf5b3c9 2018-03-17 stsp printf("commit: %s\n", buf);
295 8bf5b3c9 2018-03-17 stsp printf("Author: %s\n", commit->author);
296 8bf5b3c9 2018-03-17 stsp printf("\n%s\n", commit->logmsg);
297 8bf5b3c9 2018-03-17 stsp
298 79109fed 2018-03-27 stsp if (show_patch)
299 79109fed 2018-03-27 stsp err = print_patch(commit, id, repo);
300 79109fed 2018-03-27 stsp
301 8bf5b3c9 2018-03-17 stsp free(buf);
302 79109fed 2018-03-27 stsp return err;
303 f42b1b34 2018-03-12 stsp }
304 8bf5b3c9 2018-03-17 stsp
305 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry {
306 8bf5b3c9 2018-03-17 stsp TAILQ_ENTRY(commit_queue_entry) entry;
307 8bf5b3c9 2018-03-17 stsp struct got_object_id *id;
308 8bf5b3c9 2018-03-17 stsp struct got_commit_object *commit;
309 8bf5b3c9 2018-03-17 stsp };
310 5c860e29 2018-03-12 stsp
311 f42b1b34 2018-03-12 stsp static const struct got_error *
312 8bf5b3c9 2018-03-17 stsp print_commits(struct got_object *root_obj, struct got_object_id *root_id,
313 79109fed 2018-03-27 stsp struct got_repository *repo, int show_patch)
314 f42b1b34 2018-03-12 stsp {
315 f42b1b34 2018-03-12 stsp const struct got_error *err;
316 8bf5b3c9 2018-03-17 stsp struct got_commit_object *root_commit;
317 8bf5b3c9 2018-03-17 stsp TAILQ_HEAD(, commit_queue_entry) commits;
318 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry *entry;
319 5c860e29 2018-03-12 stsp
320 8bf5b3c9 2018-03-17 stsp TAILQ_INIT(&commits);
321 5c860e29 2018-03-12 stsp
322 8bf5b3c9 2018-03-17 stsp err = got_object_commit_open(&root_commit, repo, root_obj);
323 f42b1b34 2018-03-12 stsp if (err)
324 f42b1b34 2018-03-12 stsp return err;
325 5c860e29 2018-03-12 stsp
326 8bf5b3c9 2018-03-17 stsp entry = calloc(1, sizeof(*entry));
327 8bf5b3c9 2018-03-17 stsp if (entry == NULL)
328 0a585a0d 2018-03-17 stsp return got_error_from_errno();
329 8bf5b3c9 2018-03-17 stsp entry->id = got_object_id_dup(root_id);
330 8bf5b3c9 2018-03-17 stsp if (entry->id == NULL) {
331 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
332 8bf5b3c9 2018-03-17 stsp free(entry);
333 0a585a0d 2018-03-17 stsp return err;
334 8bf5b3c9 2018-03-17 stsp }
335 8bf5b3c9 2018-03-17 stsp entry->commit = root_commit;
336 8bf5b3c9 2018-03-17 stsp TAILQ_INSERT_HEAD(&commits, entry, entry);
337 8bf5b3c9 2018-03-17 stsp
338 8bf5b3c9 2018-03-17 stsp while (!TAILQ_EMPTY(&commits)) {
339 8bf5b3c9 2018-03-17 stsp struct got_parent_id *pid;
340 5c860e29 2018-03-12 stsp
341 8bf5b3c9 2018-03-17 stsp entry = TAILQ_FIRST(&commits);
342 79109fed 2018-03-27 stsp err = print_commit(entry->commit, entry->id, repo, show_patch);
343 8bf5b3c9 2018-03-17 stsp if (err)
344 8bf5b3c9 2018-03-17 stsp break;
345 5c860e29 2018-03-12 stsp
346 8bf5b3c9 2018-03-17 stsp SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
347 8bf5b3c9 2018-03-17 stsp struct got_object *obj;
348 8bf5b3c9 2018-03-17 stsp struct got_commit_object *pcommit;
349 8bf5b3c9 2018-03-17 stsp struct commit_queue_entry *pentry;
350 8bf5b3c9 2018-03-17 stsp
351 8bf5b3c9 2018-03-17 stsp err = got_object_open(&obj, repo, pid->id);
352 8bf5b3c9 2018-03-17 stsp if (err)
353 8bf5b3c9 2018-03-17 stsp break;
354 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
355 8bf5b3c9 2018-03-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
356 8bf5b3c9 2018-03-17 stsp break;
357 8bf5b3c9 2018-03-17 stsp }
358 8bf5b3c9 2018-03-17 stsp
359 8bf5b3c9 2018-03-17 stsp err = got_object_commit_open(&pcommit, repo, obj);
360 8bf5b3c9 2018-03-17 stsp got_object_close(obj);
361 8bf5b3c9 2018-03-17 stsp if (err)
362 8bf5b3c9 2018-03-17 stsp break;
363 8bf5b3c9 2018-03-17 stsp
364 8bf5b3c9 2018-03-17 stsp pentry = calloc(1, sizeof(*pentry));
365 8bf5b3c9 2018-03-17 stsp if (pentry == NULL) {
366 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
367 8bf5b3c9 2018-03-17 stsp got_object_commit_close(pcommit);
368 8bf5b3c9 2018-03-17 stsp break;
369 8bf5b3c9 2018-03-17 stsp }
370 8bf5b3c9 2018-03-17 stsp pentry->id = got_object_id_dup(pid->id);
371 8bf5b3c9 2018-03-17 stsp if (pentry->id == NULL) {
372 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
373 8bf5b3c9 2018-03-17 stsp got_object_commit_close(pcommit);
374 8bf5b3c9 2018-03-17 stsp break;
375 8bf5b3c9 2018-03-17 stsp }
376 8bf5b3c9 2018-03-17 stsp pentry->commit = pcommit;
377 8bf5b3c9 2018-03-17 stsp TAILQ_INSERT_TAIL(&commits, pentry, entry);
378 8bf5b3c9 2018-03-17 stsp }
379 8bf5b3c9 2018-03-17 stsp
380 8bf5b3c9 2018-03-17 stsp TAILQ_REMOVE(&commits, entry, entry);
381 8bf5b3c9 2018-03-17 stsp got_object_commit_close(entry->commit);
382 8bf5b3c9 2018-03-17 stsp free(entry->id);
383 8bf5b3c9 2018-03-17 stsp free(entry);
384 8bf5b3c9 2018-03-17 stsp }
385 8bf5b3c9 2018-03-17 stsp
386 f42b1b34 2018-03-12 stsp return err;
387 f42b1b34 2018-03-12 stsp }
388 5c860e29 2018-03-12 stsp
389 6f3d1eb0 2018-03-12 stsp __dead void
390 6f3d1eb0 2018-03-12 stsp usage_log(void)
391 6f3d1eb0 2018-03-12 stsp {
392 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s log [repository-path]\n", getprogname());
393 6f3d1eb0 2018-03-12 stsp exit(1);
394 6f3d1eb0 2018-03-12 stsp }
395 6f3d1eb0 2018-03-12 stsp
396 d7d4f210 2018-03-12 stsp const struct got_error *
397 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
398 f42b1b34 2018-03-12 stsp {
399 f42b1b34 2018-03-12 stsp const struct got_error *error;
400 f42b1b34 2018-03-12 stsp struct got_repository *repo;
401 f42b1b34 2018-03-12 stsp struct got_reference *head_ref;
402 f42b1b34 2018-03-12 stsp struct got_object_id *id;
403 f42b1b34 2018-03-12 stsp struct got_object *obj;
404 6f3d1eb0 2018-03-12 stsp char *repo_path = NULL;
405 79109fed 2018-03-27 stsp int ch;
406 79109fed 2018-03-27 stsp int show_patch = 0;
407 5c860e29 2018-03-12 stsp
408 6715a751 2018-03-16 stsp #ifndef PROFILE
409 f42b1b34 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
410 f42b1b34 2018-03-12 stsp err(1, "pledge");
411 6715a751 2018-03-16 stsp #endif
412 79109fed 2018-03-27 stsp
413 79109fed 2018-03-27 stsp while ((ch = getopt(argc, argv, "p")) != -1) {
414 79109fed 2018-03-27 stsp switch (ch) {
415 79109fed 2018-03-27 stsp case 'p':
416 79109fed 2018-03-27 stsp show_patch = 1;
417 79109fed 2018-03-27 stsp break;
418 79109fed 2018-03-27 stsp default:
419 79109fed 2018-03-27 stsp usage();
420 79109fed 2018-03-27 stsp /* NOTREACHED */
421 79109fed 2018-03-27 stsp }
422 79109fed 2018-03-27 stsp }
423 79109fed 2018-03-27 stsp
424 79109fed 2018-03-27 stsp argc -= optind;
425 79109fed 2018-03-27 stsp argv += optind;
426 79109fed 2018-03-27 stsp
427 79109fed 2018-03-27 stsp if (argc == 0) {
428 6f3d1eb0 2018-03-12 stsp repo_path = getcwd(NULL, 0);
429 6f3d1eb0 2018-03-12 stsp if (repo_path == NULL)
430 6f3d1eb0 2018-03-12 stsp err(1, "getcwd");
431 79109fed 2018-03-27 stsp } else if (argc == 1)
432 6f3d1eb0 2018-03-12 stsp repo_path = argv[1];
433 6f3d1eb0 2018-03-12 stsp else
434 6f3d1eb0 2018-03-12 stsp usage_log();
435 f42b1b34 2018-03-12 stsp
436 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
437 d7d4f210 2018-03-12 stsp if (error != NULL)
438 d7d4f210 2018-03-12 stsp return error;
439 d7d4f210 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
440 d7d4f210 2018-03-12 stsp if (error != NULL)
441 d7d4f210 2018-03-12 stsp return error;
442 f42b1b34 2018-03-12 stsp error = got_ref_resolve(&id, repo, head_ref);
443 d7d4f210 2018-03-12 stsp if (error != NULL)
444 d7d4f210 2018-03-12 stsp return error;
445 f42b1b34 2018-03-12 stsp
446 f42b1b34 2018-03-12 stsp error = got_object_open(&obj, repo, id);
447 d7d4f210 2018-03-12 stsp if (error != NULL)
448 d7d4f210 2018-03-12 stsp return error;
449 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
450 79109fed 2018-03-27 stsp error = print_commits(obj, id, repo, show_patch);
451 8bf5b3c9 2018-03-17 stsp else
452 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
453 f42b1b34 2018-03-12 stsp got_object_close(obj);
454 f42b1b34 2018-03-12 stsp free(id);
455 f42b1b34 2018-03-12 stsp got_ref_close(head_ref);
456 f42b1b34 2018-03-12 stsp got_repo_close(repo);
457 8bf5b3c9 2018-03-17 stsp return error;
458 5c860e29 2018-03-12 stsp }
459 5c860e29 2018-03-12 stsp
460 f42b1b34 2018-03-12 stsp #ifdef notyet
461 d7d4f210 2018-03-12 stsp const struct got_error *
462 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
463 5c860e29 2018-03-12 stsp {
464 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
465 5c860e29 2018-03-12 stsp git_status_list *status;
466 5c860e29 2018-03-12 stsp git_status_options statusopts;
467 5c860e29 2018-03-12 stsp size_t i;
468 5c860e29 2018-03-12 stsp
469 5c860e29 2018-03-12 stsp git_libgit2_init();
470 5c860e29 2018-03-12 stsp
471 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
472 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
473 5c860e29 2018-03-12 stsp
474 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
475 5c860e29 2018-03-12 stsp errx(1, "bar repository");
476 5c860e29 2018-03-12 stsp
477 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
478 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
479 5c860e29 2018-03-12 stsp
480 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
481 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
482 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
483 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
484 5c860e29 2018-03-12 stsp
485 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
486 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
487 5c860e29 2018-03-12 stsp
488 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
489 5c860e29 2018-03-12 stsp const git_status_entry *se;
490 5c860e29 2018-03-12 stsp
491 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
492 5c860e29 2018-03-12 stsp switch (se->status) {
493 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
494 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
495 5c860e29 2018-03-12 stsp break;
496 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
497 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
498 5c860e29 2018-03-12 stsp break;
499 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
500 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
501 5c860e29 2018-03-12 stsp break;
502 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
503 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
504 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
505 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
506 5c860e29 2018-03-12 stsp break;
507 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
508 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
509 5c860e29 2018-03-12 stsp break;
510 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
511 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
512 5c860e29 2018-03-12 stsp break;
513 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
514 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
515 5c860e29 2018-03-12 stsp break;
516 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
517 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
518 5c860e29 2018-03-12 stsp break;
519 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
520 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
521 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
522 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
523 5c860e29 2018-03-12 stsp break;
524 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
525 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
526 5c860e29 2018-03-12 stsp break;
527 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
528 5c860e29 2018-03-12 stsp default:
529 5c860e29 2018-03-12 stsp break;
530 5c860e29 2018-03-12 stsp }
531 5c860e29 2018-03-12 stsp }
532 5c860e29 2018-03-12 stsp
533 5c860e29 2018-03-12 stsp git_status_list_free(status);
534 5c860e29 2018-03-12 stsp git_repository_free(repo);
535 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
536 5c860e29 2018-03-12 stsp
537 5c860e29 2018-03-12 stsp return 0;
538 5c860e29 2018-03-12 stsp }
539 f42b1b34 2018-03-12 stsp #endif