]> git.proxmox.com Git - libgit2.git/commitdiff
Add support for blob files
authorVicent Marti <tanoku@gmail.com>
Sun, 14 Nov 2010 20:06:10 +0000 (22:06 +0200)
committerVicent Marti <tanoku@gmail.com>
Tue, 16 Nov 2010 00:59:13 +0000 (02:59 +0200)
Blob files can now be loaded from the repository like all the other base
Git types.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
src/blob.c [new file with mode: 0644]
src/blob.h [new file with mode: 0644]
src/git/blob.h [new file with mode: 0644]
src/repository.c

diff --git a/src/blob.c b/src/blob.c
new file mode 100644 (file)
index 0000000..77f5ff8
--- /dev/null
@@ -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 (file)
index 0000000..db37d2c
--- /dev/null
@@ -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 (file)
index 0000000..23afd64
--- /dev/null
@@ -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
index 8cef1821808c3818b01fc5e8868b53e6e35fc26e..4beb3e0c7bba1b9fa2220d11d294d8ef0ed2dc2f 100644 (file)
@@ -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)