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>
Jeff King committed
Feb 24, 2012 at 17:02 UTC
92ac3197e4859ba8c19e3e7f7b8cf5dc38e4669d
2 files changed
+20
-2
convert.c
+15
-2
@@ -231,6 +231,13 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
231
if (!stats.cr)
232
return 0;
233
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
+
241
/* only grow if not in place */
242
if (strbuf_avail(buf) + buf->len < len)
243
strbuf_grow(buf, len - buf->len);
@@ -391,6 +398,9 @@ static int apply_filter(const char *path, const char *src, size_t len,
398
if (!cmd)
399
return 0;
400
401
+ if (!dst)
402
+ return 1;
403
+
404
memset(&async, 0, sizeof(async));
405
async.proc = filter_buffer;
406
async.data = ¶ms;
@@ -525,6 +535,9 @@ static int ident_to_git(const char *path, const char *src, size_t len,
535
if (!ident || !count_ident(src, len))
536
return 0;
537
538
+ if (!buf)
539
+ return 1;
540
+
541
/* only grow if not in place */
542
if (strbuf_avail(buf) + buf->len < len)
543
strbuf_grow(buf, len - buf->len);
@@ -754,13 +767,13 @@ int convert_to_git(const char *path, const char *src, size_t len,
767
filter = ca.drv->clean;
768
769
ret |= apply_filter(path, src, len, dst, filter);
757
- if (ret) {
770
+ if (ret && dst) {
771
src = dst->buf;
772
len = dst->len;
773
}
774
ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
775
ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
763
- if (ret) {
776
+ if (ret && dst) {
777
src = dst->buf;
778
len = dst->len;
779
}
convert.h
+5
@@ -40,6 +40,11 @@ extern int convert_to_working_tree(const char *path, const char *src,
40
size_t len, struct strbuf *dst);
41
extern int renormalize_buffer(const char *path, const char *src, size_t len,
42
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
+}
48
49
/*****************************************************************
50
*