diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/branch.c | 102 | ||||
-rw-r--r-- | src/clone.c | 4 | ||||
-rw-r--r-- | src/config.c | 52 | ||||
-rw-r--r-- | src/config.h | 5 | ||||
-rw-r--r-- | src/message.c | 30 | ||||
-rw-r--r-- | src/push.c | 2 | ||||
-rw-r--r-- | src/refspec.c | 54 | ||||
-rw-r--r-- | src/refspec.h | 22 | ||||
-rw-r--r-- | src/remote.c | 4 | ||||
-rw-r--r-- | src/repository.c | 47 |
10 files changed, 58 insertions, 264 deletions
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); + + git_buf_sanitize(out); - if (!git_reference__is_branch(canonical_branch_name)) - return not_a_local_branch(canonical_branch_name); + 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); - - *repository_path = '\0'; - if ((error = find_repo(&path, NULL, start_path, flags, ceiling_dirs)) < 0) - return error != GIT_ENOTFOUND ? -1 : error; + assert(start_path); - 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; - } + git_buf_sanitize(out); - /* 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); - } - git_buf_free(&path); - git_buf_free(&buf); + error = git_futils_readbuffer(out, git_buf_cstr(&path)); - if (!error) - error = (int)st.st_size + 1; /* add 1 for NUL byte */ + git_buf_free(&path); return error; } |