1 /*
2  * libgit2 "cat-file" example - shows how to print data from the ODB
3  *
4  * Written by the libgit2 contributors
5  *
6  * To the extent possible under law, the author(s) have dedicated all copyright
7  * and related and neighboring rights to this software to the public domain
8  * worldwide. This software is distributed without any warranty.
9  *
10  * You should have received a copy of the CC0 Public Domain Dedication along
11  * with this software. If not, see
12  * <http://creativecommons.org/publicdomain/zero/1.0/>.
13  */
14 
15 #include "common.h"
16 
print_signature(const char * header,const git_signature * sig)17 static void print_signature(const char *header, const git_signature *sig)
18 {
19 	char sign;
20 	int offset, hours, minutes;
21 
22 	if (!sig)
23 		return;
24 
25 	offset = sig->when.offset;
26 	if (offset < 0) {
27 		sign = '-';
28 		offset = -offset;
29 	} else {
30 		sign = '+';
31 	}
32 
33 	hours   = offset / 60;
34 	minutes = offset % 60;
35 
36 	printf("%s %s <%s> %ld %c%02d%02d\n",
37 		   header, sig->name, sig->email, (long)sig->when.time,
38 		   sign, hours, minutes);
39 }
40 
41 /** Printing out a blob is simple, get the contents and print */
show_blob(const git_blob * blob)42 static void show_blob(const git_blob *blob)
43 {
44 	/* ? Does this need crlf filtering? */
45 	fwrite(git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob), 1, stdout);
46 }
47 
48 /** Show each entry with its type, id and attributes */
show_tree(const git_tree * tree)49 static void show_tree(const git_tree *tree)
50 {
51 	size_t i, max_i = (int)git_tree_entrycount(tree);
52 	char oidstr[GIT_OID_HEXSZ + 1];
53 	const git_tree_entry *te;
54 
55 	for (i = 0; i < max_i; ++i) {
56 		te = git_tree_entry_byindex(tree, i);
57 
58 		git_oid_tostr(oidstr, sizeof(oidstr), git_tree_entry_id(te));
59 
60 		printf("%06o %s %s\t%s\n",
61 			git_tree_entry_filemode(te),
62 			git_object_type2string(git_tree_entry_type(te)),
63 			oidstr, git_tree_entry_name(te));
64 	}
65 }
66 
67 /**
68  * Commits and tags have a few interesting fields in their header.
69  */
show_commit(const git_commit * commit)70 static void show_commit(const git_commit *commit)
71 {
72 	unsigned int i, max_i;
73 	char oidstr[GIT_OID_HEXSZ + 1];
74 
75 	git_oid_tostr(oidstr, sizeof(oidstr), git_commit_tree_id(commit));
76 	printf("tree %s\n", oidstr);
77 
78 	max_i = (unsigned int)git_commit_parentcount(commit);
79 	for (i = 0; i < max_i; ++i) {
80 		git_oid_tostr(oidstr, sizeof(oidstr), git_commit_parent_id(commit, i));
81 		printf("parent %s\n", oidstr);
82 	}
83 
84 	print_signature("author", git_commit_author(commit));
85 	print_signature("committer", git_commit_committer(commit));
86 
87 	if (git_commit_message(commit))
88 		printf("\n%s\n", git_commit_message(commit));
89 }
90 
show_tag(const git_tag * tag)91 static void show_tag(const git_tag *tag)
92 {
93 	char oidstr[GIT_OID_HEXSZ + 1];
94 
95 	git_oid_tostr(oidstr, sizeof(oidstr), git_tag_target_id(tag));;
96 	printf("object %s\n", oidstr);
97 	printf("type %s\n", git_object_type2string(git_tag_target_type(tag)));
98 	printf("tag %s\n", git_tag_name(tag));
99 	print_signature("tagger", git_tag_tagger(tag));
100 
101 	if (git_tag_message(tag))
102 		printf("\n%s\n", git_tag_message(tag));
103 }
104 
105 enum {
106 	SHOW_TYPE = 1,
107 	SHOW_SIZE = 2,
108 	SHOW_NONE = 3,
109 	SHOW_PRETTY = 4
110 };
111 
112 /* Forward declarations for option-parsing helper */
113 struct opts {
114 	const char *dir;
115 	const char *rev;
116 	int action;
117 	int verbose;
118 };
119 static void parse_opts(struct opts *o, int argc, char *argv[]);
120 
121 
122 /** Entry point for this command */
lg2_cat_file(git_repository * repo,int argc,char * argv[])123 int lg2_cat_file(git_repository *repo, int argc, char *argv[])
124 {
125 	struct opts o = { ".", NULL, 0, 0 };
126 	git_object *obj = NULL;
127 	char oidstr[GIT_OID_HEXSZ + 1];
128 
129 	parse_opts(&o, argc, argv);
130 
131 	check_lg2(git_revparse_single(&obj, repo, o.rev),
132 			"Could not resolve", o.rev);
133 
134 	if (o.verbose) {
135 		char oidstr[GIT_OID_HEXSZ + 1];
136 		git_oid_tostr(oidstr, sizeof(oidstr), git_object_id(obj));
137 
138 		printf("%s %s\n--\n",
139 			git_object_type2string(git_object_type(obj)), oidstr);
140 	}
141 
142 	switch (o.action) {
143 	case SHOW_TYPE:
144 		printf("%s\n", git_object_type2string(git_object_type(obj)));
145 		break;
146 	case SHOW_SIZE: {
147 		git_odb *odb;
148 		git_odb_object *odbobj;
149 
150 		check_lg2(git_repository_odb(&odb, repo), "Could not open ODB", NULL);
151 		check_lg2(git_odb_read(&odbobj, odb, git_object_id(obj)),
152 			"Could not find obj", NULL);
153 
154 		printf("%ld\n", (long)git_odb_object_size(odbobj));
155 
156 		git_odb_object_free(odbobj);
157 		git_odb_free(odb);
158 		}
159 		break;
160 	case SHOW_NONE:
161 		/* just want return result */
162 		break;
163 	case SHOW_PRETTY:
164 
165 		switch (git_object_type(obj)) {
166 		case GIT_OBJECT_BLOB:
167 			show_blob((const git_blob *)obj);
168 			break;
169 		case GIT_OBJECT_COMMIT:
170 			show_commit((const git_commit *)obj);
171 			break;
172 		case GIT_OBJECT_TREE:
173 			show_tree((const git_tree *)obj);
174 			break;
175 		case GIT_OBJECT_TAG:
176 			show_tag((const git_tag *)obj);
177 			break;
178 		default:
179 			printf("unknown %s\n", oidstr);
180 			break;
181 		}
182 		break;
183 	}
184 
185 	git_object_free(obj);
186 
187 	return 0;
188 }
189 
190 /** Print out usage information */
usage(const char * message,const char * arg)191 static void usage(const char *message, const char *arg)
192 {
193 	if (message && arg)
194 		fprintf(stderr, "%s: %s\n", message, arg);
195 	else if (message)
196 		fprintf(stderr, "%s\n", message);
197 	fprintf(stderr,
198 			"usage: cat-file (-t | -s | -e | -p) [-v] [-q] "
199 			"[-h|--help] [--git-dir=<dir>] <object>\n");
200 	exit(1);
201 }
202 
203 /** Parse the command-line options taken from git */
parse_opts(struct opts * o,int argc,char * argv[])204 static void parse_opts(struct opts *o, int argc, char *argv[])
205 {
206 	struct args_info args = ARGS_INFO_INIT;
207 
208 	for (args.pos = 1; args.pos < argc; ++args.pos) {
209 		char *a = argv[args.pos];
210 
211 		if (a[0] != '-') {
212 			if (o->rev != NULL)
213 				usage("Only one rev should be provided", NULL);
214 			else
215 				o->rev = a;
216 		}
217 		else if (!strcmp(a, "-t"))
218 			o->action = SHOW_TYPE;
219 		else if (!strcmp(a, "-s"))
220 			o->action = SHOW_SIZE;
221 		else if (!strcmp(a, "-e"))
222 			o->action = SHOW_NONE;
223 		else if (!strcmp(a, "-p"))
224 			o->action = SHOW_PRETTY;
225 		else if (!strcmp(a, "-q"))
226 			o->verbose = 0;
227 		else if (!strcmp(a, "-v"))
228 			o->verbose = 1;
229 		else if (!strcmp(a, "--help") || !strcmp(a, "-h"))
230 			usage(NULL, NULL);
231 		else if (!match_str_arg(&o->dir, &args, "--git-dir"))
232 			usage("Unknown option", a);
233 	}
234 
235 	if (!o->action || !o->rev)
236 		usage(NULL, NULL);
237 
238 }
239