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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Pieter de Bie <pdebie@xxxxxxxxx>
---

    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


--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux