int git_reference_normalize_name( char *buffer_out, size_t buffer_size, const char *name, unsigned int flags) { git_buf buf = GIT_BUF_INIT; int error; if ((error = git_reference__normalize_name(&buf, name, flags)) < 0) goto cleanup; if (git_buf_len(&buf) > buffer_size - 1) { giterr_set( GITERR_REFERENCE, "The provided buffer is too short to hold the normalization of '%s'", name); error = GIT_EBUFS; goto cleanup; } git_buf_copy_cstr(buffer_out, buffer_size, &buf); error = 0; cleanup: git_buf_free(&buf); return error; }
int git_message_prettify(char *message_out, size_t buffer_size, const char *message, int strip_comments) { git_buf buf = GIT_BUF_INIT; ssize_t out_size = -1; if (message_out && buffer_size) *message_out = '\0'; if (git_message__prettify(&buf, message, strip_comments) < 0) goto done; if (message_out && buf.size + 1 > buffer_size) { /* +1 for NUL byte */ giterr_set(GITERR_INVALID, "Buffer too short to hold the cleaned message"); goto done; } if (message_out) git_buf_copy_cstr(message_out, buffer_size, &buf); out_size = buf.size + 1; done: git_buf_free(&buf); return (int)out_size; }
int git_branch_upstream_name( char *tracking_branch_name_out, size_t buffer_size, git_repository *repo, const char *canonical_branch_name) { git_buf buf = GIT_BUF_INIT; int error; assert(canonical_branch_name); if (tracking_branch_name_out && buffer_size) *tracking_branch_name_out = '\0'; if ((error = git_branch_upstream__name( &buf, repo, canonical_branch_name)) < 0) goto cleanup; if (tracking_branch_name_out && buf.size + 1 > buffer_size) { /* +1 for NUL byte */ giterr_set( GITERR_INVALID, "Buffer too short to hold the tracked reference name."); error = -1; goto cleanup; } if (tracking_branch_name_out) git_buf_copy_cstr(tracking_branch_name_out, buffer_size, &buf); error = (int)buf.size + 1; cleanup: git_buf_free(&buf); return (int)error; }
const char *cl_git_sandbox_path(int is_dir, ...) { const char *path = NULL; static char _temp[GIT_PATH_MAX]; git_buf buf = GIT_BUF_INIT; va_list arg; cl_git_pass(git_buf_sets(&buf, clar_sandbox_path())); va_start(arg, is_dir); while ((path = va_arg(arg, const char *)) != NULL) { cl_git_pass(git_buf_joinpath(&buf, buf.ptr, path)); } va_end(arg); cl_git_pass(git_path_prettify(&buf, buf.ptr, NULL)); if (is_dir) git_path_to_dir(&buf); /* make sure we won't truncate */ cl_assert(git_buf_len(&buf) < sizeof(_temp)); git_buf_copy_cstr(_temp, sizeof(_temp), &buf); git_buf_dispose(&buf); return _temp; }
int git_repository_discover( char *repository_path, size_t size, const char *start_path, int across_fs, const char *ceiling_dirs) { git_buf path = GIT_BUF_INIT; uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0; int error; assert(start_path && repository_path && size > 0); *repository_path = '\0'; if ((error = find_repo(&path, NULL, start_path, flags, ceiling_dirs)) < 0) return error != GIT_ENOTFOUND ? -1 : error; if (size < (size_t)(path.size + 1)) { giterr_set(GITERR_REPOSITORY, "The given buffer is too long to store the discovered path"); git_buf_free(&path); return -1; } /* success: we discovered a repository */ git_buf_copy_cstr(repository_path, size, &path); git_buf_free(&path); return 0; }
int git_futils_dirs_get_str(char *out, size_t outlen, git_futils_dir_t which) { const git_buf *path = NULL; GITERR_CHECK_ERROR(git_futils_check_selector(which)); GITERR_CHECK_ERROR(git_futils_dirs_get(&path, which)); if (!out || path->size >= outlen) { giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path"); return GIT_EBUFS; } git_buf_copy_cstr(out, outlen, path); return 0; }
int git_branch_remote_name(char *buffer, size_t buffer_len, git_repository *repo, const char *refname) { int ret; git_buf buf = GIT_BUF_INIT; if ((ret = remote_name(&buf, repo, refname)) < 0) return ret; if (buffer) git_buf_copy_cstr(buffer, buffer_len, &buf); ret = (int)git_buf_len(&buf) + 1; git_buf_free(&buf); return ret; }
/* more thorough test of concatenation options */ void test_core_buffer__2(void) { git_buf buf = GIT_BUF_INIT; int i; char data[128]; cl_assert(buf.size == 0); /* this must be safe to do */ git_buf_free(&buf); cl_assert(buf.size == 0); cl_assert(buf.asize == 0); /* empty buffer should be empty string */ cl_assert_equal_s("", git_buf_cstr(&buf)); cl_assert(buf.size == 0); /* cl_assert(buf.asize == 0); -- should not assume what git_buf does */ /* free should set us back to the beginning */ git_buf_free(&buf); cl_assert(buf.size == 0); cl_assert(buf.asize == 0); /* add letter */ git_buf_putc(&buf, '+'); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s("+", git_buf_cstr(&buf)); /* add letter again */ git_buf_putc(&buf, '+'); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s("++", git_buf_cstr(&buf)); /* let's try that a few times */ for (i = 0; i < 16; ++i) { git_buf_putc(&buf, '+'); cl_assert(git_buf_oom(&buf) == 0); } cl_assert_equal_s("++++++++++++++++++", git_buf_cstr(&buf)); git_buf_free(&buf); /* add data */ git_buf_put(&buf, "xo", 2); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s("xo", git_buf_cstr(&buf)); /* add letter again */ git_buf_put(&buf, "xo", 2); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s("xoxo", git_buf_cstr(&buf)); /* let's try that a few times */ for (i = 0; i < 16; ++i) { git_buf_put(&buf, "xo", 2); cl_assert(git_buf_oom(&buf) == 0); } cl_assert_equal_s("xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxo", git_buf_cstr(&buf)); git_buf_free(&buf); /* set to string */ git_buf_sets(&buf, test_string); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(test_string, git_buf_cstr(&buf)); /* append string */ git_buf_puts(&buf, test_string); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(test_string_x2, git_buf_cstr(&buf)); /* set to string again (should overwrite - not append) */ git_buf_sets(&buf, test_string); cl_assert(git_buf_oom(&buf) == 0); cl_assert_equal_s(test_string, git_buf_cstr(&buf)); /* test clear */ git_buf_clear(&buf); cl_assert_equal_s("", git_buf_cstr(&buf)); git_buf_free(&buf); /* test extracting data into buffer */ git_buf_puts(&buf, REP4("0123456789")); cl_assert(git_buf_oom(&buf) == 0); git_buf_copy_cstr(data, sizeof(data), &buf); cl_assert_equal_s(REP4("0123456789"), data); git_buf_copy_cstr(data, 11, &buf); cl_assert_equal_s("0123456789", data); git_buf_copy_cstr(data, 3, &buf); cl_assert_equal_s("01", data); git_buf_copy_cstr(data, 1, &buf); cl_assert_equal_s("", data); git_buf_copy_cstr(data, sizeof(data), &buf); cl_assert_equal_s(REP4("0123456789"), data); git_buf_sets(&buf, REP256("x")); git_buf_copy_cstr(data, sizeof(data), &buf); /* since sizeof(data) == 128, only 127 bytes should be copied */ cl_assert_equal_s(REP4(REP16("x")) REP16("x") REP16("x") REP16("x") "xxxxxxxxxxxxxxx", data); git_buf_free(&buf); git_buf_copy_cstr(data, sizeof(data), &buf); cl_assert_equal_s("", data); }