summaryrefslogtreecommitdiff
path: root/src/crlf.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-09-07 17:53:49 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2021-10-17 09:49:01 -0400
commitf0e693b18afbe1de37d7da5b5a8967b6c87d8e53 (patch)
treebe5e1cdbfa218ba81ec06bf45e45cfeb7f79a2a5 /src/crlf.c
parent5346be3ddd3bcf19779c5d62e71f8442a0171133 (diff)
downloadlibgit2-ethomson/gitstr.tar.gz
str: introduce `git_str` for internal, `git_buf` is externalethomson/gitstr
libgit2 has two distinct requirements that were previously solved by `git_buf`. We require: 1. A general purpose string class that provides a number of utility APIs for manipulating data (eg, concatenating, truncating, etc). 2. A structure that we can use to return strings to callers that they can take ownership of. By using a single class (`git_buf`) for both of these purposes, we have confused the API to the point that refactorings are difficult and reasoning about correctness is also difficult. Move the utility class `git_buf` to be called `git_str`: this represents its general purpose, as an internal string buffer class. The name also is an homage to Junio Hamano ("gitstr"). The public API remains `git_buf`, and has a much smaller footprint. It is generally only used as an "out" param with strict requirements that follow the documentation. (Exceptions exist for some legacy APIs to avoid breaking callers unnecessarily.) Utility functions exist to convert a user-specified `git_buf` to a `git_str` so that we can call internal functions, then converting it back again.
Diffstat (limited to 'src/crlf.c')
-rw-r--r--src/crlf.c43
1 files changed, 23 insertions, 20 deletions
diff --git a/src/crlf.c b/src/crlf.c
index 406f7140f..7895ddec2 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -12,6 +12,7 @@
#include "git2/index.h"
#include "git2/sys/filter.h"
+#include "buf.h"
#include "futils.h"
#include "hash.h"
#include "filter.h"
@@ -152,7 +153,7 @@ static git_configmap_value output_eol(struct crlf_attrs *ca)
GIT_INLINE(int) check_safecrlf(
struct crlf_attrs *ca,
const git_filter_source *src,
- git_buf_text_stats *stats)
+ git_str_text_stats *stats)
{
const char *filename = git_filter_source_path(src);
@@ -206,19 +207,19 @@ GIT_INLINE(int) check_safecrlf(
static int crlf_apply_to_odb(
struct crlf_attrs *ca,
- git_buf *to,
- const git_buf *from,
+ git_str *to,
+ const git_str *from,
const git_filter_source *src)
{
- git_buf_text_stats stats;
+ git_str_text_stats stats;
bool is_binary;
int error;
/* Binary attribute? Empty file? Nothing to do */
- if (ca->crlf_action == GIT_CRLF_BINARY || !git_buf_len(from))
+ if (ca->crlf_action == GIT_CRLF_BINARY || from->size == 0)
return GIT_PASSTHROUGH;
- is_binary = git_buf_gather_text_stats(&stats, from, false);
+ is_binary = git_str_gather_text_stats(&stats, from, false);
/* Heuristics to see if we can skip the conversion.
* Straight from Core Git.
@@ -246,22 +247,22 @@ static int crlf_apply_to_odb(
return GIT_PASSTHROUGH;
/* Actually drop the carriage returns */
- return git_buf_crlf_to_lf(to, from);
+ return git_str_crlf_to_lf(to, from);
}
static int crlf_apply_to_workdir(
struct crlf_attrs *ca,
- git_buf *to,
- const git_buf *from)
+ git_str *to,
+ const git_str *from)
{
- git_buf_text_stats stats;
+ git_str_text_stats stats;
bool is_binary;
/* Empty file? Nothing to do. */
- if (git_buf_len(from) == 0 || output_eol(ca) != GIT_EOL_CRLF)
+ if (git_str_len(from) == 0 || output_eol(ca) != GIT_EOL_CRLF)
return GIT_PASSTHROUGH;
- is_binary = git_buf_gather_text_stats(&stats, from, false);
+ is_binary = git_str_gather_text_stats(&stats, from, false);
/* If there are no LFs, or all LFs are part of a CRLF, nothing to do */
if (stats.lf == 0 || stats.lf == stats.crlf)
@@ -280,7 +281,7 @@ static int crlf_apply_to_workdir(
return GIT_PASSTHROUGH;
}
- return git_buf_lf_to_crlf(to, from);
+ return git_str_lf_to_crlf(to, from);
}
static int convert_attrs(
@@ -368,22 +369,24 @@ static int crlf_check(
static int crlf_apply(
git_filter *self,
void **payload, /* may be read and/or set */
- git_buf *to,
- const git_buf *from,
+ git_str *to,
+ const git_str *from,
const git_filter_source *src)
{
+ int error = 0;
+
/* initialize payload in case `check` was bypassed */
if (!*payload) {
- int error = crlf_check(self, payload, src, NULL);
-
- if (error < 0)
+ if ((error = crlf_check(self, payload, src, NULL)) < 0)
return error;
}
if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
- return crlf_apply_to_workdir(*payload, to, from);
+ error = crlf_apply_to_workdir(*payload, to, from);
else
- return crlf_apply_to_odb(*payload, to, from, src);
+ error = crlf_apply_to_odb(*payload, to, from, src);
+
+ return error;
}
static int crlf_stream(