diff mbox series

[05/16] index-info.c: identify empty input lines in read_index_info

Message ID 7e3bcc16e23c97d8a4efbb9e14b230ef9f44a1a7.1718130288.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series mktree: support more flexible usage | expand

Commit Message

Victoria Dye June 11, 2024, 6:24 p.m. UTC
From: Victoria Dye <vdye@github.com>

Update 'read_index_info()' to return INDEX_INFO_EMPTY_LINE (value 1), rather
than the default error code (value -1) when the function encounters an empty
line in stdin. This grants the caller the flexibility to handle such
scenarios differently than a typical error. In the case of 'update-index',
we'll still exit with a "malformed input line" error. However, when
'read_index_info()' is used to process the input to 'mktree' in a later
patch, the empty line return value will signal a new tree in --batch mode.

Signed-off-by: Victoria Dye <vdye@github.com>
---
 builtin/update-index.c | 4 +++-
 index-info.c           | 5 +++++
 index-info.h           | 2 ++
 3 files changed, 10 insertions(+), 1 deletion(-)

Comments

Junio C Hamano June 11, 2024, 10:52 p.m. UTC | #1
"Victoria Dye via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Victoria Dye <vdye@github.com>
>
> Update 'read_index_info()' to return INDEX_INFO_EMPTY_LINE (value 1), rather
> than the default error code (value -1) when the function encounters an empty
> line in stdin. This grants the caller the flexibility to handle such
> scenarios differently than a typical error. In the case of 'update-index',
> we'll still exit with a "malformed input line" error. However, when
> 'read_index_info()' is used to process the input to 'mktree' in a later
> patch, the empty line return value will signal a new tree in --batch mode.

Interesting.  We could even introduce "# commented input" but that
is a different story ;-).

I also wonder if we can flip it around and teach read_index_info()
to (1) silently accept and do a callback when it recognises the
input line is one of the supported formats, and (2) send any
unrecognised line, not just an empty one, with "unrecognised" status
code.  That way, the caller can handle more than single kind of
"special input line" more easily, perhaps?

Thanks.
Victoria Dye June 18, 2024, 5:33 p.m. UTC | #2
Junio C Hamano wrote:
> "Victoria Dye via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
>> From: Victoria Dye <vdye@github.com>
>>
>> Update 'read_index_info()' to return INDEX_INFO_EMPTY_LINE (value 1), rather
>> than the default error code (value -1) when the function encounters an empty
>> line in stdin. This grants the caller the flexibility to handle such
>> scenarios differently than a typical error. In the case of 'update-index',
>> we'll still exit with a "malformed input line" error. However, when
>> 'read_index_info()' is used to process the input to 'mktree' in a later
>> patch, the empty line return value will signal a new tree in --batch mode.
> 
> Interesting.  We could even introduce "# commented input" but that
> is a different story ;-).
> 
> I also wonder if we can flip it around and teach read_index_info()
> to (1) silently accept and do a callback when it recognises the
> input line is one of the supported formats, and (2) send any
> unrecognised line, not just an empty one, with "unrecognised" status
> code.  That way, the caller can handle more than single kind of
> "special input line" more easily, perhaps?

This is an interesting idea. The simplest way to do this would probably to
have 'bad_line' stop printing the "malformed input line" error and instead
return INDEX_INFO_UNRECOGNIZED_LINE, and pass in a strbuf so that the
"malformed" line is available to the caller.

That seems simple enough, so I'll include it in V2; I can always revert back
to INDEX_INFO_EMPTY_LINE if the generalized approach doesn't work out for
some reason.

> 
> Thanks.
diff mbox series

Patch

diff --git a/builtin/update-index.c b/builtin/update-index.c
index 77df380cb54..b1b334807f8 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -788,7 +788,9 @@  static enum parse_opt_result stdin_cacheinfo_callback(
 		return error("option '%s' must be the last argument", opt->long_name);
 	allow_add = allow_replace = allow_remove = 1;
 	ret = read_index_info(*nul_term_line, apply_index_info, NULL);
-	if (ret)
+	if (ret == INDEX_INFO_EMPTY_LINE)
+		return error("malformed input line ''");
+	else if (ret < 0)
 		return -1;
 
 	return 0;
diff --git a/index-info.c b/index-info.c
index 0b68e34c361..735cbf1f476 100644
--- a/index-info.c
+++ b/index-info.c
@@ -22,6 +22,11 @@  int read_index_info(int nul_term_line, each_index_info_fn fn, void *cbdata)
 		unsigned long ul;
 		int stage;
 
+		if (!buf.len) {
+			ret = INDEX_INFO_EMPTY_LINE;
+			break;
+		}
+
 		/* This reads lines formatted in one of three formats:
 		 *
 		 * (1) mode         SP sha1          TAB path
diff --git a/index-info.h b/index-info.h
index d650498325a..1884972021d 100644
--- a/index-info.h
+++ b/index-info.h
@@ -5,6 +5,8 @@ 
 
 typedef int (*each_index_info_fn)(unsigned int, struct object_id *, int, const char *, void *);
 
+#define INDEX_INFO_EMPTY_LINE 1
+
 /* Iterate over parsed index info from stdin */
 int read_index_info(int nul_term_line, each_index_info_fn fn, void *cbdata);