4 * Repository handling functions.
6 * Luiz Fernando N. Capitulino
7 * <lcapitulino@gmail.com>
18 * Open a GIT repository.
20 * This function must be called before anything else, note that it
21 * does change the process' current working directory.
23 * \param path relative or absolute repository's path
25 * \returns 0 on success, -1 otherwise.
27 int git_repo_open(const char *path
)
40 return git_config(git_default_config
);
44 * Translate a repository's 'ref' into a SHA1
46 * \param ref ref to be translated
47 * \param sha1 pointer to a 20-byte array to return the SHA1 into
49 * \returns 0 on success, -1 on error.
51 int git_repo_translate_ref(const char *ref
, unsigned char *sha1
)
58 return get_sha1(ref
, sha1
);
62 * Get repository's HEAD SHA1
64 * \param sha1 pointer to a 20-byte array to return the HEAD
67 * \returns 0 on success, -1 on error.
69 int git_repo_head(unsigned char *sha1
)
71 return git_repo_translate_ref("HEAD", sha1
);
74 static int repo_read_object(unsigned char *sha1
, int type
,
75 void **buf
, size_t *size
)
81 if (!sha1
|| (!buf
&& !size
))
84 p
= read_sha1_file(sha1
, &mtype
, &msize
);
96 *size
= (size_t) msize
;
106 * Read blob's contents into memory.
108 * \param sha1 blob's SHA1
109 * \param buf pointer to return the blob's contents (should be freed
110 * with free(), might be NULL)
111 * \param size pointer to return the blob's size (might be NULL)
113 * \returns 0 on success, -1 otherwise
115 int git_repo_blob_read(unsigned char *sha1
, void **buf
, size_t *size
)
117 return repo_read_object(sha1
, OBJ_BLOB
, buf
, size
);
121 * Read commit's contents into memory.
123 * \param sha1 commit's SHA1
124 * \param buf pointer to return the commit's contents (should be
125 * freed with free(), might be NULL)
126 * \param size pointer to return the commit's size (might be NULL)
128 * \returns 0 on success, -1 otherwise
130 int git_repo_commit_read(unsigned char *sha1
, void **buf
, size_t *size
)
132 return repo_read_object(sha1
, OBJ_COMMIT
, buf
, size
);
136 * Read tree's contents into memory.
138 * \param sha1 tree's SHA1
139 * \param buf pointer to return the tree's contents (should be
140 * freed with free(), might be NULL)
141 * \param size pointer to return the tree's size (might be NULL)
143 * \returns 0 on success, -1 otherwise
145 int git_repo_tree_read(unsigned char *sha1
, void **buf
, size_t *size
)
147 return repo_read_object(sha1
, OBJ_TREE
, buf
, size
);
151 * Read tag's contents into memory.
153 * \param sha1 tag's SHA1
154 * \param buf pointer to return the tag's contents into (should be
155 * freed with free(), might be NULL)
156 * \param size pointer to return the tag's size (might be NULL)
158 * \returns 0 on success, -1 otherwise
160 int git_repo_tag_read(unsigned char *sha1
, void **buf
, size_t *size
)
162 return repo_read_object(sha1
, OBJ_TAG
, buf
, size
);