@@ -150,6 +150,78 @@ int error = git_repository_open_ext(&repo, "/tmp/…",
150
150
151
151
152
152
153
+ ## Objects
154
+
155
+ ### SHAs and OIDs
156
+
157
+ SHA-1 hashes are usually written as 40 characters of hexadecimal.
158
+ These are converted to a binary representation internally, called ` git_oid ` , and there are routines for converting back and forth.
159
+
160
+ ``` c
161
+ /* Convert a SHA to an OID */
162
+ const char *sha = " 4a202b346bb0fb0db7eff3cffeb3c70babbd2045" ;
163
+ git_oid oid = {{0}};
164
+ int error = git_oid_fromstr(&oid, sha);
165
+
166
+ /* Make a shortened printable string from an OID * /
167
+ char shortsha[ 10] = {0};
168
+ git_oid_tostr (shortsha, 9, &oid);
169
+
170
+ /* Or libgit2 can allocate a buffer for you * /
171
+ char * newsha = git_oid_allocfmt(&oid);
172
+ /* … * /
173
+ free (newsha);
174
+ ```
175
+
176
+ ([`git_oid_fromstr`](http://libgit2.github.com/libgit2/#HEAD/group/oid/git_oid_fromstr),
177
+ [`git_oid_tostr`](http://libgit2.github.com/libgit2/#HEAD/group/oid/git_oid_tostr),
178
+ [`git_oid_allocfmt`](http://libgit2.github.com/libgit2/#HEAD/group/oid/git_oid_allocfmt))
179
+
180
+
181
+ ### Lookups
182
+
183
+ There are four kinds of objects in a Git repository – commits, trees, blobs, and tag annotations.
184
+ Each type of object has an API for doing lookups.
185
+
186
+ ```c
187
+ git_commit *commit;
188
+ int error = git_commit_lookup(&commit, repo, &oid);
189
+
190
+ git_tree *tree;
191
+ error = git_tree_lookup(&tree, repo, &oid);
192
+
193
+ git_blob *blob;
194
+ error = git_blob_lookup(&blob, repo, &oid);
195
+
196
+ git_tag tag;
197
+ error = git_tag_lookup(&tag, repo, &oid);
198
+ ```
199
+
200
+ ([ ` git_commit_lookup ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/commit/git_commit_lookup ) ,
201
+ [ ` git_tree_lookup ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/tree/git_tree_lookup ) ,
202
+ [ ` git_blob_lookup ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/blob/git_blob_lookup ) ,
203
+ [ ` git_tag_lookup ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/tag/git_tag_lookup ) )
204
+
205
+
206
+ ### Casting
207
+
208
+ ` git_object ` acts like a "base class" for all of these types.
209
+
210
+ ``` c
211
+ git_object *obj;
212
+ int error = git_object_lookup(&obj, repo, &oid, GIT_OBJ_ANY);
213
+ if (git_object_type(obj) == GIT_OBJ_COMMIT) {
214
+ /* This is relatively safe * /
215
+ git_commit * commit = (git_commit* )obj;
216
+ }
217
+ /* etc. */
218
+ ```
219
+
220
+ ([ ` git_object_lookup ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/object/git_object_lookup ) ,
221
+ [ ` git_object_type ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/object/git_object_type ) ,
222
+ [ ` git_otype ` ] ( http://libgit2.github.com/libgit2/#HEAD/type/git_otype ) )
223
+
224
+
153
225
## Diff
154
226
155
227
### Index to Workdir
0 commit comments