libgit-thin: Update QUESTIONS file
[git/libgit-gsoc.git] / libgit-thin / ltrepo.c
blob5ef914140764a82ce9de849f73cf18590414403c
1 /**
2 * @file
3 *
4 * Repository handling functions.
5 *
6 * Luiz Fernando N. Capitulino
7 * <lcapitulino@gmail.com>
8 */
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <errno.h>
13 #include <cache.h>
15 #include "ltrepo.h"
17 /**
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)
29 int err;
31 if (!path) {
32 errno = EINVAL;
33 return -1;
36 err = chdir(path);
37 if (err)
38 return -1;
40 return git_config(git_default_config);
43 /**
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)
53 if (!ref || !sha1) {
54 errno = EINVAL;
55 return -1;
58 return get_sha1(ref, sha1);
61 /**
62 * Get repository's HEAD SHA1
64 * \param sha1 pointer to a 20-byte array to return the HEAD
65 * SHA1 into
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)
77 void *p;
78 int mtype;
79 unsigned long msize;
81 if (!sha1 || (!buf && !size))
82 goto out_err;
84 p = read_sha1_file(sha1, &mtype, &msize);
85 if (!p)
86 return -1;
88 if (mtype != type) {
89 free(p);
90 goto out_err;
93 if (buf)
94 *buf = p;
95 if (size)
96 *size = (size_t) msize;
98 return 0;
100 out_err:
101 errno = EINVAL;
102 return -1;
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);