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>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_refs.h"
31 #include "got_repository.h"
33 #ifndef nitems
34 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
35 #endif
37 struct cmd {
38 const char *cmd_name;
39 const struct got_error *(*cmd_main)(int, char *[]);
40 void (*cmd_usage)(void);
41 const char *cmd_descr;
42 };
44 __dead void usage(void);
45 __dead void usage_log(void);
47 const struct got_error* cmd_log(int, char *[]);
48 const struct got_error* cmd_status(int, char *[]);
50 struct cmd got_commands[] = {
51 { "log", cmd_log, usage_log,
52 "show repository history" },
53 #ifdef notyet
54 { "status", cmd_status, usage_status,
55 "show modification status of files" },
56 #endif
57 };
59 int
60 main(int argc, char *argv[])
61 {
62 struct cmd *cmd;
63 unsigned int i;
64 int ch;
65 int hflag = 0;
67 setlocale(LC_ALL, "");
69 if (pledge("stdio rpath wpath cpath", NULL) == -1)
70 err(1, "pledge");
72 while ((ch = getopt(argc, argv, "h")) != -1) {
73 switch (ch) {
74 case 'h':
75 hflag = 1;
76 break;
77 default:
78 usage();
79 /* NOTREACHED */
80 }
81 }
83 argc -= optind;
84 argv += optind;
86 if (argc <= 0)
87 usage();
89 for (i = 0; i < nitems(got_commands); i++) {
90 const struct got_error *error;
92 cmd = &got_commands[i];
94 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
95 continue;
97 if (hflag)
98 got_commands[i].cmd_usage();
100 error = got_commands[i].cmd_main(argc, argv);
101 if (error) {
102 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
103 return 1;
106 return 0;
109 fprintf(stderr, "%s: unknown command -- %s\n", getprogname(), argv[0]);
110 return 1;
113 __dead void
114 usage(void)
116 int i;
118 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
119 "Available commands:\n", getprogname());
120 for (i = 0; i < nitems(got_commands); i++) {
121 struct cmd *cmd = &got_commands[i];
122 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
124 exit(1);
127 static const struct got_error *
128 print_commit_object(struct got_object *, struct got_object_id *,
129 struct got_repository *);
131 static const struct got_error *
132 print_parent_commits(struct got_commit_object *commit,
133 struct got_repository *repo)
135 struct got_parent_id *pid;
136 const struct got_error *err = NULL;
137 struct got_object *obj;
139 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
140 err = got_object_open(&obj, repo, pid->id);
141 if (err != NULL)
142 return err;
143 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
144 return got_error(GOT_ERR_OBJ_TYPE);
145 err = print_commit_object(obj, pid->id, repo);
146 got_object_close(obj);
149 return err;
152 static const struct got_error *
153 print_commit_object(struct got_object *obj, struct got_object_id *id,
154 struct got_repository *repo)
156 struct got_commit_object *commit;
157 char *buf;
158 const struct got_error *err;
160 err = got_object_commit_open(&commit, repo, obj);
161 if (err)
162 return err;
164 err = got_object_id_str(&buf, id);
165 if (err)
166 return err;
168 printf("-----------------------------------------------\n");
169 printf("commit: %s\n", buf);
170 printf("Author: %s\n", commit->author);
171 printf("\n%s\n", commit->logmsg);
173 free(buf);
175 err = print_parent_commits(commit, repo);
176 got_object_commit_close(commit);
177 return err;
180 __dead void
181 usage_log(void)
183 fprintf(stderr, "usage: %s log [REPO_PATH]\n", getprogname());
184 exit(1);
187 const struct got_error *
188 cmd_log(int argc, char *argv[])
190 const struct got_error *error;
191 struct got_repository *repo;
192 struct got_reference *head_ref;
193 struct got_object_id *id;
194 struct got_object *obj;
195 char *repo_path = NULL;
197 if (pledge("stdio rpath wpath cpath", NULL) == -1)
198 err(1, "pledge");
200 if (argc == 1) {
201 repo_path = getcwd(NULL, 0);
202 if (repo_path == NULL)
203 err(1, "getcwd");
204 } else if (argc == 2)
205 repo_path = argv[1];
206 else
207 usage_log();
209 error = got_repo_open(&repo, repo_path);
210 if (error != NULL)
211 return error;
212 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
213 if (error != NULL)
214 return error;
215 error = got_ref_resolve(&id, repo, head_ref);
216 if (error != NULL)
217 return error;
219 error = got_object_open(&obj, repo, id);
220 if (error != NULL)
221 return error;
222 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
223 error = print_commit_object(obj, id, repo);
224 if (error)
225 return error;
226 } else
227 return got_error(GOT_ERR_OBJ_TYPE);
228 got_object_close(obj);
229 free(id);
230 got_ref_close(head_ref);
231 got_repo_close(repo);
232 return NULL;
235 #ifdef notyet
236 const struct got_error *
237 cmd_status(int argc __unused, char *argv[] __unused)
239 git_repository *repo = NULL;
240 git_status_list *status;
241 git_status_options statusopts;
242 size_t i;
244 git_libgit2_init();
246 if (git_repository_open_ext(&repo, ".", 0, NULL))
247 errx(1, "git_repository_open: %s", giterr_last()->message);
249 if (git_repository_is_bare(repo))
250 errx(1, "bar repository");
252 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
253 errx(1, "git_status_init_options: %s", giterr_last()->message);
255 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
256 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
257 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
258 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
260 if (git_status_list_new(&status, repo, &statusopts))
261 errx(1, "git_status_list_new: %s", giterr_last()->message);
263 for (i = 0; i < git_status_list_entrycount(status); i++) {
264 const git_status_entry *se;
266 se = git_status_byindex(status, i);
267 switch (se->status) {
268 case GIT_STATUS_WT_NEW:
269 printf("? %s\n", se->index_to_workdir->new_file.path);
270 break;
271 case GIT_STATUS_WT_MODIFIED:
272 printf("M %s\n", se->index_to_workdir->new_file.path);
273 break;
274 case GIT_STATUS_WT_DELETED:
275 printf("R %s\n", se->index_to_workdir->new_file.path);
276 break;
277 case GIT_STATUS_WT_RENAMED:
278 printf("m %s -> %s\n",
279 se->index_to_workdir->old_file.path,
280 se->index_to_workdir->new_file.path);
281 break;
282 case GIT_STATUS_WT_TYPECHANGE:
283 printf("t %s\n", se->index_to_workdir->new_file.path);
284 break;
285 case GIT_STATUS_INDEX_NEW:
286 printf("A %s\n", se->head_to_index->new_file.path);
287 break;
288 case GIT_STATUS_INDEX_MODIFIED:
289 printf("M %s\n", se->head_to_index->old_file.path);
290 break;
291 case GIT_STATUS_INDEX_DELETED:
292 printf("R %s\n", se->head_to_index->old_file.path);
293 break;
294 case GIT_STATUS_INDEX_RENAMED:
295 printf("m %s -> %s\n",
296 se->head_to_index->old_file.path,
297 se->head_to_index->new_file.path);
298 break;
299 case GIT_STATUS_INDEX_TYPECHANGE:
300 printf("t %s\n", se->head_to_index->old_file.path);
301 break;
302 case GIT_STATUS_CURRENT:
303 default:
304 break;
308 git_status_list_free(status);
309 git_repository_free(repo);
310 git_libgit2_shutdown();
312 return 0;
314 #endif