From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH 1/3] for_each_reflog_ent(): extract a helper to process a single entry
Date: Fri, 8 Mar 2013 13:53:42 -0800 [thread overview]
Message-ID: <1362779624-15513-2-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1362779624-15513-1-git-send-email-gitster@pobox.com>
Split the logic that takes a single line of reflog entry in a strbuf
parses it and calls the callback function out of the loop into a
separate helper function.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
refs.c | 59 ++++++++++++++++++++++++++++++-----------------------------
1 file changed, 30 insertions(+), 29 deletions(-)
diff --git a/refs.c b/refs.c
index da74a2b..9f702a7 100644
--- a/refs.c
+++ b/refs.c
@@ -2290,6 +2290,34 @@ int read_ref_at(const char *refname, unsigned long at_time, int cnt,
return 1;
}
+static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
+{
+ unsigned char osha1[20], nsha1[20];
+ char *email_end, *message;
+ unsigned long timestamp;
+ int tz;
+
+ /* old SP new SP name <email> SP time TAB msg LF */
+ if (sb->len < 83 || sb->buf[sb->len - 1] != '\n' ||
+ get_sha1_hex(sb->buf, osha1) || sb->buf[40] != ' ' ||
+ get_sha1_hex(sb->buf + 41, nsha1) || sb->buf[81] != ' ' ||
+ !(email_end = strchr(sb->buf + 82, '>')) ||
+ email_end[1] != ' ' ||
+ !(timestamp = strtoul(email_end + 2, &message, 10)) ||
+ !message || message[0] != ' ' ||
+ (message[1] != '+' && message[1] != '-') ||
+ !isdigit(message[2]) || !isdigit(message[3]) ||
+ !isdigit(message[4]) || !isdigit(message[5]))
+ return 0; /* corrupt? */
+ email_end[1] = '\0';
+ tz = strtol(message + 1, NULL, 10);
+ if (message[6] != '\t')
+ message += 6;
+ else
+ message += 7;
+ return fn(osha1, nsha1, sb->buf + 82, timestamp, tz, message, cb_data);
+}
+
int for_each_recent_reflog_ent(const char *refname, each_reflog_ent_fn fn, long ofs, void *cb_data)
{
const char *logfile;
@@ -2314,35 +2342,8 @@ int for_each_recent_reflog_ent(const char *refname, each_reflog_ent_fn fn, long
}
}
- while (!strbuf_getwholeline(&sb, logfp, '\n')) {
- unsigned char osha1[20], nsha1[20];
- char *email_end, *message;
- unsigned long timestamp;
- int tz;
-
- /* old SP new SP name <email> SP time TAB msg LF */
- if (sb.len < 83 || sb.buf[sb.len - 1] != '\n' ||
- get_sha1_hex(sb.buf, osha1) || sb.buf[40] != ' ' ||
- get_sha1_hex(sb.buf + 41, nsha1) || sb.buf[81] != ' ' ||
- !(email_end = strchr(sb.buf + 82, '>')) ||
- email_end[1] != ' ' ||
- !(timestamp = strtoul(email_end + 2, &message, 10)) ||
- !message || message[0] != ' ' ||
- (message[1] != '+' && message[1] != '-') ||
- !isdigit(message[2]) || !isdigit(message[3]) ||
- !isdigit(message[4]) || !isdigit(message[5]))
- continue; /* corrupt? */
- email_end[1] = '\0';
- tz = strtol(message + 1, NULL, 10);
- if (message[6] != '\t')
- message += 6;
- else
- message += 7;
- ret = fn(osha1, nsha1, sb.buf + 82, timestamp, tz, message,
- cb_data);
- if (ret)
- break;
- }
+ while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
+ ret = show_one_reflog_ent(&sb, fn, cb_data);
fclose(logfp);
strbuf_release(&sb);
return ret;
--
1.8.2-rc3-189-g94c4d42
next prev parent reply other threads:[~2013-03-08 21:54 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-03-08 21:53 [PATCH 0/3] Enumerating reflog entries in a wrong order Junio C Hamano
2013-03-08 21:53 ` Junio C Hamano [this message]
2013-03-08 21:53 ` [PATCH 2/3] for_each_recent_reflog_ent(): simplify opening of a reflog file Junio C Hamano
2013-03-08 21:53 ` [PATCH 3/3] reflog: add for_each_reflog_ent_reverse() API Junio C Hamano
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1362779624-15513-2-git-send-email-gitster@pobox.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
/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.
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 NNTP newsgroup(s).