git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Jeff King <peff@peff.net>
Subject: [PREVIEW v3 7/9] update-index: there are only two line terminators
Date: Wed, 13 Jan 2016 19:03:07 -0800	[thread overview]
Message-ID: <1452740590-16827-8-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1452740590-16827-1-git-send-email-gitster@pobox.com>

The program by default works on LF terminated lines, with an option
to use NUL terminated records, when reading the index-info and
cacheinfo.  Instead of using line_termination that happens to take
LF or NUL, use the value of nul_term_line and switch between
strbuf_getline_{lf,nul} based on it.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/update-index.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/builtin/update-index.c b/builtin/update-index.c
index 7431938..6d90424 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -468,12 +468,14 @@ static void update_one(const char *path)
 	report("add '%s'", path);
 }
 
-static void read_index_info(int line_termination)
+static void read_index_info(int nul_term_line)
 {
 	struct strbuf buf = STRBUF_INIT;
 	struct strbuf uq = STRBUF_INIT;
+	strbuf_getline_fn getline_fn;
 
-	while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
+	getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
+	while (getline_fn(&buf, stdin) != EOF) {
 		char *ptr, *tab;
 		char *path_name;
 		unsigned char sha1[20];
@@ -522,7 +524,7 @@ static void read_index_info(int line_termination)
 			goto bad_line;
 
 		path_name = ptr;
-		if (line_termination && path_name[0] == '"') {
+		if (!nul_term_line && path_name[0] == '"') {
 			strbuf_reset(&uq);
 			if (unquote_c_style(&uq, path_name, NULL)) {
 				die("git update-index: bad quoting of path name");
@@ -844,12 +846,12 @@ static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
 static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
 			      const struct option *opt, int unset)
 {
-	int *line_termination = opt->value;
+	int *nul_term_line = opt->value;
 
 	if (ctx->argc != 1)
 		return error("option '%s' must be the last argument", opt->long_name);
 	allow_add = allow_replace = allow_remove = 1;
-	read_index_info(*line_termination);
+	read_index_info(*nul_term_line);
 	return 0;
 }
 
@@ -901,7 +903,7 @@ static int reupdate_callback(struct parse_opt_ctx_t *ctx,
 
 int cmd_update_index(int argc, const char **argv, const char *prefix)
 {
-	int newfd, entries, has_errors = 0, line_termination = '\n';
+	int newfd, entries, has_errors = 0, nul_term_line = 0;
 	int untracked_cache = -1;
 	int read_from_stdin = 0;
 	int prefix_length = prefix ? strlen(prefix) : 0;
@@ -912,6 +914,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	int split_index = -1;
 	struct lock_file *lock_file;
 	struct parse_opt_ctx_t ctx;
+	strbuf_getline_fn getline_fn;
 	int parseopt_state = PARSE_OPT_UNKNOWN;
 	struct option options[] = {
 		OPT_BIT('q', NULL, &refresh_args.flags,
@@ -963,13 +966,13 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			N_("add to index only; do not add content to object database"), 1),
 		OPT_SET_INT(0, "force-remove", &force_remove,
 			N_("remove named paths even if present in worktree"), 1),
-		OPT_SET_INT('z', NULL, &line_termination,
-			N_("with --stdin: input lines are terminated by null bytes"), '\0'),
+		OPT_SET_INT('z', NULL, &nul_term_line,
+			    N_("with --stdin: input lines are terminated by null bytes"), 1),
 		{OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
 			N_("read list of paths to be updated from standard input"),
 			PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 			(parse_opt_cb *) stdin_callback},
-		{OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL,
+		{OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
 			N_("add entries from standard input to the index"),
 			PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 			(parse_opt_cb *) stdin_cacheinfo_callback},
@@ -1057,6 +1060,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		}
 	}
 	argc = parse_options_end(&ctx);
+
+	getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
 	if (preferred_index_format) {
 		if (preferred_index_format < INDEX_FORMAT_LB ||
 		    INDEX_FORMAT_UB < preferred_index_format)
@@ -1073,9 +1078,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
 
 		setup_work_tree();
-		while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
+		while (getline_fn(&buf, stdin) != EOF) {
 			char *p;
-			if (line_termination && buf.buf[0] == '"') {
+			if (!nul_term_line && buf.buf[0] == '"') {
 				strbuf_reset(&nbuf);
 				if (unquote_c_style(&nbuf, buf.buf, NULL))
 					die("line is badly quoted");
-- 
2.7.0-242-gdd583c7

  parent reply	other threads:[~2016-01-14  3:03 UTC|newest]

Thread overview: 114+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-11 20:21 [PATCH] revisions --stdin: accept CRLF line terminators Johannes Sixt
2015-08-11 21:35 ` Junio C Hamano
2015-08-11 22:14   ` Junio C Hamano
2015-08-12 18:24     ` Johannes Sixt
2015-10-28 22:25     ` [PATCH 00/17] Peace with CRLF Junio C Hamano
2015-10-28 22:25       ` [PATCH 01/17] strbuf: add strbuf_gets() Junio C Hamano
2015-10-28 22:25       ` [PATCH 02/17] check-attr, check-ignore, checkout-index: read paths with strbuf_gets() Junio C Hamano
2015-10-28 22:25       ` [PATCH 03/17] update-index: read --index-info " Junio C Hamano
2015-10-28 22:25       ` [PATCH 04/17] update-index: read list of paths with strbuf_gets() under --stdin Junio C Hamano
2015-10-28 22:25       ` [PATCH 05/17] mktree: read textual tree representation with strbuf_gets() Junio C Hamano
2015-10-28 22:25       ` [PATCH 06/17] hash-object: read --stdin-paths " Junio C Hamano
2015-10-28 22:25       ` [PATCH 07/17] revision: read --stdin " Junio C Hamano
2015-10-28 22:25       ` [PATCH 08/17] rev-parse: read parseopt spec " Junio C Hamano
2015-10-28 22:25       ` [PATCH 09/17] ident.c: read /etc/mailname " Junio C Hamano
2015-10-28 22:25       ` [PATCH 10/17] remote.c: read $GIT_DIR/remotes/* " Junio C Hamano
2015-10-28 22:25       ` [PATCH 11/17] clone/sha1_file: read info/alternates " Junio C Hamano
2015-10-28 22:25       ` [PATCH 12/17] transport-helper: read helper response " Junio C Hamano
2015-10-28 22:25       ` [PATCH 13/17] cat-file: read batch stream " Junio C Hamano
2015-10-28 22:25       ` [PATCH 14/17] column: read lines " Junio C Hamano
2015-10-28 22:25       ` [PATCH 15/17] send-pack: read list of refs " Junio C Hamano
2015-10-28 22:26       ` [PATCH 16/17] grep: read -f file " Junio C Hamano
2015-10-28 22:26       ` [PATCH 17/17] test-sha1-array: read command stream " Junio C Hamano
2015-12-16 22:03       ` [PATCH v2 00/17] Peace with CRLF Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 01/17] strbuf: make strbuf_getline_crlf() global Junio C Hamano
2016-01-04 12:25           ` Johannes Schindelin
2016-01-04 19:17             ` Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 02/17] check-attr, check-ignore, checkout-index: read paths with strbuf_getline_crlf() Junio C Hamano
2016-01-04 12:25           ` Johannes Schindelin
2016-01-04 12:27             ` Johannes Schindelin
2016-01-04 19:20               ` Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 03/17] update-index: read --index-info " Junio C Hamano
2016-01-04 12:27           ` Johannes Schindelin
2016-01-04 19:50             ` Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 04/17] update-index: read list of paths with strbuf_getline_crlf() under --stdin Junio C Hamano
2016-01-04 12:27           ` Johannes Schindelin
2015-12-16 22:03         ` [PATCH v2 05/17] mktree: read textual tree representation with strbuf_getline_crlf() Junio C Hamano
2016-01-04 12:27           ` Johannes Schindelin
2015-12-16 22:03         ` [PATCH v2 06/17] hash-object: read --stdin-paths " Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 07/17] revision: read --stdin " Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 08/17] rev-parse: read parseopt spec " Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 09/17] ident.c: read /etc/mailname " Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 10/17] remote.c: read $GIT_DIR/remotes/* " Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 11/17] clone/sha1_file: read info/alternates " Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 12/17] transport-helper: read helper response " Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 13/17] cat-file: read batch stream " Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 14/17] column: read lines " Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 15/17] send-pack: read list of refs " Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 16/17] grep: read -f file " Junio C Hamano
2016-01-04 12:27           ` Johannes Schindelin
2016-01-04 19:30             ` Junio C Hamano
2015-12-16 22:03         ` [PATCH v2 17/17] test-sha1-array: read command stream " Junio C Hamano
2016-01-04 12:27           ` Johannes Schindelin
2016-01-04 12:25         ` [PATCH v2 00/17] Peace with CRLF Johannes Schindelin
2016-01-14  3:03         ` [PREVIEW v3 0/9] Preview of "Peace with CRLF" rerolled Junio C Hamano
2016-01-14  3:03           ` [PREVIEW v3 1/9] strbuf: miniscule style fix Junio C Hamano
2016-01-14  3:03           ` [PREVIEW v3 2/9] strbuf: make strbuf_getline_crlf() global Junio C Hamano
2016-01-14  3:03           ` [PREVIEW v3 3/9] strbuf: introduce strbuf_getline_{lf,nul}() Junio C Hamano
2016-01-14  3:03           ` [PREVIEW v3 4/9] mktree: there are only two line terminators Junio C Hamano
2016-01-14 10:11             ` Jeff King
2016-01-14  3:03           ` [PREVIEW v3 5/9] check-attr: " Junio C Hamano
2016-01-14  3:03           ` [PREVIEW v3 6/9] check-ignore: " Junio C Hamano
2016-01-14  3:03           ` Junio C Hamano [this message]
2016-01-14 10:09             ` [PREVIEW v3 7/9] update-index: " Jeff King
2016-01-14  3:03           ` [PREVIEW v3 8/9] checkout-index: " Junio C Hamano
2016-01-14 10:18             ` Jeff King
2016-01-14 17:13               ` Junio C Hamano
2016-01-14 20:13                 ` Jeff King
2016-01-14  3:03           ` [PREVIEW v3 9/9] strbuf: give strbuf_getline() to the "most text friendly" variant Junio C Hamano
2016-01-14  3:03           ` [PREVIEW v3 9/9] strbuf: retire strbuf_getline() for now Junio C Hamano
2016-01-14  3:09             ` Junio C Hamano
2016-01-14 10:23           ` [PREVIEW v3 0/9] Preview of "Peace with CRLF" rerolled Jeff King
2016-01-14 23:58           ` [PATCH v4 00/21] Peace with CRLF Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 01/21] strbuf: miniscule style fix Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 02/21] strbuf: make strbuf_getline_crlf() global Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 03/21] strbuf: introduce strbuf_getline_{lf,nul}() Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 04/21] mktree: there are only two possible line terminations Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 05/21] check-attr: " Junio C Hamano
2016-01-15 19:16               ` Jeff King
2016-01-15 19:36                 ` Jeff King
2016-01-15 21:20                   ` Junio C Hamano
2016-01-15 21:23                     ` Jeff King
2016-01-15 21:29                       ` Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 06/21] check-ignore: " Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 07/21] update-index: " Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 08/21] checkout-index: " Junio C Hamano
2016-01-15 20:08               ` Jeff King
2016-01-15 20:20                 ` Jeff King
2016-01-15 21:22                 ` Junio C Hamano
2016-01-15 23:31                   ` Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 09/21] strbuf: give strbuf_getline() to the "most text friendly" variant Junio C Hamano
2016-01-15  1:46               ` Eric Sunshine
2016-01-15 18:02                 ` Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 10/21] hash-object: read --stdin-paths with strbuf_getline() Junio C Hamano
2016-01-15 20:23               ` Jeff King
2016-01-14 23:58             ` [PATCH v4 11/21] revision: read --stdin " Junio C Hamano
2016-01-15  1:50               ` Eric Sunshine
2016-01-15 18:38                 ` Junio C Hamano
2016-01-15 20:27               ` Jeff King
2016-01-14 23:58             ` [PATCH v4 12/21] rev-parse: read parseopt spec " Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 13/21] ident.c: read /etc/mailname " Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 14/21] remote.c: read $GIT_DIR/remotes/* " Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 15/21] clone/sha1_file: read info/alternates " Junio C Hamano
2016-01-15  1:52               ` Eric Sunshine
2016-01-14 23:58             ` [PATCH v4 16/21] transport-helper: read helper response " Junio C Hamano
2016-01-15 20:31               ` Jeff King
2016-01-14 23:58             ` [PATCH v4 17/21] cat-file: read batch stream " Junio C Hamano
2016-01-15 20:41               ` Jeff King
2016-01-14 23:58             ` [PATCH v4 18/21] column: read lines " Junio C Hamano
2016-01-15 20:43               ` Jeff King
2016-01-14 23:58             ` [PATCH v4 19/21] send-pack: read list of refs " Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 20/21] grep: read -f file " Junio C Hamano
2016-01-14 23:58             ` [PATCH v4 21/21] test-sha1-array: read command stream " Junio C Hamano
2016-01-15 19:10             ` [PATCH v4 00/21] Peace with CRLF Jeff King
2016-01-15 20:49             ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1452740590-16827-8-git-send-email-gitster@pobox.com \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).