diff --git a/include/git2/branch.h b/include/git2/branch.h index 44d6fd9c3..851de290a 100644 --- a/include/git2/branch.h +++ b/include/git2/branch.h @@ -200,25 +200,20 @@ GIT_EXTERN(int) git_branch_set_upstream(git_reference *branch, const char *upstr * Return the name of the reference supporting the remote tracking branch, * given the name of a local branch reference. * - * @param tracking_branch_name_out The user-allocated buffer which will be - * filled with the name of the reference. Pass NULL if you just want to - * get the needed size of the name of the reference as the output value. - * - * @param buffer_size Size of the `out` buffer in bytes. + * @param out Pointer to the user-allocated git_buf which will be + * filled with the name of the reference. * * @param repo the repository where the branches live * - * @param canonical_branch_name name of the local branch. + * @param refname reference name of the local branch. * - * @return number of characters in the reference name - * including the trailing NUL byte; GIT_ENOTFOUND when no remote tracking - * reference exists, otherwise an error code. + * @return 0, GIT_ENOTFOUND when no remote tracking reference exists, + * otherwise an error code. */ GIT_EXTERN(int) git_branch_upstream_name( - char *tracking_branch_name_out, - size_t buffer_size, + git_buf *out, git_repository *repo, - const char *canonical_branch_name); + const char *refname); /** * Determine if the current local branch is pointed at by HEAD. @@ -234,25 +229,19 @@ GIT_EXTERN(int) git_branch_is_head( /** * Return the name of remote that the remote tracking branch belongs to. * - * @param remote_name_out The user-allocated buffer which will be - * filled with the name of the remote. Pass NULL if you just want to - * get the needed size of the name of the remote as the output value. - * - * @param buffer_size Size of the `out` buffer in bytes. + * @param out Pointer to the user-allocated git_buf which will be filled iwth the name of the remote. * * @param repo The repository where the branch lives. * * @param canonical_branch_name name of the remote tracking branch. * - * @return Number of characters in the reference name - * including the trailing NUL byte; GIT_ENOTFOUND + * @return 0, GIT_ENOTFOUND * when no remote matching remote was found, * GIT_EAMBIGUOUS when the branch maps to several remotes, * otherwise an error code. */ GIT_EXTERN(int) git_branch_remote_name( - char *remote_name_out, - size_t buffer_size, + git_buf *out, git_repository *repo, const char *canonical_branch_name); diff --git a/include/git2/config.h b/include/git2/config.h index f650f1b41..663b4f6ba 100644 --- a/include/git2/config.h +++ b/include/git2/config.h @@ -9,6 +9,7 @@ #include "common.h" #include "types.h" +#include "buffer.h" /** * @file git2/config.h @@ -90,11 +91,10 @@ typedef struct { * This method will not guess the path to the xdg compatible * config file (.config/git/config). * - * @param out Buffer to store the path in - * @param length size of the buffer in bytes - * @return 0 if a global configuration file has been found. Its path will be stored in `buffer`. + * @param out Pointer to a user-allocated git_buf in which to store the path + * @return 0 if a global configuration file has been found. Its path will be stored in `out`. */ -GIT_EXTERN(int) git_config_find_global(char *out, size_t length); +GIT_EXTERN(int) git_config_find_global(git_buf *out); /** * Locate the path to the global xdg compatible configuration file @@ -107,25 +107,23 @@ GIT_EXTERN(int) git_config_find_global(char *out, size_t length); * may be used on any `git_config` call to load the * xdg compatible configuration file. * - * @param out Buffer to store the path in - * @param length size of the buffer in bytes + * @param out Pointer to a user-allocated git_buf in which to store the path * @return 0 if a xdg compatible configuration file has been - * found. Its path will be stored in `buffer`. + * found. Its path will be stored in `out`. */ -GIT_EXTERN(int) git_config_find_xdg(char *out, size_t length); +GIT_EXTERN(int) git_config_find_xdg(git_buf *out); /** * Locate the path to the system configuration file * * If /etc/gitconfig doesn't exist, it will look for * %PROGRAMFILES%\Git\etc\gitconfig. - - * @param out Buffer to store the path in - * @param length size of the buffer in bytes + * + * @param out Pointer to a user-allocated git_buf in which to store the path * @return 0 if a system configuration file has been - * found. Its path will be stored in `buffer`. + * found. Its path will be stored in `out`. */ -GIT_EXTERN(int) git_config_find_system(char *out, size_t length); +GIT_EXTERN(int) git_config_find_system(git_buf *out); /** * Open the global, XDG and system configuration files diff --git a/include/git2/message.h b/include/git2/message.h index 395c88690..bcdb72f6a 100644 --- a/include/git2/message.h +++ b/include/git2/message.h @@ -8,6 +8,7 @@ #define INCLUDE_git_message_h__ #include "common.h" +#include "buffer.h" /** * @file git2/message.h @@ -23,25 +24,17 @@ GIT_BEGIN_DECL * * Optionally, can remove lines starting with a "#". * - * @param out The user-allocated buffer which will be filled with the - * cleaned up message. Pass NULL if you just want to get the needed - * size of the prettified message as the output value. - * - * @param out_size Size of the `out` buffer in bytes. + * @param out The user-allocated git_buf which will be filled with the + * cleaned up message. * * @param message The message to be prettified. * * @param strip_comments Non-zero to remove lines starting with "#", 0 to * leave them in. * - * @return -1 on error, else number of characters in prettified message - * including the trailing NUL byte + * @return 0 or an error code. */ -GIT_EXTERN(int) git_message_prettify( - char *out, - size_t out_size, - const char *message, - int strip_comments); +GIT_EXTERN(int) git_message_prettify(git_buf *out, const char *message, int strip_comments); /** @} */ GIT_END_DECL diff --git a/include/git2/refspec.h b/include/git2/refspec.h index d96b83ce2..9acdc72d5 100644 --- a/include/git2/refspec.h +++ b/include/git2/refspec.h @@ -10,6 +10,7 @@ #include "common.h" #include "types.h" #include "net.h" +#include "buffer.h" /** * @file git2/refspec.h @@ -82,23 +83,21 @@ GIT_EXTERN(int) git_refspec_dst_matches(const git_refspec *refspec, const char * * Transform a reference to its target following the refspec's rules * * @param out where to store the target name - * @param outlen the size of the `out` buffer * @param spec the refspec * @param name the name of the reference to transform * @return 0, GIT_EBUFS or another error */ -GIT_EXTERN(int) git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name); +GIT_EXTERN(int) git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name); /** * Transform a target reference to its source reference following the refspec's rules * * @param out where to store the source reference name - * @param outlen the size of the `out` buffer * @param spec the refspec * @param name the name of the reference to transform * @return 0, GIT_EBUFS or another error */ -GIT_EXTERN(int) git_refspec_rtransform(char *out, size_t outlen, const git_refspec *spec, const char *name); +GIT_EXTERN(int) git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name); GIT_END_DECL diff --git a/include/git2/repository.h b/include/git2/repository.h index 9f71d2959..a0453da5c 100644 --- a/include/git2/repository.h +++ b/include/git2/repository.h @@ -10,6 +10,7 @@ #include "common.h" #include "types.h" #include "oid.h" +#include "buffer.h" /** * @file git2/repository.h @@ -58,10 +59,8 @@ GIT_EXTERN(int) git_repository_wrap_odb(git_repository **out, git_odb *odb); * The method will automatically detect if the repository is bare * (if there is a repository). * - * @param path_out The user allocated buffer which will - * contain the found path. - * - * @param path_size repository_path size + * @param out A pointer to a user-allocated git_buf which will contain + * the found path. * * @param start_path The base path where the lookup starts. * @@ -77,8 +76,7 @@ GIT_EXTERN(int) git_repository_wrap_odb(git_repository **out, git_odb *odb); * @return 0 or an error code */ GIT_EXTERN(int) git_repository_discover( - char *path_out, - size_t path_size, + git_buf *out, const char *start_path, int across_fs, const char *ceiling_dirs); @@ -464,21 +462,11 @@ GIT_EXTERN(int) git_repository_index(git_index **out, git_repository *repo); * Use this function to get the contents of this file. Don't forget to * remove the file after you create the commit. * - * If the repository message exists and there are no errors reading it, this - * returns the bytes needed to store the message in memory (i.e. message - * file size plus one terminating NUL byte). That value is returned even if - * `out` is NULL or `len` is shorter than the necessary size. - * - * The `out` buffer will *always* be NUL terminated, even if truncation - * occurs. - * - * @param out Buffer to write data into or NULL to just read required size - * @param len Length of `out` buffer in bytes + * @param out git_buf to write data into * @param repo Repository to read prepared message from - * @return GIT_ENOTFOUND if no message exists, other value < 0 for other - * errors, or total bytes in message (may be > `len`) on success + * @return 0, GIT_ENOTFOUND if no message exists or an error code */ -GIT_EXTERN(int) git_repository_message(char *out, size_t len, git_repository *repo); +GIT_EXTERN(int) git_repository_message(git_buf *out, git_repository *repo); /** * Remove git's prepared message. diff --git a/src/branch.c b/src/branch.c index 3b9aa0d20..a1a04b2b4 100644 --- a/src/branch.c +++ b/src/branch.c @@ -286,10 +286,10 @@ static int retrieve_upstream_configuration( return error; } -int git_branch_upstream__name( - git_buf *tracking_name, +int git_branch_upstream_name( + git_buf *out, git_repository *repo, - const char *canonical_branch_name) + const char *refname) { const char *remote_name, *merge_name; git_buf buf = GIT_BUF_INIT; @@ -297,22 +297,24 @@ int git_branch_upstream__name( git_remote *remote = NULL; const git_refspec *refspec; - assert(tracking_name && canonical_branch_name); + assert(out && refname); - if (!git_reference__is_branch(canonical_branch_name)) - return not_a_local_branch(canonical_branch_name); + git_buf_sanitize(out); + + if (!git_reference__is_branch(refname)) + return not_a_local_branch(refname); if ((error = retrieve_upstream_configuration( - &remote_name, repo, canonical_branch_name, "branch.%s.remote")) < 0) + &remote_name, repo, refname, "branch.%s.remote")) < 0) goto cleanup; if ((error = retrieve_upstream_configuration( - &merge_name, repo, canonical_branch_name, "branch.%s.merge")) < 0) + &merge_name, repo, refname, "branch.%s.merge")) < 0) goto cleanup; if (!*remote_name || !*merge_name) { giterr_set(GITERR_REFERENCE, - "branch '%s' does not have an upstream", canonical_branch_name); + "branch '%s' does not have an upstream", refname); error = GIT_ENOTFOUND; goto cleanup; } @@ -327,13 +329,13 @@ int git_branch_upstream__name( goto cleanup; } - if (git_refspec_transform_r(&buf, refspec, merge_name) < 0) + if (git_refspec_transform(&buf, refspec, merge_name) < 0) goto cleanup; } else if (git_buf_sets(&buf, merge_name) < 0) goto cleanup; - error = git_buf_set(tracking_name, git_buf_cstr(&buf), git_buf_len(&buf)); + error = git_buf_set(out, git_buf_cstr(&buf), git_buf_len(&buf)); cleanup: git_remote_free(remote); @@ -341,7 +343,7 @@ cleanup: return error; } -static int remote_name(git_buf *buf, git_repository *repo, const char *canonical_branch_name) +int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refname) { git_strarray remote_list = {0}; size_t i; @@ -350,12 +352,14 @@ static int remote_name(git_buf *buf, git_repository *repo, const char *canonical int error = 0; char *remote_name = NULL; - assert(buf && repo && canonical_branch_name); + assert(buf && repo && refname); + + git_buf_sanitize(buf); /* Verify that this is a remote branch */ - if (!git_reference__is_remote(canonical_branch_name)) { + if (!git_reference__is_remote(refname)) { giterr_set(GITERR_INVALID, "Reference '%s' is not a remote branch.", - canonical_branch_name); + refname); error = GIT_ERROR; goto cleanup; } @@ -369,7 +373,7 @@ static int remote_name(git_buf *buf, git_repository *repo, const char *canonical if ((error = git_remote_load(&remote, repo, remote_list.strings[i])) < 0) continue; - fetchspec = git_remote__matching_dst_refspec(remote, canonical_branch_name); + fetchspec = git_remote__matching_dst_refspec(remote, refname); if (fetchspec) { /* If we have not already set out yet, then set * it to the matching remote name. Otherwise @@ -381,7 +385,7 @@ static int remote_name(git_buf *buf, git_repository *repo, const char *canonical git_remote_free(remote); giterr_set(GITERR_REFERENCE, - "Reference '%s' is ambiguous", canonical_branch_name); + "Reference '%s' is ambiguous", refname); error = GIT_EAMBIGUOUS; goto cleanup; } @@ -395,68 +399,18 @@ static int remote_name(git_buf *buf, git_repository *repo, const char *canonical error = git_buf_puts(buf, remote_name); } else { giterr_set(GITERR_REFERENCE, - "Could not determine remote for '%s'", canonical_branch_name); + "Could not determine remote for '%s'", refname); error = GIT_ENOTFOUND; } cleanup: + if (error < 0) + git_buf_free(buf); + git_strarray_free(&remote_list); return error; } -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; -} - -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; -} - int git_branch_upstream( git_reference **tracking_out, git_reference *branch) @@ -464,7 +418,7 @@ int git_branch_upstream( int error; git_buf tracking_name = GIT_BUF_INIT; - if ((error = git_branch_upstream__name(&tracking_name, + if ((error = git_branch_upstream_name(&tracking_name, git_reference_owner(branch), git_reference_name(branch))) < 0) return error; @@ -547,7 +501,7 @@ int git_branch_set_upstream(git_reference *branch, const char *upstream_name) if (local) git_buf_puts(&value, "."); else - remote_name(&value, repo, git_reference_name(upstream)); + git_branch_remote_name(&value, repo, git_reference_name(upstream)); if (git_buf_printf(&key, "branch.%s.remote", shortname) < 0) goto on_error; @@ -566,7 +520,7 @@ int git_branch_set_upstream(git_reference *branch, const char *upstream_name) fetchspec = git_remote__matching_dst_refspec(remote, git_reference_name(upstream)); git_buf_clear(&value); - if (!fetchspec || git_refspec_transform_l(&value, fetchspec, git_reference_name(upstream)) < 0) + if (!fetchspec || git_refspec_rtransform(&value, fetchspec, git_reference_name(upstream)) < 0) goto on_error; git_remote_free(remote); diff --git a/src/clone.c b/src/clone.c index 828c47ffb..2e9d72ab9 100644 --- a/src/clone.c +++ b/src/clone.c @@ -131,7 +131,7 @@ static int reference_matches_remote_head( if (!error && !git_oid__cmp(&head_info->remote_head_oid, &oid)) { /* Determine the local reference name from the remote tracking one */ - error = git_refspec_transform_l( + error = git_refspec_rtransform( &head_info->branchname, head_info->refspec, reference_name); if (!error && @@ -199,7 +199,7 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote) } /* Determine the remote tracking reference name from the local master */ - if ((error = git_refspec_transform_r( + if ((error = git_refspec_transform( &remote_master_name, head_info.refspec, GIT_REFS_HEADS_MASTER_FILE)) < 0) diff --git a/src/config.c b/src/config.c index fa1dd8182..6aa71468a 100644 --- a/src/config.c +++ b/src/config.c @@ -935,61 +935,21 @@ void git_config_iterator_free(git_config_iterator *iter) iter->free(iter); } -static int git_config__find_file_to_path( - char *out, size_t outlen, int (*find)(git_buf *buf)) -{ - int error = 0; - git_buf path = GIT_BUF_INIT; - - if ((error = find(&path)) < 0) - goto done; - - if (path.size >= outlen) { - giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path"); - error = GIT_EBUFS; - goto done; - } - - git_buf_copy_cstr(out, outlen, &path); - -done: - git_buf_free(&path); - return error; -} - -int git_config_find_global_r(git_buf *path) +int git_config_find_global(git_buf *path) { return git_futils_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL); } -int git_config_find_global(char *global_config_path, size_t length) -{ - return git_config__find_file_to_path( - global_config_path, length, git_config_find_global_r); -} - -int git_config_find_xdg_r(git_buf *path) +int git_config_find_xdg(git_buf *path) { return git_futils_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG); } -int git_config_find_xdg(char *xdg_config_path, size_t length) -{ - return git_config__find_file_to_path( - xdg_config_path, length, git_config_find_xdg_r); -} - -int git_config_find_system_r(git_buf *path) +int git_config_find_system(git_buf *path) { return git_futils_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM); } -int git_config_find_system(char *system_config_path, size_t length) -{ - return git_config__find_file_to_path( - system_config_path, length, git_config_find_system_r); -} - int git_config__global_location(git_buf *buf) { const git_buf *paths; @@ -1026,16 +986,16 @@ int git_config_open_default(git_config **out) if ((error = git_config_new(&cfg)) < 0) return error; - if (!git_config_find_global_r(&buf) || !git_config__global_location(&buf)) { + if (!git_config_find_global(&buf) || !git_config__global_location(&buf)) { error = git_config_add_file_ondisk(cfg, buf.ptr, GIT_CONFIG_LEVEL_GLOBAL, 0); } - if (!error && !git_config_find_xdg_r(&buf)) + if (!error && !git_config_find_xdg(&buf)) error = git_config_add_file_ondisk(cfg, buf.ptr, GIT_CONFIG_LEVEL_XDG, 0); - if (!error && !git_config_find_system_r(&buf)) + if (!error && !git_config_find_system(&buf)) error = git_config_add_file_ondisk(cfg, buf.ptr, GIT_CONFIG_LEVEL_SYSTEM, 0); diff --git a/src/config.h b/src/config.h index 3cd888c88..03d910616 100644 --- a/src/config.h +++ b/src/config.h @@ -24,11 +24,6 @@ struct git_config { git_vector files; }; -extern int git_config_find_global_r(git_buf *global_config_path); -extern int git_config_find_xdg_r(git_buf *system_config_path); -extern int git_config_find_system_r(git_buf *system_config_path); - - extern int git_config__global_location(git_buf *buf); extern int git_config_rename_section( diff --git a/src/message.c b/src/message.c index 0eff426f2..07b2569ad 100644 --- a/src/message.c +++ b/src/message.c @@ -21,7 +21,7 @@ static size_t line_length_without_trailing_spaces(const char *line, size_t len) /* Greatly inspired from git.git "stripspace" */ /* see https://github.com/git/git/blob/497215d8811ac7b8955693ceaad0899ecd894ed2/builtin/stripspace.c#L4-67 */ -int git_message__prettify(git_buf *message_out, const char *message, int strip_comments) +int git_message_prettify(git_buf *message_out, const char *message, int strip_comments) { const size_t message_len = strlen(message); @@ -29,6 +29,8 @@ int git_message__prettify(git_buf *message_out, const char *message, int strip_c size_t i, line_length, rtrimmed_line_length; char *next_newline; + git_buf_sanitize(message_out); + for (i = 0; i < strlen(message); i += line_length) { next_newline = memchr(message + i, '\n', message_len - i); @@ -58,29 +60,3 @@ int git_message__prettify(git_buf *message_out, const char *message, int strip_c return git_buf_oom(message_out) ? -1 : 0; } - -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; -} diff --git a/src/push.c b/src/push.c index 0be82f3b2..d39a27182 100644 --- a/src/push.c +++ b/src/push.c @@ -214,7 +214,7 @@ int git_push_update_tips(git_push *push) if (!fetch_spec) continue; - if ((error = git_refspec_transform_r(&remote_ref_name, fetch_spec, status->ref)) < 0) + if ((error = git_refspec_transform(&remote_ref_name, fetch_spec, status->ref)) < 0) goto on_error; /* Find matching push ref spec */ diff --git a/src/refspec.c b/src/refspec.c index a97340071..fa60aa7aa 100644 --- a/src/refspec.c +++ b/src/refspec.c @@ -178,54 +178,6 @@ int git_refspec_dst_matches(const git_refspec *refspec, const char *refname) return (p_fnmatch(refspec->dst, refname, 0) == 0); } -static int refspec_transform_internal(char *out, size_t outlen, const char *from, const char *to, const char *name) -{ - size_t baselen, namelen; - - baselen = strlen(to); - if (outlen <= baselen) { - giterr_set(GITERR_INVALID, "Reference name too long"); - return GIT_EBUFS; - } - - /* - * No '*' at the end means that it's mapped to one specific local - * branch, so no actual transformation is needed. - */ - if (to[baselen - 1] != '*') { - memcpy(out, to, baselen + 1); /* include '\0' */ - return 0; - } - - /* There's a '*' at the end, so remove its length */ - baselen--; - - /* skip the prefix, -1 is for the '*' */ - name += strlen(from) - 1; - - namelen = strlen(name); - - if (outlen <= baselen + namelen) { - giterr_set(GITERR_INVALID, "Reference name too long"); - return GIT_EBUFS; - } - - memcpy(out, to, baselen); - memcpy(out + baselen, name, namelen + 1); - - return 0; -} - -int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name) -{ - return refspec_transform_internal(out, outlen, spec->src, spec->dst, name); -} - -int git_refspec_rtransform(char *out, size_t outlen, const git_refspec *spec, const char *name) -{ - return refspec_transform_internal(out, outlen, spec->dst, spec->src, name); -} - static int refspec_transform( git_buf *out, const char *from, const char *to, const char *name) { @@ -233,6 +185,8 @@ static int refspec_transform( size_t from_len = from ? strlen(from) : 0; size_t name_len = name ? strlen(name) : 0; + git_buf_sanitize(out); + if (git_buf_set(out, to, to_len) < 0) return -1; @@ -253,12 +207,12 @@ static int refspec_transform( return git_buf_put(out, name + from_len, name_len - from_len); } -int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *name) +int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name) { return refspec_transform(out, spec->src, spec->dst, name); } -int git_refspec_transform_l(git_buf *out, const git_refspec *spec, const char *name) +int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name) { return refspec_transform(out, spec->dst, spec->src, name); } diff --git a/src/refspec.h b/src/refspec.h index 51b7bfee9..375465f61 100644 --- a/src/refspec.h +++ b/src/refspec.h @@ -31,28 +31,6 @@ int git_refspec__parse( void git_refspec__free(git_refspec *refspec); -/** - * Transform a reference to its target following the refspec's rules, - * and writes the results into a git_buf. - * - * @param out where to store the target name - * @param spec the refspec - * @param name the name of the reference to transform - * @return 0 or error if buffer allocation fails - */ -int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *name); - -/** - * Transform a reference from its target following the refspec's rules, - * and writes the results into a git_buf. - * - * @param out where to store the source name - * @param spec the refspec - * @param name the name of the reference to transform - * @return 0 or error if buffer allocation fails - */ -int git_refspec_transform_l(git_buf *out, const git_refspec *spec, const char *name); - int git_refspec__serialize(git_buf *out, const git_refspec *refspec); /** diff --git a/src/remote.c b/src/remote.c index 5b3656a81..5d35affd1 100644 --- a/src/remote.c +++ b/src/remote.c @@ -896,7 +896,7 @@ static int remote_head_for_ref(git_remote_head **out, git_refspec *spec, git_vec if ((error = git_reference_resolve(&resolved_ref, ref)) < 0 || (!git_reference_is_branch(resolved_ref)) || (error = git_branch_upstream(&tracking_ref, resolved_ref)) < 0 || - (error = git_refspec_transform_l(&remote_name, spec, git_reference_name(tracking_ref))) < 0) { + (error = git_refspec_rtransform(&remote_name, spec, git_reference_name(tracking_ref))) < 0) { /* Not an error if HEAD is unborn or no tracking branch */ if (error == GIT_ENOTFOUND) error = 0; @@ -1011,7 +1011,7 @@ static int update_tips_for_spec(git_remote *remote, git_refspec *spec, git_vecto continue; if (git_refspec_src_matches(spec, head->name) && spec->dst) { - if (git_refspec_transform_r(&refname, spec, head->name) < 0) + if (git_refspec_transform(&refname, spec, head->name) < 0) goto on_error; } else if (remote->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_NONE) { diff --git a/src/repository.c b/src/repository.c index 8645357b8..285d8897f 100644 --- a/src/repository.c +++ b/src/repository.c @@ -495,34 +495,18 @@ int git_repository_wrap_odb(git_repository **repo_out, git_odb *odb) } int git_repository_discover( - char *repository_path, - size_t size, + git_buf *out, 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); + assert(start_path); - *repository_path = '\0'; + git_buf_sanitize(out); - 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 small 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; + return find_repo(out, NULL, start_path, flags, ceiling_dirs); } static int load_config( @@ -598,9 +582,9 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo) git_buf system_buf = GIT_BUF_INIT; git_config *config; - git_config_find_global_r(&global_buf); - git_config_find_xdg_r(&xdg_buf); - git_config_find_system_r(&system_buf); + git_config_find_global(&global_buf); + git_config_find_xdg(&xdg_buf); + git_config_find_system(&system_buf); /* If there is no global file, open a backend for it anyway */ if (git_buf_len(&global_buf) == 0) @@ -1732,14 +1716,13 @@ cleanup: return error; } -int git_repository_message(char *buffer, size_t len, git_repository *repo) +int git_repository_message(git_buf *out, git_repository *repo) { - git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT; + git_buf path = GIT_BUF_INIT; struct stat st; int error; - if (buffer != NULL) - *buffer = '\0'; + git_buf_sanitize(out); if (git_buf_joinpath(&path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0) return -1; @@ -1749,16 +1732,10 @@ int git_repository_message(char *buffer, size_t len, git_repository *repo) error = GIT_ENOTFOUND; giterr_set(GITERR_OS, "Could not access message file"); } - else if (buffer != NULL) { - error = git_futils_readbuffer(&buf, git_buf_cstr(&path)); - git_buf_copy_cstr(buffer, len, &buf); - } + + error = git_futils_readbuffer(out, git_buf_cstr(&path)); git_buf_free(&path); - git_buf_free(&buf); - - if (!error) - error = (int)st.st_size + 1; /* add 1 for NUL byte */ return error; } diff --git a/tests/clone/empty.c b/tests/clone/empty.c index 6d19244cc..78aef7dae 100644 --- a/tests/clone/empty.c +++ b/tests/clone/empty.c @@ -38,7 +38,7 @@ void test_clone_empty__can_clone_an_empty_local_repo_barely(void) char *local_name = "refs/heads/master"; const char *expected_tracked_branch_name = "refs/remotes/origin/master"; const char *expected_remote_name = "origin"; - char buffer[1024]; + git_buf buf = GIT_BUF_INIT; git_reference *ref; cl_set_cleanup(&cleanup_repository, "./empty"); @@ -50,16 +50,14 @@ void test_clone_empty__can_clone_an_empty_local_repo_barely(void) cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned, local_name)); /* ...one can still retrieve the name of the remote tracking reference */ - cl_assert_equal_i((int)strlen(expected_tracked_branch_name) + 1, - git_branch_upstream_name(buffer, 1024, g_repo_cloned, local_name)); + cl_git_pass(git_branch_upstream_name(&buf, g_repo_cloned, local_name)); - cl_assert_equal_s(expected_tracked_branch_name, buffer); + cl_assert_equal_s(expected_tracked_branch_name, buf.ptr); /* ...and the name of the remote... */ - cl_assert_equal_i((int)strlen(expected_remote_name) + 1, - git_branch_remote_name(buffer, 1024, g_repo_cloned, expected_tracked_branch_name)); + cl_git_pass(git_branch_remote_name(&buf, g_repo_cloned, expected_tracked_branch_name)); - cl_assert_equal_s(expected_remote_name, buffer); + cl_assert_equal_s(expected_remote_name, buf.ptr); /* ...even when the remote HEAD is unborn as well */ cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned, diff --git a/tests/network/remote/remotes.c b/tests/network/remote/remotes.c index 44a98d31a..09963e8ef 100644 --- a/tests/network/remote/remotes.c +++ b/tests/network/remote/remotes.c @@ -230,27 +230,20 @@ void test_network_remote_remotes__fnmatch(void) void test_network_remote_remotes__transform(void) { - char ref[1024] = {0}; + git_buf ref = GIT_BUF_INIT; - cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master")); + cl_git_pass(git_refspec_transform(&ref, _refspec, "refs/heads/master")); cl_assert_equal_s(ref, "refs/remotes/test/master"); + git_buf_free(&ref); } void test_network_remote_remotes__transform_destination_to_source(void) { - char ref[1024] = {0}; + git_buf ref = GIT_BUF_INIT; - cl_git_pass(git_refspec_rtransform(ref, sizeof(ref), _refspec, "refs/remotes/test/master")); - cl_assert_equal_s(ref, "refs/heads/master"); -} - -void test_network_remote_remotes__transform_r(void) -{ - git_buf buf = GIT_BUF_INIT; - - cl_git_pass(git_refspec_transform_r(&buf, _refspec, "refs/heads/master")); - cl_assert_equal_s(git_buf_cstr(&buf), "refs/remotes/test/master"); - git_buf_free(&buf); + cl_git_pass(git_refspec_rtransform(&ref, _refspec, "refs/remotes/test/master")); + cl_assert_equal_s(ref.ptr, "refs/heads/master"); + git_buf_free(&ref); } void test_network_remote_remotes__missing_refspecs(void) diff --git a/tests/object/commit/commitstagedfile.c b/tests/object/commit/commitstagedfile.c index 9867ab418..150927728 100644 --- a/tests/object/commit/commitstagedfile.c +++ b/tests/object/commit/commitstagedfile.c @@ -25,7 +25,7 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid; git_signature *signature; git_tree *tree; - char buffer[128]; + git_buf buffer; /* * The test below replicates the following git scenario @@ -111,7 +111,8 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); - cl_assert_equal_i(16, git_message_prettify(buffer, 128, "Initial commit", 0)); + memset(&buffer, 0, sizeof(git_buf)); + cl_git_pass(git_message_prettify(&buffer, "Initial commit", 0)); cl_git_pass(git_commit_create_v( &commit_oid, @@ -120,12 +121,13 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) signature, signature, NULL, - buffer, + buffer.ptr, tree, 0)); cl_assert(git_oid_cmp(&expected_commit_oid, &commit_oid) == 0); + git_buf_free(&buffer); git_signature_free(signature); git_tree_free(tree); git_index_free(index); diff --git a/tests/object/message.c b/tests/object/message.c index 7ef6374b3..ab5565341 100644 --- a/tests/object/message.c +++ b/tests/object/message.c @@ -6,7 +6,7 @@ static void assert_message_prettifying(char *expected_output, char *input, int s { git_buf prettified_message = GIT_BUF_INIT; - git_message__prettify(&prettified_message, input, strip_comments); + git_message_prettify(&prettified_message, input, strip_comments); cl_assert_equal_s(expected_output, git_buf_cstr(&prettified_message)); git_buf_free(&prettified_message); @@ -172,65 +172,28 @@ void test_object_message__keep_comments(void) void test_object_message__message_prettify(void) { - char buffer[100]; + git_buf buffer; - cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 0) == 1); - cl_assert_equal_s(buffer, ""); - cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 1) == 1); - cl_assert_equal_s(buffer, ""); + memset(&buffer, 0, sizeof(buffer)); + cl_git_pass(git_message_prettify(&buffer, "", 0)); + cl_assert_equal_s(buffer.ptr, ""); + git_buf_free(&buffer); + cl_git_pass(git_message_prettify(&buffer, "", 1)); + cl_assert_equal_s(buffer.ptr, ""); + git_buf_free(&buffer); - cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 0)); - cl_assert_equal_s("Short\n", buffer); - cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 1)); - cl_assert_equal_s("Short\n", buffer); + cl_git_pass(git_message_prettify(&buffer, "Short", 0)); + cl_assert_equal_s("Short\n", buffer.ptr); + git_buf_free(&buffer); + cl_git_pass(git_message_prettify(&buffer, "Short", 1)); + cl_assert_equal_s("Short\n", buffer.ptr); + git_buf_free(&buffer); - cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 0) > 0); - cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n# with some comments still in\n"); + cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 0)); + cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n# with some comments still in\n"); + git_buf_free(&buffer); - cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 1) > 0); - cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n"); - - /* try out overflow */ - cl_assert(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "12345678", - 0) > 0); - cl_assert_equal_s(buffer, - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "12345678\n"); - - cl_assert(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "12345678\n", - 0) > 0); - cl_assert_equal_s(buffer, - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "12345678\n"); - - cl_git_fail(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "123456789", - 0)); - cl_git_fail(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "123456789\n", - 0)); - cl_git_fail(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890", - 0)); - cl_git_fail(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890" - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890""x", - 0)); - - cl_assert(git_message_prettify(buffer, sizeof(buffer), - "1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n" - "# 1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n" - "1234567890", - 1) > 0); - - cl_assert(git_message_prettify(NULL, 0, "", 0) == 1); - cl_assert(git_message_prettify(NULL, 0, "Short test", 0) == 12); - cl_assert(git_message_prettify(NULL, 0, "Test\n# with\nComments", 1) == 15); + cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 1)); + cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n"); + git_buf_free(&buffer); } diff --git a/tests/online/push.c b/tests/online/push.c index 33f174654..8efe21e0e 100644 --- a/tests/online/push.c +++ b/tests/online/push.c @@ -219,7 +219,7 @@ static void verify_tracking_branches(git_remote *remote, expected_ref expected_r if (!fetch_spec) continue; - cl_git_pass(git_refspec_transform_r(&ref_name, fetch_spec, expected_refs[i].name)); + cl_git_pass(git_refspec_transform(&ref_name, fetch_spec, expected_refs[i].name)); /* Find matching remote branch */ git_vector_foreach(&actual_refs, j, actual_ref) { diff --git a/tests/refs/branches/remote.c b/tests/refs/branches/remote.c index c110adb33..bac088454 100644 --- a/tests/refs/branches/remote.c +++ b/tests/refs/branches/remote.c @@ -21,53 +21,40 @@ void test_refs_branches_remote__cleanup(void) void test_refs_branches_remote__can_get_remote_for_branch(void) { - char remotename[1024] = {0}; + git_buf remotename = {0}; - cl_assert_equal_i(expected_remote_name_length, - git_branch_remote_name(NULL, 0, g_repo, remote_tracking_branch_name)); + cl_git_pass(git_branch_remote_name(&remotename, g_repo, remote_tracking_branch_name)); - cl_assert_equal_i(expected_remote_name_length, - git_branch_remote_name(remotename, expected_remote_name_length, g_repo, - remote_tracking_branch_name)); - - cl_assert_equal_s("test", remotename); -} - -void test_refs_branches_remote__insufficient_buffer_returns_error(void) -{ - char remotename[1024] = {0}; - - cl_assert_equal_i(expected_remote_name_length, - git_branch_remote_name(NULL, 0, g_repo, remote_tracking_branch_name)); - - cl_git_fail_with(git_branch_remote_name(remotename, - expected_remote_name_length - 1, g_repo, remote_tracking_branch_name), - expected_remote_name_length); + cl_assert_equal_s("test", remotename.ptr); + git_buf_free(&remotename); } void test_refs_branches_remote__no_matching_remote_returns_error(void) { const char *unknown = "refs/remotes/nonexistent/master"; + git_buf buf; giterr_clear(); - cl_git_fail_with(git_branch_remote_name( - NULL, 0, g_repo, unknown), GIT_ENOTFOUND); + memset(&buf, 0, sizeof(git_buf)); + cl_git_fail_with(git_branch_remote_name(&buf, g_repo, unknown), GIT_ENOTFOUND); cl_assert(giterr_last() != NULL); } void test_refs_branches_remote__local_remote_returns_error(void) { const char *local = "refs/heads/master"; + git_buf buf; giterr_clear(); - cl_git_fail_with(git_branch_remote_name( - NULL, 0, g_repo, local), GIT_ERROR); + memset(&buf, 0, sizeof(git_buf)); + cl_git_fail_with(git_branch_remote_name(&buf, g_repo, local), GIT_ERROR); cl_assert(giterr_last() != NULL); } void test_refs_branches_remote__ambiguous_remote_returns_error(void) { git_remote *remote; + git_buf buf; /* Create the remote */ cl_git_pass(git_remote_create(&remote, g_repo, "addtest", "http://github.com/libgit2/libgit2")); @@ -80,7 +67,7 @@ void test_refs_branches_remote__ambiguous_remote_returns_error(void) git_remote_free(remote); giterr_clear(); - cl_git_fail_with(git_branch_remote_name(NULL, 0, g_repo, - remote_tracking_branch_name), GIT_EAMBIGUOUS); + memset(&buf, 0, sizeof(git_buf)); + cl_git_fail_with(git_branch_remote_name(&buf, g_repo, remote_tracking_branch_name), GIT_EAMBIGUOUS); cl_assert(giterr_last() != NULL); } diff --git a/tests/refs/branches/upstreamname.c b/tests/refs/branches/upstreamname.c index f05607d44..d30002e08 100644 --- a/tests/refs/branches/upstreamname.c +++ b/tests/refs/branches/upstreamname.c @@ -21,7 +21,7 @@ void test_refs_branches_upstreamname__cleanup(void) void test_refs_branches_upstreamname__can_retrieve_the_remote_tracking_reference_name_of_a_local_branch(void) { - cl_git_pass(git_branch_upstream__name( + cl_git_pass(git_branch_upstream_name( &upstream_name, repo, "refs/heads/master")); cl_assert_equal_s("refs/remotes/test/master", git_buf_cstr(&upstream_name)); @@ -29,14 +29,8 @@ void test_refs_branches_upstreamname__can_retrieve_the_remote_tracking_reference void test_refs_branches_upstreamname__can_retrieve_the_local_upstream_reference_name_of_a_local_branch(void) { - cl_git_pass(git_branch_upstream__name( + cl_git_pass(git_branch_upstream_name( &upstream_name, repo, "refs/heads/track-local")); cl_assert_equal_s("refs/heads/master", git_buf_cstr(&upstream_name)); } - -void test_refs_branches_upstreamname__can_return_the_size_of_thelocal_upstream_reference_name_of_a_local_branch(void) -{ - cl_assert_equal_i((int)strlen("refs/heads/master") + 1, - git_branch_upstream_name(NULL, 0, repo, "refs/heads/track-local")); -} diff --git a/tests/repo/discover.c b/tests/repo/discover.c index f93ff2462..7904b6496 100644 --- a/tests/repo/discover.c +++ b/tests/repo/discover.c @@ -25,12 +25,13 @@ static void ensure_repository_discover(const char *start_path, const char *ceiling_dirs, - const char *expected_path) + git_buf *expected_path) { - char found_path[GIT_PATH_MAX]; - cl_git_pass(git_repository_discover(found_path, sizeof(found_path), start_path, 0, ceiling_dirs)); + git_buf found_path = GIT_BUF_INIT; + cl_git_pass(git_repository_discover(&found_path, start_path, 0, ceiling_dirs)); //across_fs is always 0 as we can't automate the filesystem change tests - cl_assert_equal_s(found_path, expected_path); + cl_assert_equal_s(found_path.ptr, expected_path->ptr); + git_buf_free(&found_path); } static void write_file(const char *path, const char *content) @@ -69,42 +70,40 @@ static void append_ceiling_dir(git_buf *ceiling_dirs, const char *path) void test_repo_discover__0(void) { - // test discover + // test discover git_repository *repo; - git_buf ceiling_dirs_buf = GIT_BUF_INIT; + git_buf ceiling_dirs_buf = GIT_BUF_INIT, repository_path = GIT_BUF_INIT, + sub_repository_path = GIT_BUF_INIT, found_path = GIT_BUF_INIT; const char *ceiling_dirs; - char repository_path[GIT_PATH_MAX]; - char sub_repository_path[GIT_PATH_MAX]; - char found_path[GIT_PATH_MAX]; const mode_t mode = 0777; git_futils_mkdir_r(DISCOVER_FOLDER, NULL, mode); append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER); ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&repository_path, DISCOVER_FOLDER, 0, ceiling_dirs)); cl_git_pass(git_repository_init(&repo, DISCOVER_FOLDER, 1)); - cl_git_pass(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs)); + cl_git_pass(git_repository_discover(&repository_path, DISCOVER_FOLDER, 0, ceiling_dirs)); git_repository_free(repo); cl_git_pass(git_repository_init(&repo, SUB_REPOSITORY_FOLDER, 0)); cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode)); - cl_git_pass(git_repository_discover(sub_repository_path, sizeof(sub_repository_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); + cl_git_pass(git_repository_discover(&sub_repository_path, SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode)); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, &sub_repository_path); cl_git_pass(git_futils_mkdir_r(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, NULL, mode)); write_file(REPOSITORY_ALTERNATE_FOLDER "/" DOT_GIT, "gitdir: ../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT); write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/" DOT_GIT, "gitdir: ../../../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT); write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB "/" DOT_GIT, "gitdir: ../../../../"); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, &repository_path); cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER1, NULL, mode)); write_file(ALTERNATE_MALFORMED_FOLDER1 "/" DOT_GIT, "Anything but not gitdir:"); @@ -114,29 +113,31 @@ void test_repo_discover__0(void) write_file(ALTERNATE_MALFORMED_FOLDER3 "/" DOT_GIT, "gitdir: \n\n\n"); cl_git_pass(git_futils_mkdir_r(ALTERNATE_NOT_FOUND_FOLDER, NULL, mode)); write_file(ALTERNATE_NOT_FOUND_FOLDER "/" DOT_GIT, "gitdir: a_repository_that_surely_does_not_exist"); - cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs)); - cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs)); - cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs)); + cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs)); + cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs)); + cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs)); append_ceiling_dir(&ceiling_dirs_buf, SUB_REPOSITORY_FOLDER); ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf); //this must pass as ceiling_directories cannot predent the current //working directory to be checked - cl_git_pass(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs)); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs)); + cl_git_pass(git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER, 0, ceiling_dirs)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs)); //.gitfile redirection should not be affected by ceiling directories - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path); - ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path); + ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, &repository_path); cl_git_pass(git_futils_rmdir_r(TEMP_REPO_FOLDER, NULL, GIT_RMDIR_REMOVE_FILES)); git_repository_free(repo); git_buf_free(&ceiling_dirs_buf); + git_buf_free(&repository_path); + git_buf_free(&sub_repository_path); } diff --git a/tests/repo/message.c b/tests/repo/message.c index 629d40c12..57e8e5f4d 100644 --- a/tests/repo/message.c +++ b/tests/repo/message.c @@ -5,48 +5,37 @@ static git_repository *_repo; static git_buf _path; -static char *_actual; +static git_buf _actual; void test_repo_message__initialize(void) { _repo = cl_git_sandbox_init("testrepo.git"); + git_buf_init(&_actual, 0); } void test_repo_message__cleanup(void) { cl_git_sandbox_cleanup(); git_buf_free(&_path); - git__free(_actual); - _actual = NULL; + git_buf_free(&_actual); } void test_repo_message__none(void) { - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(NULL, 0, _repo)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&_actual, _repo)); } void test_repo_message__message(void) { const char expected[] = "Test\n\nThis is a test of the emergency broadcast system\n"; - ssize_t len; cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), "MERGE_MSG")); cl_git_mkfile(git_buf_cstr(&_path), expected); - len = git_repository_message(NULL, 0, _repo); - cl_assert(len > 0); - - _actual = git__malloc(len + 1); - cl_assert(_actual != NULL); - - /* Test non truncation */ - cl_assert(git_repository_message(_actual, len, _repo) > 0); + cl_git_pass(git_repository_message(&_actual, _repo)); cl_assert_equal_s(expected, _actual); - - /* Test truncation and that trailing NUL is inserted */ - cl_assert(git_repository_message(_actual, 6, _repo) > 0); - cl_assert_equal_s("Test\n", _actual); + git_buf_free(&_actual); cl_git_pass(p_unlink(git_buf_cstr(&_path))); - cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(NULL, 0, _repo)); + cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&_actual, _repo)); }