add for_each_reflog_ent() iterator
Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Dec 18, 2006 at 01:18 UTC
2ff81662c2fb2679f841c09a28bd80c0a63011ac
2 files changed
+29
refs.c
+25
@@ -1091,3 +1091,28 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1091
logfile, show_rfc2822_date(date, tz));
1092
return 0;
1093
}
1094
+
1095
+void for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
1096
+{
1097
+ const char *logfile;
1098
+ FILE *logfp;
1099
+ char buf[1024];
1100
+
1101
+ logfile = git_path("logs/%s", ref);
1102
+ logfp = fopen(logfile, "r");
1103
+ if (!logfp)
1104
+ return;
1105
+ while (fgets(buf, sizeof(buf), logfp)) {
1106
+ unsigned char osha1[20], nsha1[20];
1107
+ int len;
1108
+
1109
+ /* old SP new SP name <email> SP time TAB msg LF */
1110
+ len = strlen(buf);
1111
+ if (len < 83 || buf[len-1] != '\n' ||
1112
+ get_sha1_hex(buf, osha1) || buf[40] != ' ' ||
1113
+ get_sha1_hex(buf + 41, nsha1) || buf[81] != ' ')
1114
+ continue; /* corrupt? */
1115
+ fn(osha1, nsha1, buf+82, cb_data);
1116
+ }
1117
+ fclose(logfp);
1118
+}
refs.h
+4
@@ -44,6 +44,10 @@ extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, cons
44
/** Reads log for the value of ref during at_time. **/
45
extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1);
46
47
+/* iterate over reflog entries */
48
+typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, char *, void *);
49
+void for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data);
50
+
51
/** Returns 0 if target has the right format for a ref. **/
52
extern int check_ref_format(const char *target);
53