teach convert_to_git a "dry run" mode
Some callers may want to know whether convert_to_git will actually do anything before performing the conversion itself (e.g., to decide whether to stream or handle blobs in-core). This patch lets callers specify the dry run mode by passing a NULL destination buffer. The return value, instead of indicating whether conversion happened, will indicate whether conversion would occur. For readability, we also include a wrapper function which makes it more obvious we are not actually performing the conversion. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
d0482e88a7
commit
92ac3197e4
17
convert.c
17
convert.c
@ -231,6 +231,13 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
|
|||||||
if (!stats.cr)
|
if (!stats.cr)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* At this point all of our source analysis is done, and we are sure we
|
||||||
|
* would convert. If we are in dry-run mode, we can give an answer.
|
||||||
|
*/
|
||||||
|
if (!buf)
|
||||||
|
return 1;
|
||||||
|
|
||||||
/* only grow if not in place */
|
/* only grow if not in place */
|
||||||
if (strbuf_avail(buf) + buf->len < len)
|
if (strbuf_avail(buf) + buf->len < len)
|
||||||
strbuf_grow(buf, len - buf->len);
|
strbuf_grow(buf, len - buf->len);
|
||||||
@ -391,6 +398,9 @@ static int apply_filter(const char *path, const char *src, size_t len,
|
|||||||
if (!cmd)
|
if (!cmd)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
if (!dst)
|
||||||
|
return 1;
|
||||||
|
|
||||||
memset(&async, 0, sizeof(async));
|
memset(&async, 0, sizeof(async));
|
||||||
async.proc = filter_buffer;
|
async.proc = filter_buffer;
|
||||||
async.data = ¶ms;
|
async.data = ¶ms;
|
||||||
@ -525,6 +535,9 @@ static int ident_to_git(const char *path, const char *src, size_t len,
|
|||||||
if (!ident || !count_ident(src, len))
|
if (!ident || !count_ident(src, len))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
if (!buf)
|
||||||
|
return 1;
|
||||||
|
|
||||||
/* only grow if not in place */
|
/* only grow if not in place */
|
||||||
if (strbuf_avail(buf) + buf->len < len)
|
if (strbuf_avail(buf) + buf->len < len)
|
||||||
strbuf_grow(buf, len - buf->len);
|
strbuf_grow(buf, len - buf->len);
|
||||||
@ -754,13 +767,13 @@ int convert_to_git(const char *path, const char *src, size_t len,
|
|||||||
filter = ca.drv->clean;
|
filter = ca.drv->clean;
|
||||||
|
|
||||||
ret |= apply_filter(path, src, len, dst, filter);
|
ret |= apply_filter(path, src, len, dst, filter);
|
||||||
if (ret) {
|
if (ret && dst) {
|
||||||
src = dst->buf;
|
src = dst->buf;
|
||||||
len = dst->len;
|
len = dst->len;
|
||||||
}
|
}
|
||||||
ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
|
ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
|
||||||
ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
|
ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
|
||||||
if (ret) {
|
if (ret && dst) {
|
||||||
src = dst->buf;
|
src = dst->buf;
|
||||||
len = dst->len;
|
len = dst->len;
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,11 @@ extern int convert_to_working_tree(const char *path, const char *src,
|
|||||||
size_t len, struct strbuf *dst);
|
size_t len, struct strbuf *dst);
|
||||||
extern int renormalize_buffer(const char *path, const char *src, size_t len,
|
extern int renormalize_buffer(const char *path, const char *src, size_t len,
|
||||||
struct strbuf *dst);
|
struct strbuf *dst);
|
||||||
|
static inline int would_convert_to_git(const char *path, const char *src,
|
||||||
|
size_t len, enum safe_crlf checksafe)
|
||||||
|
{
|
||||||
|
return convert_to_git(path, src, len, NULL, checksafe);
|
||||||
|
}
|
||||||
|
|
||||||
/*****************************************************************
|
/*****************************************************************
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user