Skip to content

Commit 92ac319

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0482e8 commit 92ac319

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

convert.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
231231
if (!stats.cr)
232232
return 0;
233233

234+
/*
235+
* At this point all of our source analysis is done, and we are sure we
236+
* would convert. If we are in dry-run mode, we can give an answer.
237+
*/
238+
if (!buf)
239+
return 1;
240+
234241
/* only grow if not in place */
235242
if (strbuf_avail(buf) + buf->len < len)
236243
strbuf_grow(buf, len - buf->len);
@@ -391,6 +398,9 @@ static int apply_filter(const char *path, const char *src, size_t len,
391398
if (!cmd)
392399
return 0;
393400

401+
if (!dst)
402+
return 1;
403+
394404
memset(&async, 0, sizeof(async));
395405
async.proc = filter_buffer;
396406
async.data = &params;
@@ -525,6 +535,9 @@ static int ident_to_git(const char *path, const char *src, size_t len,
525535
if (!ident || !count_ident(src, len))
526536
return 0;
527537

538+
if (!buf)
539+
return 1;
540+
528541
/* only grow if not in place */
529542
if (strbuf_avail(buf) + buf->len < len)
530543
strbuf_grow(buf, len - buf->len);
@@ -754,13 +767,13 @@ int convert_to_git(const char *path, const char *src, size_t len,
754767
filter = ca.drv->clean;
755768

756769
ret |= apply_filter(path, src, len, dst, filter);
757-
if (ret) {
770+
if (ret && dst) {
758771
src = dst->buf;
759772
len = dst->len;
760773
}
761774
ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
762775
ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
763-
if (ret) {
776+
if (ret && dst) {
764777
src = dst->buf;
765778
len = dst->len;
766779
}

convert.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ extern int convert_to_working_tree(const char *path, const char *src,
4040
size_t len, struct strbuf *dst);
4141
extern int renormalize_buffer(const char *path, const char *src, size_t len,
4242
struct strbuf *dst);
43+
static inline int would_convert_to_git(const char *path, const char *src,
44+
size_t len, enum safe_crlf checksafe)
45+
{
46+
return convert_to_git(path, src, len, NULL, checksafe);
47+
}
4348

4449
/*****************************************************************
4550
*

0 commit comments

Comments
 (0)