summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2010-11-20 17:37:32 +0200
committerVicent Marti <tanoku@gmail.com>2010-11-20 17:37:32 +0200
commit30b171a185a1ef788d41265f08ad1da98d72918a (patch)
treeb1e2de22198c7832fc9ab9d3563259c510181fc2 /src
parent69a09b7c6972343e321c7c0e3441fcc4b5340444 (diff)
downloadlibgit2-30b171a185a1ef788d41265f08ad1da98d72918a.tar.gz
Change blob API to return temp refs to the content
If the user wants permanent references, he can duplicate the temporary one manually. Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/blob.c35
-rw-r--r--src/git/blob.h15
2 files changed, 18 insertions, 32 deletions
diff --git a/src/blob.c b/src/blob.c
index 818f838d2..c301ae15d 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -30,35 +30,20 @@
#include "common.h"
#include "blob.h"
-int git_blob_rawcontent(git_blob *blob, void *buffer, size_t len)
+const char *git_blob_rawcontent(git_blob *blob)
{
- 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;
+ assert(blob);
- } else {
-
- if (len + 1 < blob->object.source.raw.len)
- return GIT_ENOMEM;
-
- if ((error = git_object__source_open((git_object *)blob)) < 0)
- return error;
+ if (blob->content.data != NULL)
+ return blob->content.data;
- memcpy(buffer, blob->object.source.raw.data, blob->object.source.raw.len);
- ((char *)buffer)[blob->object.source.raw.len] = 0;
+ if (blob->object.in_memory)
+ return NULL;
- git_object__source_close((git_object *)blob);
- }
+ if (!blob->object.source.open && git_object__source_open((git_object *)blob) < 0)
+ return NULL;
- return GIT_SUCCESS;
+ return blob->object.source.raw.data;
}
int git_blob_rawsize(git_blob *blob)
@@ -99,6 +84,8 @@ int git_blob_set_rawcontent(git_blob *blob, const void *buffer, size_t len)
blob->object.modified = 1;
+ git_object__source_close((git_object *)blob);
+
if (blob->content.data != NULL)
gitfo_free_buf(&blob->content);
diff --git a/src/git/blob.h b/src/git/blob.h
index 23afd64a9..93ced42ee 100644
--- a/src/git/blob.h
+++ b/src/git/blob.h
@@ -63,18 +63,17 @@ GIT_EXTERN(int) git_blob_set_rawcontent_fromfile(git_blob *blob, const char *fil
GIT_EXTERN(int) git_blob_set_rawcontent(git_blob *blob, const void *buffer, size_t len);
/**
- * Read the raw content of a blob.
+ * Get a read-only buffer with 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.
+ * A pointer to the raw content of a blob is returned;
+ * this pointer is owned internally by the object and shall
+ * not be free'd. The pointer may be invalidated at a later
+ * time (e.g. when changing the contents of the blob).
*
* @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
+ * @return the pointer; NULL if the blob has no contents
*/
-GIT_EXTERN(int) git_blob_rawcontent(git_blob *blob, void *buffer, size_t len);
+GIT_EXTERN(const char *) git_blob_rawcontent(git_blob *blob);
/**
* Get the size in bytes of the contents of a blob