]>
git.proxmox.com Git - libgit2.git/blob - examples/cat-file.c
2 * libgit2 "cat-file" example - shows how to print data from the ODB
4 * Written by the libgit2 contributors
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.
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/>.
17 static void print_signature(const char *header
, const git_signature
*sig
)
20 int offset
, hours
, minutes
;
25 offset
= sig
->when
.offset
;
34 minutes
= offset
% 60;
36 printf("%s %s <%s> %ld %c%02d%02d\n",
37 header
, sig
->name
, sig
->email
, (long)sig
->when
.time
,
38 sign
, hours
, minutes
);
41 /** Printing out a blob is simple, get the contents and print */
42 static void show_blob(const git_blob
*blob
)
44 /* ? Does this need crlf filtering? */
45 fwrite(git_blob_rawcontent(blob
), (size_t)git_blob_rawsize(blob
), 1, stdout
);
48 /** Show each entry with its type, id and attributes */
49 static void show_tree(const git_tree
*tree
)
51 size_t i
, max_i
= (int)git_tree_entrycount(tree
);
52 char oidstr
[GIT_OID_HEXSZ
+ 1];
53 const git_tree_entry
*te
;
55 for (i
= 0; i
< max_i
; ++i
) {
56 te
= git_tree_entry_byindex(tree
, i
);
58 git_oid_tostr(oidstr
, sizeof(oidstr
), git_tree_entry_id(te
));
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
));
68 * Commits and tags have a few interesting fields in their header.
70 static void show_commit(const git_commit
*commit
)
72 unsigned int i
, max_i
;
73 char oidstr
[GIT_OID_HEXSZ
+ 1];
75 git_oid_tostr(oidstr
, sizeof(oidstr
), git_commit_tree_id(commit
));
76 printf("tree %s\n", oidstr
);
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
);
84 print_signature("author", git_commit_author(commit
));
85 print_signature("committer", git_commit_committer(commit
));
87 if (git_commit_message(commit
))
88 printf("\n%s\n", git_commit_message(commit
));
91 static void show_tag(const git_tag
*tag
)
93 char oidstr
[GIT_OID_HEXSZ
+ 1];
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
));
101 if (git_tag_message(tag
))
102 printf("\n%s\n", git_tag_message(tag
));
112 /* Forward declarations for option-parsing helper */
113 struct catfile_options
{
120 static void parse_opts(struct catfile_options
*o
, int argc
, char *argv
[]);
123 /** Entry point for this command */
124 int lg2_cat_file(git_repository
*repo
, int argc
, char *argv
[])
126 struct catfile_options o
= { ".", NULL
, 0, 0 };
127 git_object
*obj
= NULL
;
128 char oidstr
[GIT_OID_HEXSZ
+ 1];
130 parse_opts(&o
, argc
, argv
);
132 check_lg2(git_revparse_single(&obj
, repo
, o
.rev
),
133 "Could not resolve", o
.rev
);
136 char oidstr
[GIT_OID_HEXSZ
+ 1];
137 git_oid_tostr(oidstr
, sizeof(oidstr
), git_object_id(obj
));
139 printf("%s %s\n--\n",
140 git_object_type2string(git_object_type(obj
)), oidstr
);
145 printf("%s\n", git_object_type2string(git_object_type(obj
)));
149 git_odb_object
*odbobj
;
151 check_lg2(git_repository_odb(&odb
, repo
), "Could not open ODB", NULL
);
152 check_lg2(git_odb_read(&odbobj
, odb
, git_object_id(obj
)),
153 "Could not find obj", NULL
);
155 printf("%ld\n", (long)git_odb_object_size(odbobj
));
157 git_odb_object_free(odbobj
);
162 /* just want return result */
166 switch (git_object_type(obj
)) {
167 case GIT_OBJECT_BLOB
:
168 show_blob((const git_blob
*)obj
);
170 case GIT_OBJECT_COMMIT
:
171 show_commit((const git_commit
*)obj
);
173 case GIT_OBJECT_TREE
:
174 show_tree((const git_tree
*)obj
);
177 show_tag((const git_tag
*)obj
);
180 printf("unknown %s\n", oidstr
);
186 git_object_free(obj
);
191 /** Print out usage information */
192 static void usage(const char *message
, const char *arg
)
195 fprintf(stderr
, "%s: %s\n", message
, arg
);
197 fprintf(stderr
, "%s\n", message
);
199 "usage: cat-file (-t | -s | -e | -p) [-v] [-q] "
200 "[-h|--help] [--git-dir=<dir>] <object>\n");
204 /** Parse the command-line options taken from git */
205 static void parse_opts(struct catfile_options
*o
, int argc
, char *argv
[])
207 struct args_info args
= ARGS_INFO_INIT
;
209 for (args
.pos
= 1; args
.pos
< argc
; ++args
.pos
) {
210 char *a
= argv
[args
.pos
];
214 usage("Only one rev should be provided", NULL
);
218 else if (!strcmp(a
, "-t"))
219 o
->action
= SHOW_TYPE
;
220 else if (!strcmp(a
, "-s"))
221 o
->action
= SHOW_SIZE
;
222 else if (!strcmp(a
, "-e"))
223 o
->action
= SHOW_NONE
;
224 else if (!strcmp(a
, "-p"))
225 o
->action
= SHOW_PRETTY
;
226 else if (!strcmp(a
, "-q"))
228 else if (!strcmp(a
, "-v"))
230 else if (!strcmp(a
, "--help") || !strcmp(a
, "-h"))
232 else if (!match_str_arg(&o
->dir
, &args
, "--git-dir"))
233 usage("Unknown option", a
);
236 if (!o
->action
|| !o
->rev
)