Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
20 #include <err.h>
21 #include <errno.h>
22 #include <locale.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <libgen.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_refs.h"
32 #include "got_repository.h"
33 #include "got_worktree.h"
35 #ifndef nitems
36 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
37 #endif
39 struct cmd {
40 const char *cmd_name;
41 const struct got_error *(*cmd_main)(int, char *[]);
42 void (*cmd_usage)(void);
43 const char *cmd_descr;
44 };
46 __dead void usage(void);
47 __dead void usage_checkout(void);
48 __dead void usage_log(void);
50 const struct got_error* cmd_checkout(int, char *[]);
51 const struct got_error* cmd_log(int, char *[]);
52 const struct got_error* cmd_status(int, char *[]);
54 struct cmd got_commands[] = {
55 { "checkout", cmd_checkout, usage_checkout,
56 "check out a new work tree from a repository" },
57 { "log", cmd_log, usage_log,
58 "show repository history" },
59 #ifdef notyet
60 { "status", cmd_status, usage_status,
61 "show modification status of files" },
62 #endif
63 };
65 int
66 main(int argc, char *argv[])
67 {
68 struct cmd *cmd;
69 unsigned int i;
70 int ch;
71 int hflag = 0;
73 setlocale(LC_ALL, "");
75 while ((ch = getopt(argc, argv, "h")) != -1) {
76 switch (ch) {
77 case 'h':
78 hflag = 1;
79 break;
80 default:
81 usage();
82 /* NOTREACHED */
83 }
84 }
86 argc -= optind;
87 argv += optind;
89 if (argc <= 0)
90 usage();
92 for (i = 0; i < nitems(got_commands); i++) {
93 const struct got_error *error;
95 cmd = &got_commands[i];
97 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
98 continue;
100 if (hflag)
101 got_commands[i].cmd_usage();
103 error = got_commands[i].cmd_main(argc, argv);
104 if (error) {
105 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
106 return 1;
109 return 0;
112 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
113 return 1;
116 __dead void
117 usage(void)
119 int i;
121 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
122 "Available commands:\n", getprogname());
123 for (i = 0; i < nitems(got_commands); i++) {
124 struct cmd *cmd = &got_commands[i];
125 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
127 exit(1);
130 __dead void
131 usage_checkout(void)
133 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
134 "[worktree-path]\n", getprogname());
135 exit(1);
138 static void
139 checkout_progress(void *arg, const char *path)
141 char *worktree_path = arg;
143 while (path[0] == '/')
144 path++;
146 printf("A %s/%s\n", worktree_path, path);
149 const struct got_error *
150 cmd_checkout(int argc, char *argv[])
152 const struct got_error *error = NULL;
153 struct got_repository *repo = NULL;
154 struct got_reference *head_ref = NULL;
155 struct got_worktree *worktree = NULL;
156 char *repo_path = NULL;
157 char *worktree_path = NULL;
158 const char *path_prefix = "";
159 int ch;
161 optind = 0;
162 while ((ch = getopt(argc, argv, "p:")) != -1) {
163 switch (ch) {
164 case 'p':
165 path_prefix = optarg;
166 break;
167 default:
168 usage();
169 /* NOTREACHED */
173 argc -= optind;
174 argv += optind;
176 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
177 err(1, "pledge");
179 if (argc == 1) {
180 char *cwd, *base, *dotgit;
181 repo_path = argv[0];
182 cwd = getcwd(NULL, 0);
183 if (cwd == NULL)
184 err(1, "getcwd");
185 base = basename(repo_path);
186 if (base == NULL)
187 err(1, "basename");
188 dotgit = strstr(base, ".git");
189 if (dotgit)
190 *dotgit = '\0';
191 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
192 free(cwd);
193 return got_error(GOT_ERR_NO_MEM);
195 free(cwd);
196 } else if (argc == 2) {
197 repo_path = argv[0];
198 worktree_path = strdup(argv[1]);
199 if (worktree_path == NULL)
200 return got_error(GOT_ERR_NO_MEM);
201 } else
202 usage_checkout();
204 error = got_repo_open(&repo, repo_path);
205 if (error != NULL)
206 goto done;
207 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
208 if (error != NULL)
209 goto done;
211 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
212 if (error != NULL)
213 goto done;
215 error = got_worktree_open(&worktree, worktree_path);
216 if (error != NULL)
217 goto done;
219 error = got_worktree_checkout_files(worktree, head_ref, repo,
220 checkout_progress, worktree_path);
221 if (error != NULL)
222 goto done;
224 printf("checked out %s\n", worktree_path);
226 done:
227 free(worktree_path);
228 return error;
231 static const struct got_error *
232 print_commit_object(struct got_object *, struct got_object_id *,
233 struct got_repository *);
235 static const struct got_error *
236 print_parent_commits(struct got_commit_object *commit,
237 struct got_repository *repo)
239 struct got_parent_id *pid;
240 const struct got_error *err = NULL;
241 struct got_object *obj;
243 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
244 err = got_object_open(&obj, repo, pid->id);
245 if (err != NULL)
246 return err;
247 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
248 return got_error(GOT_ERR_OBJ_TYPE);
249 err = print_commit_object(obj, pid->id, repo);
250 got_object_close(obj);
253 return err;
256 static const struct got_error *
257 print_commit_object(struct got_object *obj, struct got_object_id *id,
258 struct got_repository *repo)
260 struct got_commit_object *commit;
261 char *buf;
262 const struct got_error *err;
264 err = got_object_commit_open(&commit, repo, obj);
265 if (err)
266 return err;
268 err = got_object_id_str(&buf, id);
269 if (err)
270 return err;
272 printf("-----------------------------------------------\n");
273 printf("commit: %s\n", buf);
274 printf("Author: %s\n", commit->author);
275 printf("\n%s\n", commit->logmsg);
277 free(buf);
279 err = print_parent_commits(commit, repo);
280 got_object_commit_close(commit);
281 return err;
284 __dead void
285 usage_log(void)
287 fprintf(stderr, "usage: %s log [repository-path]\n", getprogname());
288 exit(1);
291 const struct got_error *
292 cmd_log(int argc, char *argv[])
294 const struct got_error *error;
295 struct got_repository *repo;
296 struct got_reference *head_ref;
297 struct got_object_id *id;
298 struct got_object *obj;
299 char *repo_path = NULL;
301 if (pledge("stdio rpath wpath cpath", NULL) == -1)
302 err(1, "pledge");
304 if (argc == 1) {
305 repo_path = getcwd(NULL, 0);
306 if (repo_path == NULL)
307 err(1, "getcwd");
308 } else if (argc == 2)
309 repo_path = argv[1];
310 else
311 usage_log();
313 error = got_repo_open(&repo, repo_path);
314 if (error != NULL)
315 return error;
316 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
317 if (error != NULL)
318 return error;
319 error = got_ref_resolve(&id, repo, head_ref);
320 if (error != NULL)
321 return error;
323 error = got_object_open(&obj, repo, id);
324 if (error != NULL)
325 return error;
326 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
327 error = print_commit_object(obj, id, repo);
328 if (error)
329 return error;
330 } else
331 return got_error(GOT_ERR_OBJ_TYPE);
332 got_object_close(obj);
333 free(id);
334 got_ref_close(head_ref);
335 got_repo_close(repo);
336 return NULL;
339 #ifdef notyet
340 const struct got_error *
341 cmd_status(int argc __unused, char *argv[] __unused)
343 git_repository *repo = NULL;
344 git_status_list *status;
345 git_status_options statusopts;
346 size_t i;
348 git_libgit2_init();
350 if (git_repository_open_ext(&repo, ".", 0, NULL))
351 errx(1, "git_repository_open: %s", giterr_last()->message);
353 if (git_repository_is_bare(repo))
354 errx(1, "bar repository");
356 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
357 errx(1, "git_status_init_options: %s", giterr_last()->message);
359 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
360 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
361 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
362 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
364 if (git_status_list_new(&status, repo, &statusopts))
365 errx(1, "git_status_list_new: %s", giterr_last()->message);
367 for (i = 0; i < git_status_list_entrycount(status); i++) {
368 const git_status_entry *se;
370 se = git_status_byindex(status, i);
371 switch (se->status) {
372 case GIT_STATUS_WT_NEW:
373 printf("? %s\n", se->index_to_workdir->new_file.path);
374 break;
375 case GIT_STATUS_WT_MODIFIED:
376 printf("M %s\n", se->index_to_workdir->new_file.path);
377 break;
378 case GIT_STATUS_WT_DELETED:
379 printf("R %s\n", se->index_to_workdir->new_file.path);
380 break;
381 case GIT_STATUS_WT_RENAMED:
382 printf("m %s -> %s\n",
383 se->index_to_workdir->old_file.path,
384 se->index_to_workdir->new_file.path);
385 break;
386 case GIT_STATUS_WT_TYPECHANGE:
387 printf("t %s\n", se->index_to_workdir->new_file.path);
388 break;
389 case GIT_STATUS_INDEX_NEW:
390 printf("A %s\n", se->head_to_index->new_file.path);
391 break;
392 case GIT_STATUS_INDEX_MODIFIED:
393 printf("M %s\n", se->head_to_index->old_file.path);
394 break;
395 case GIT_STATUS_INDEX_DELETED:
396 printf("R %s\n", se->head_to_index->old_file.path);
397 break;
398 case GIT_STATUS_INDEX_RENAMED:
399 printf("m %s -> %s\n",
400 se->head_to_index->old_file.path,
401 se->head_to_index->new_file.path);
402 break;
403 case GIT_STATUS_INDEX_TYPECHANGE:
404 printf("t %s\n", se->head_to_index->old_file.path);
405 break;
406 case GIT_STATUS_CURRENT:
407 default:
408 break;
412 git_status_list_free(status);
413 git_repository_free(repo);
414 git_libgit2_shutdown();
416 return 0;
418 #endif