diff options
author | Vicent Marti <tanoku@gmail.com> | 2010-11-14 23:06:10 +0300 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2010-11-16 03:59:13 +0300 |
commit | 237da40181daa416e25527aee35173a1cd5ad92b (patch) | |
tree | c9340f5c6b4ae99c3a871a1cf50490aec3facc6a /src | |
parent | 0be421994e34e2d8e200f15b64894dd0c09c4eec (diff) |
Add support for blob files
Blob files can now be loaded from the repository like all the other base
Git types.
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/blob.c | 146 | ||||
-rw-r--r-- | src/blob.h | 17 | ||||
-rw-r--r-- | src/git/blob.h | 100 | ||||
-rw-r--r-- | src/repository.c | 16 |
4 files changed, 277 insertions, 2 deletions
diff --git a/src/blob.c b/src/blob.c new file mode 100644 index 000000000..77f5ff887 --- /dev/null +++ b/src/blob.c @@ -0,0 +1,146 @@ +/* + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, + * as published by the Free Software Foundation. + * + * In addition to the permissions in the GNU General Public License, + * the authors give you unlimited permission to link the compiled + * version of this file into combinations with other programs, + * and to distribute those combinations without any restriction + * coming from the use of this file. (The General Public License + * restrictions do apply in other respects; for example, they cover + * modification of the file, and distribution when not linked into + * a combined executable.) + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "git/common.h" +#include "git/odb.h" +#include "git/repository.h" + +#include "common.h" +#include "blob.h" + +int git_blob_rawcontent(git_blob *blob, void *buffer, size_t len) +{ + int error; + + assert(blob && buffer); + + if (blob->content.data != NULL) { + + if (len + 1 < blob->content.len) + return GIT_ENOMEM; + + memcpy(buffer, blob->content.data, blob->content.len); + ((char *)buffer)[blob->content.len] = 0; + + } else { + + if (len + 1 < blob->object.source.raw.len) + return GIT_ENOMEM; + + if ((error = git_object__source_open((git_object *)blob)) < 0) + return error; + + memcpy(buffer, blob->object.source.raw.data, blob->object.source.raw.len); + ((char *)buffer)[blob->object.source.raw.len] = 0; + + git_object__source_close((git_object *)blob); + } + + return GIT_SUCCESS; +} + +int git_blob_rawsize(git_blob *blob) +{ + assert(blob); + + if (blob->content.data != NULL) + return blob->content.len; + + return blob->object.source.raw.len; +} + +void git_blob__free(git_blob *blob) +{ + gitfo_free_buf(&blob->content); +} + +int git_blob__parse(git_blob *blob) +{ + assert(blob); + return GIT_SUCCESS; +} + +int git_blob__writeback(git_blob *blob, git_odb_source *src) +{ + assert(blob->object.modified); + + if (blob->content.data == NULL) + return GIT_EMISSINGOBJDATA; + + return git__source_write(src, blob->content.data, blob->content.len); +} + +int git_blob_set_rawcontent(git_blob *blob, const void *buffer, size_t len) +{ + assert(blob && buffer); + + blob->object.modified = 1; + + if (blob->content.data != NULL) + gitfo_free_buf(&blob->content); + + blob->content.data = git__malloc(len); + blob->content.len = len; + + if (blob->content.data == NULL) + return GIT_ENOMEM; + + memcpy(blob->content.data, buffer, len); + + return GIT_SUCCESS; +} + +int git_blob_set_rawcontent_fromfile(git_blob *blob, const char *filename) +{ + assert(blob && filename); + blob->object.modified = 1; + + if (blob->content.data != NULL) + gitfo_free_buf(&blob->content); + + return gitfo_read_file(&blob->content, filename); +} + +int git_blob_writefile(git_oid *written_id, git_repository *repo, const char *path) +{ + int error; + git_blob *blob; + + if (gitfo_exists(path) < 0) + return GIT_ENOTFOUND; + + if ((error = git_blob_new(&blob, repo)) < 0) + return error; + + if ((error = git_blob_set_rawcontent_fromfile(blob, path)) < 0) + return error; + + if ((error = git_object_write((git_object *)blob)) < 0) + return error; + + git_oid_cpy(written_id, git_object_id((git_object *)blob)); + return GIT_SUCCESS; +} + diff --git a/src/blob.h b/src/blob.h new file mode 100644 index 000000000..db37d2c24 --- /dev/null +++ b/src/blob.h @@ -0,0 +1,17 @@ +#ifndef INCLUDE_blob_h__ +#define INCLUDE_blob_h__ + +#include "git/blob.h" +#include "repository.h" +#include "fileops.h" + +struct git_blob { + git_object object; + gitfo_buf content; +}; + +void git_blob__free(git_blob *blob); +int git_blob__parse(git_blob *blob); +int git_blob__writeback(git_blob *blob, git_odb_source *src); + +#endif diff --git a/src/git/blob.h b/src/git/blob.h new file mode 100644 index 000000000..23afd64a9 --- /dev/null +++ b/src/git/blob.h @@ -0,0 +1,100 @@ +#ifndef INCLUDE_git_blob_h__ +#define INCLUDE_git_blob_h__ + +#include "common.h" +#include "oid.h" + +/** + * @file git/blob.h + * @brief Git blob load and write routines + * @defgroup git_blob Git blob load and write routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** In-memory representation of a blob object. */ +typedef struct git_blob git_blob; + +/** + * Lookup a blob object from a repository. + * The generated blob object is owned by the revision + * repo and shall not be freed by the user. + * + * @param blob pointer to the looked up blob + * @param repo the repo to use when locating the blob. + * @param id identity of the blob to locate. + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git_oid *id); + +/** + * Create a new in-memory git_blob. + * + * The blob object must be manually filled using + * the 'set_rawcontent' methods before it can + * be written back to disk. + * + * @param blob pointer to the new blob + * @param repo The repository where the object will reside + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_blob_new(git_blob **blob, git_repository *repo); + +/** + * Fill a blob with the contents inside + * the pointed file. + * + * @param blob pointer to the new blob + * @param filename name of the file to read + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_blob_set_rawcontent_fromfile(git_blob *blob, const char *filename); + +/** + * Fill a blob with the contents inside + * the pointed buffer + * + * @param blob pointer to the blob + * @param buffer buffer with the contents for the blob + * @param len size of the buffer + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_blob_set_rawcontent(git_blob *blob, const void *buffer, size_t len); + +/** + * Read the raw content of a blob. + * + * A copy of the raw content is stored on the buffer passed + * to the function. If the buffer is not long enough, + * the method will fail. + * + * @param blob pointer to the blob + * @param buffer buffer to fill with contents + * @param len size of the buffer + * @return 0 on success; error code otherwise + */ +GIT_EXTERN(int) git_blob_rawcontent(git_blob *blob, void *buffer, size_t len); + +/** + * Get the size in bytes of the contents of a blob + * + * @param blob pointer to the blob + * @return size on bytes + */ +GIT_EXTERN(int) git_blob_rawsize(git_blob *blob); + +/** + * Read a file from the working folder of a repository + * and write it to the Object Database as a loose blob, + * if such doesn't exist yet. + * + * @param written_id return the id of the written blob + * @param repo repository where the blob will be written + * @param path file from which the blob will be created + */ +GIT_EXTERN(int) git_blob_writefile(git_oid *written_id, git_repository *repo, const char *path); + +/** @} */ +GIT_END_DECL +#endif diff --git a/src/repository.c b/src/repository.c index 8cef18218..4beb3e0c7 100644 --- a/src/repository.c +++ b/src/repository.c @@ -28,6 +28,7 @@ #include "repository.h" #include "commit.h" #include "tag.h" +#include "blob.h" #include "fileops.h" static const int default_table_size = 32; @@ -39,7 +40,7 @@ static const size_t object_sizes[] = { 0, sizeof(git_commit), sizeof(git_tree), - sizeof(git_object), /* TODO: sizeof(git_blob) */ + sizeof(git_blob), sizeof(git_tag) }; @@ -371,6 +372,10 @@ int git_object_write(git_object *object) error = git_tag__writeback((git_tag *)object, source); break; + case GIT_OBJ_BLOB: + error = git_blob__writeback((git_blob *)object, source); + break; + default: error = GIT_ERROR; break; @@ -404,6 +409,10 @@ void git_object_free(git_object *object) git_tag__free((git_tag *)object); break; + case GIT_OBJ_BLOB: + git_blob__free((git_blob *)object); + break; + default: free(object); break; @@ -524,8 +533,10 @@ int git_repository_lookup(git_object **object_out, git_repository *repo, const g break; case GIT_OBJ_BLOB: + error = git_blob__parse((git_blob *)object); + break; + default: - /* blobs get no parsing */ break; } @@ -550,5 +561,6 @@ int git_repository_lookup(git_object **object_out, git_repository *repo, const g GIT_NEWOBJECT_TEMPLATE(commit, COMMIT) GIT_NEWOBJECT_TEMPLATE(tag, TAG) GIT_NEWOBJECT_TEMPLATE(tree, TREE) +GIT_NEWOBJECT_TEMPLATE(blob, BLOB) |