git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] builtin-name-rev: refactor stdin handling to its own function

From
PBPieter de Bie <pdebie@ai.rug.nl>
Date
Aug 1, 2008, 11:16 UTC
Message-ID
<1217589372-4151-1-git-send-email-pdebie@ai.rug.nl>
In-Reply-To
<alpine.DEB.1.00.0808011256330.9611@pacific.mpi-cbg.de.mpi-cbg.de>
Signed-off-by: Pieter de Bie <pdebie@ai.rug.nl>
---
    On 1 aug 2008, at 09:23, Junio C Hamano wrote:
    >Is it just me to find that this part is getting indented too deeply to be
    >readable?
    How about something like this then?
 builtin-name-rev.c |   93 ++++++++++++++++++++++++++++------------------------
 1 files changed, 50 insertions(+), 43 deletions(-)
diff --git a/builtin-name-rev.c b/builtin-name-rev.c
index 0536af4..057172d 100644
--- a/builtin-name-rev.c
+++ b/builtin-name-rev.c
@@ -176,6 +176,52 @@ static char const * const name_rev_usage[] = {
 	NULL
 };
 
+static void handle_stdin_line(char *p_start, int name_only)
+{
+	int forty = 0;
+	char *p;
+	for (p = p_start; *p; p++) {
+#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
+		if (!ishex(*p))
+			forty = 0;
+		else if (++forty == 40 && !ishex(*(p+1))) {
+			unsigned char sha1[40];
+			const char *name = NULL;
+			char c = *(p+1);
+
+			forty = 0;
+
+			*(p+1) = 0;
+			if (!get_sha1(p - 39, sha1)) {
+				struct object *o =
+					lookup_object(sha1);
+				if (o)
+					name = get_rev_name(o);
+			}
+			*(p+1) = c;
+
+			if (!name)
+				continue;
+
+			if (name_only) {
+				fwrite(p_start, p - p_start + 1 - 40,
+					1, stdout);sssss
+				printf(name);
+			}
+			else {
+				fwrite(p_start, p - p_start + 1, 1, stdout);
+				printf(" (%s)", name);
+			}
+			p_start = p + 1;
+		}
+	}
+
+	/* flush */
+	if (p != p_start)
+		fwrite(p_start, p - p_start, 1, stdout);
+
+}
+
 int cmd_name_rev(int argc, const char **argv, const char *prefix)
 {
 	struct object_array revs = { 0, 0, NULL };
@@ -234,53 +280,14 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 
 	if (transform_stdin) {
 		char buffer[2048];
-		char *p, *p_start;
+		char *p_start;
 
 		while (!feof(stdin)) {
-			int forty = 0;
-			p = fgets(buffer, sizeof(buffer), stdin);
-			if (!p)
+			p_start = fgets(buffer, sizeof(buffer), stdin);
+			if (!p_start)
 				break;
 
-			for (p_start = p; *p; p++) {
-#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
-				if (!ishex(*p))
-					forty = 0;
-				else if (++forty == 40 &&
-						!ishex(*(p+1))) {
-					unsigned char sha1[40];
-					const char *name = NULL;
-					char c = *(p+1);
-
-					forty = 0;
-
-					*(p+1) = 0;
-					if (!get_sha1(p - 39, sha1)) {
-						struct object *o =
-							lookup_object(sha1);
-						if (o)
-							name = get_rev_name(o);
-					}
-					*(p+1) = c;
-
-					if (!name)
-						continue;
-
-					if (data.name_only) {
-						fwrite(p_start, p - p_start + 1 - 40, 1, stdout);
-						printf(name);
-					}
-					else {
-						fwrite(p_start, p - p_start + 1, 1, stdout);
-						printf(" (%s)", name);
-					}
-					p_start = p + 1;
-				}
-			}
-
-			/* flush */
-			if (p_start != p)
-				fwrite(p_start, p - p_start, 1, stdout);
+			handle_stdin_line(p_start, data.name_only);
 		}
 	} else if (all) {
 		int i, max;
-- 
1.6.0.rc1.214.g5f0bd
Previous: Johannes SchindelinNext: Junio C Hamano
Message 4 of 8 in “git-name-rev: allow --name-only in combination with --stdin”
  1. git-name-rev: allow --name-only in combination with --stdinPieter de Bie, Jul 31, 2008
  2. Junio C HamanoAug 1, 2008
  3. Johannes SchindelinAug 1, 2008
  4. builtin-name-rev: refactor stdin handling to its own functionPieter de Bie, Aug 1, 2008
  5. Junio C HamanoAug 1, 2008
  6. Junio C HamanoAug 2, 2008
  7. git-name-rev: don't use printf without formatRené Scharfe, Aug 3, 2008
  8. Junio C HamanoAug 3, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.