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 #ifndef PROFILE
177 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
178 err(1, "pledge");
179 #endif
181 if (argc == 1) {
182 char *cwd, *base, *dotgit;
183 repo_path = argv[0];
184 cwd = getcwd(NULL, 0);
185 if (cwd == NULL)
186 err(1, "getcwd");
187 base = basename(repo_path);
188 if (base == NULL)
189 err(1, "basename");
190 dotgit = strstr(base, ".git");
191 if (dotgit)
192 *dotgit = '\0';
193 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
194 free(cwd);
195 return got_error(GOT_ERR_NO_MEM);
197 free(cwd);
198 } else if (argc == 2) {
199 repo_path = argv[0];
200 worktree_path = strdup(argv[1]);
201 if (worktree_path == NULL)
202 return got_error(GOT_ERR_NO_MEM);
203 } else
204 usage_checkout();
206 error = got_repo_open(&repo, repo_path);
207 if (error != NULL)
208 goto done;
209 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
210 if (error != NULL)
211 goto done;
213 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
214 if (error != NULL)
215 goto done;
217 error = got_worktree_open(&worktree, worktree_path);
218 if (error != NULL)
219 goto done;
221 error = got_worktree_checkout_files(worktree, head_ref, repo,
222 checkout_progress, worktree_path);
223 if (error != NULL)
224 goto done;
226 printf("checked out %s\n", worktree_path);
228 done:
229 free(worktree_path);
230 return error;
233 static const struct got_error *
234 print_commit_object(struct got_object *, struct got_object_id *,
235 struct got_repository *);
237 static const struct got_error *
238 print_parent_commits(struct got_commit_object *commit,
239 struct got_repository *repo)
241 struct got_parent_id *pid;
242 const struct got_error *err = NULL;
243 struct got_object *obj;
245 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
246 err = got_object_open(&obj, repo, pid->id);
247 if (err != NULL)
248 return err;
249 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
250 return got_error(GOT_ERR_OBJ_TYPE);
251 err = print_commit_object(obj, pid->id, repo);
252 got_object_close(obj);
255 return err;
258 static const struct got_error *
259 print_commit_object(struct got_object *obj, struct got_object_id *id,
260 struct got_repository *repo)
262 struct got_commit_object *commit;
263 char *buf;
264 const struct got_error *err;
266 err = got_object_commit_open(&commit, repo, obj);
267 if (err)
268 return err;
270 err = got_object_id_str(&buf, id);
271 if (err)
272 return err;
274 printf("-----------------------------------------------\n");
275 printf("commit: %s\n", buf);
276 printf("Author: %s\n", commit->author);
277 printf("\n%s\n", commit->logmsg);
279 free(buf);
281 err = print_parent_commits(commit, repo);
282 got_object_commit_close(commit);
283 return err;
286 __dead void
287 usage_log(void)
289 fprintf(stderr, "usage: %s log [repository-path]\n", getprogname());
290 exit(1);
293 const struct got_error *
294 cmd_log(int argc, char *argv[])
296 const struct got_error *error;
297 struct got_repository *repo;
298 struct got_reference *head_ref;
299 struct got_object_id *id;
300 struct got_object *obj;
301 char *repo_path = NULL;
303 #ifndef PROFILE
304 if (pledge("stdio rpath wpath cpath", NULL) == -1)
305 err(1, "pledge");
306 #endif
307 if (argc == 1) {
308 repo_path = getcwd(NULL, 0);
309 if (repo_path == NULL)
310 err(1, "getcwd");
311 } else if (argc == 2)
312 repo_path = argv[1];
313 else
314 usage_log();
316 error = got_repo_open(&repo, repo_path);
317 if (error != NULL)
318 return error;
319 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
320 if (error != NULL)
321 return error;
322 error = got_ref_resolve(&id, repo, head_ref);
323 if (error != NULL)
324 return error;
326 error = got_object_open(&obj, repo, id);
327 if (error != NULL)
328 return error;
329 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
330 error = print_commit_object(obj, id, repo);
331 if (error)
332 return error;
333 } else
334 return got_error(GOT_ERR_OBJ_TYPE);
335 got_object_close(obj);
336 free(id);
337 got_ref_close(head_ref);
338 got_repo_close(repo);
339 return NULL;
342 #ifdef notyet
343 const struct got_error *
344 cmd_status(int argc __unused, char *argv[] __unused)
346 git_repository *repo = NULL;
347 git_status_list *status;
348 git_status_options statusopts;
349 size_t i;
351 git_libgit2_init();
353 if (git_repository_open_ext(&repo, ".", 0, NULL))
354 errx(1, "git_repository_open: %s", giterr_last()->message);
356 if (git_repository_is_bare(repo))
357 errx(1, "bar repository");
359 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
360 errx(1, "git_status_init_options: %s", giterr_last()->message);
362 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
363 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
364 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
365 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
367 if (git_status_list_new(&status, repo, &statusopts))
368 errx(1, "git_status_list_new: %s", giterr_last()->message);
370 for (i = 0; i < git_status_list_entrycount(status); i++) {
371 const git_status_entry *se;
373 se = git_status_byindex(status, i);
374 switch (se->status) {
375 case GIT_STATUS_WT_NEW:
376 printf("? %s\n", se->index_to_workdir->new_file.path);
377 break;
378 case GIT_STATUS_WT_MODIFIED:
379 printf("M %s\n", se->index_to_workdir->new_file.path);
380 break;
381 case GIT_STATUS_WT_DELETED:
382 printf("R %s\n", se->index_to_workdir->new_file.path);
383 break;
384 case GIT_STATUS_WT_RENAMED:
385 printf("m %s -> %s\n",
386 se->index_to_workdir->old_file.path,
387 se->index_to_workdir->new_file.path);
388 break;
389 case GIT_STATUS_WT_TYPECHANGE:
390 printf("t %s\n", se->index_to_workdir->new_file.path);
391 break;
392 case GIT_STATUS_INDEX_NEW:
393 printf("A %s\n", se->head_to_index->new_file.path);
394 break;
395 case GIT_STATUS_INDEX_MODIFIED:
396 printf("M %s\n", se->head_to_index->old_file.path);
397 break;
398 case GIT_STATUS_INDEX_DELETED:
399 printf("R %s\n", se->head_to_index->old_file.path);
400 break;
401 case GIT_STATUS_INDEX_RENAMED:
402 printf("m %s -> %s\n",
403 se->head_to_index->old_file.path,
404 se->head_to_index->new_file.path);
405 break;
406 case GIT_STATUS_INDEX_TYPECHANGE:
407 printf("t %s\n", se->head_to_index->old_file.path);
408 break;
409 case GIT_STATUS_CURRENT:
410 default:
411 break;
415 git_status_list_free(status);
416 git_repository_free(repo);
417 git_libgit2_shutdown();
419 return 0;
421 #endif