summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/buffer.c28
-rw-r--r--src/buffer.h1
-rw-r--r--src/cc-compat.h8
-rw-r--r--src/errors.c71
-rw-r--r--src/notes.c39
5 files changed, 106 insertions, 41 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 24a0abdbe..0785b5399 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -146,17 +146,21 @@ int git_buf_puts(git_buf *buf, const char *string)
return git_buf_put(buf, string, strlen(string));
}
-int git_buf_printf(git_buf *buf, const char *format, ...)
+int git_buf_vprintf(git_buf *buf, const char *format, va_list ap)
{
int len;
- va_list arglist;
- ENSURE_SIZE(buf, buf->size + 1);
+ ENSURE_SIZE(buf, buf->size + (strlen(format) * 2));
while (1) {
- va_start(arglist, format);
- len = p_vsnprintf(buf->ptr + buf->size, buf->asize - buf->size, format, arglist);
- va_end(arglist);
+ va_list args;
+ va_copy(args, ap);
+
+ len = p_vsnprintf(
+ buf->ptr + buf->size,
+ buf->asize - buf->size,
+ format, args
+ );
if (len < 0) {
git__free(buf->ptr);
@@ -175,6 +179,18 @@ int git_buf_printf(git_buf *buf, const char *format, ...)
return 0;
}
+int git_buf_printf(git_buf *buf, const char *format, ...)
+{
+ int r;
+ va_list ap;
+
+ va_start(ap, format);
+ r = git_buf_vprintf(buf, format, ap);
+ va_end(ap);
+
+ return r;
+}
+
void git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf)
{
size_t copylen;
diff --git a/src/buffer.h b/src/buffer.h
index 1cf588a62..f15fdaa5d 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -76,6 +76,7 @@ int git_buf_putc(git_buf *buf, char c);
int git_buf_put(git_buf *buf, const char *data, size_t len);
int git_buf_puts(git_buf *buf, const char *string);
int git_buf_printf(git_buf *buf, const char *format, ...) GIT_FORMAT_PRINTF(2, 3);
+int git_buf_vprintf(git_buf *buf, const char *format, va_list ap);
void git_buf_clear(git_buf *buf);
void git_buf_consume(git_buf *buf, const char *end);
void git_buf_truncate(git_buf *buf, size_t len);
diff --git a/src/cc-compat.h b/src/cc-compat.h
index 507985daa..9f23dcae2 100644
--- a/src/cc-compat.h
+++ b/src/cc-compat.h
@@ -58,4 +58,12 @@
# include <stdbool.h>
#endif
+#ifndef va_copy
+# ifdef __va_copy
+# define va_copy(dst, src) __va_copy(dst, src)
+# else
+# define va_copy(dst, src) ((dst) = (src))
+# endif
+#endif
+
#endif /* INCLUDE_compat_h__ */
diff --git a/src/errors.c b/src/errors.c
index f708519ab..d43d7d9b5 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -7,6 +7,7 @@
#include "common.h"
#include "global.h"
#include "posix.h"
+#include "buffer.h"
#include <stdarg.h>
/********************************************
@@ -18,6 +19,18 @@ static git_error g_git_oom_error = {
GITERR_NOMEMORY
};
+static void set_error(int error_class, char *string)
+{
+ git_error *error = &GIT_GLOBAL->error_t;
+
+ git__free(error->message);
+
+ error->message = string;
+ error->klass = error_class;
+
+ GIT_GLOBAL->last_error = error;
+}
+
void giterr_set_oom(void)
{
GIT_GLOBAL->last_error = &g_git_oom_error;
@@ -25,66 +38,66 @@ void giterr_set_oom(void)
void giterr_set(int error_class, const char *string, ...)
{
- char error_str[1024];
+ git_buf buf = GIT_BUF_INIT;
va_list arglist;
- /* Grab errno before calling vsnprintf() so it won't be overwritten */
- const char *os_error_msg =
- (error_class == GITERR_OS && errno != 0) ? strerror(errno) : NULL;
+ int unix_error_code = 0;
+
+#ifdef GIT_WIN32
+ DWORD win32_error_code = 0;
+#endif
+
+ if (error_class == GITERR_OS) {
+ unix_error_code = errno;
+ errno = 0;
+
#ifdef GIT_WIN32
- DWORD dwLastError = GetLastError();
+ win32_error_code = GetLastError();
+ SetLastError(0);
#endif
+ }
va_start(arglist, string);
- p_vsnprintf(error_str, sizeof(error_str), string, arglist);
+ git_buf_vprintf(&buf, string, arglist);
va_end(arglist);
/* automatically suffix strerror(errno) for GITERR_OS errors */
if (error_class == GITERR_OS) {
- if (os_error_msg != NULL) {
- strncat(error_str, ": ", sizeof(error_str));
- strncat(error_str, os_error_msg, sizeof(error_str));
- errno = 0; /* reset so same error won't be reported twice */
+
+ if (unix_error_code != 0) {
+ git_buf_PUTS(&buf, ": ");
+ git_buf_puts(&buf, strerror(unix_error_code));
}
+
#ifdef GIT_WIN32
- else if (dwLastError != 0) {
+ else if (win32_error_code != 0) {
LPVOID lpMsgBuf = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, dwLastError, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
+ NULL, win32_error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
if (lpMsgBuf) {
- strncat(error_str, ": ", sizeof(error_str));
- strncat(error_str, (const char *)lpMsgBuf, sizeof(error_str));
+ git_buf_PUTS(&buf, ": ");
+ git_buf_puts(&buf, lpMsgBuf);
LocalFree(lpMsgBuf);
}
-
- SetLastError(0);
}
#endif
}
- giterr_set_str(error_class, error_str);
+ if (!git_buf_oom(&buf))
+ set_error(error_class, git_buf_detach(&buf));
}
void giterr_set_str(int error_class, const char *string)
{
- git_error *error = &GIT_GLOBAL->error_t;
+ char *message = git__strdup(string);
- git__free(error->message);
-
- error->message = git__strdup(string);
- error->klass = error_class;
-
- if (error->message == NULL) {
- giterr_set_oom();
- return;
- }
-
- GIT_GLOBAL->last_error = error;
+ if (message)
+ set_error(error_class, message);
}
void giterr_set_regex(const regex_t *regex, int error_code)
diff --git a/src/notes.c b/src/notes.c
index 05c70c643..4afdac0bd 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -9,6 +9,7 @@
#include "git2.h"
#include "refs.h"
+#include "config.h"
static int find_subtree(git_tree **subtree, const git_oid *root,
git_repository *repo, const char *target, int *fanout)
@@ -262,6 +263,25 @@ static int note_remove(git_repository *repo,
return error;
}
+static int note_get_default_ref(const char **out, git_repository *repo)
+{
+ int ret;
+ git_config *cfg;
+
+ *out = NULL;
+
+ if (git_repository_config__weakptr(&cfg, repo) < 0)
+ return -1;
+
+ ret = git_config_get_string(cfg, "core.notesRef", out);
+ if (ret == GIT_ENOTFOUND) {
+ *out = GIT_NOTES_DEFAULT_REF;
+ return 0;
+ }
+
+ return ret;
+}
+
int git_note_read(git_note **out, git_repository *repo,
const char *notes_ref, const git_oid *oid)
{
@@ -273,8 +293,8 @@ int git_note_read(git_note **out, git_repository *repo,
*out = NULL;
- if (!notes_ref)
- notes_ref = GIT_NOTES_DEFAULT_REF;
+ if (!notes_ref && note_get_default_ref(&notes_ref, repo) < 0)
+ return -1;
error = git_reference_lookup(&ref, repo, notes_ref);
if (error < 0)
@@ -314,8 +334,8 @@ int git_note_create(
git_commit *commit = NULL;
git_reference *ref;
- if (!notes_ref)
- notes_ref = GIT_NOTES_DEFAULT_REF;
+ if (!notes_ref && note_get_default_ref(&notes_ref, repo) < 0)
+ return -1;
error = git_reference_lookup(&ref, repo, notes_ref);
if (error < 0 && error != GIT_ENOTFOUND)
@@ -359,8 +379,9 @@ int git_note_remove(git_repository *repo, const char *notes_ref,
git_commit *commit;
git_reference *ref;
- if (!notes_ref)
- notes_ref = GIT_NOTES_DEFAULT_REF;
+
+ if (!notes_ref && note_get_default_ref(&notes_ref, repo) < 0)
+ return -1;
error = git_reference_lookup(&ref, repo, notes_ref);
if (error < 0)
@@ -388,6 +409,12 @@ int git_note_remove(git_repository *repo, const char *notes_ref,
return error;
}
+int git_note_default_ref(const char **out, git_repository *repo)
+{
+ assert(repo);
+ return note_get_default_ref(out, repo);
+}
+
const char * git_note_message(git_note *note)
{
assert(note);