@matheusbsbs / matheus-bagliano / commits / 883d60fa97
Fork 0

Sanitize for_each_reflog_ent()

It used to ignore the return value of the helper function; now, it expects it to return 0, and stops iteration upon non-zero return values; this value is then passed on as the return value of for_each_reflog_ent(). Further, it makes no sense to force the parsing upon the helper functions; for_each_reflog_ent() now calls the helper function with old and new sha1, the email, the timestamp & timezone, and the message. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jan 8, 2007 at 01:59 UTC 883d60fa97c6397450fb129634054e0a6101baac
6 files changed +44 -24
builtin-reflog.c
+12 -14
@@ -195,19 +195,12 @@ static int keep_entry(struct commit **it, unsigned char *sha1)
195 }
196
197 static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
198 - char *data, void *cb_data)
198 + const char *email, unsigned long timestamp, int tz,
199 + const char *message, void *cb_data)
200 {
201 struct expire_reflog_cb *cb = cb_data;
201 - unsigned long timestamp;
202 - char *cp, *ep;
202 struct commit *old, *new;
203
205 - cp = strchr(data, '>');
206 - if (!cp || *++cp != ' ')
207 - goto prune;
208 - timestamp = strtoul(cp, &ep, 10);
209 - if (*ep != ' ')
210 - goto prune;
204 if (timestamp < cb->cmd->expire_total)
205 goto prune;
206
@@ -221,15 +214,20 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
214 (new && !in_merge_bases(new, cb->ref_commit))))
215 goto prune;
216
224 - if (cb->newlog)
225 - fprintf(cb->newlog, "%s %s %s",
226 - sha1_to_hex(osha1), sha1_to_hex(nsha1), data);
217 + if (cb->newlog) {
218 + char sign = (tz < 0) ? '-' : '+';
219 + int zone = (tz < 0) ? (-tz) : tz;
220 + fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
221 + sha1_to_hex(osha1), sha1_to_hex(nsha1),
222 + email, timestamp, sign, zone,
223 + message);
224 + }
225 if (cb->cmd->verbose)
228 - printf("keep %s", data);
226 + printf("keep %s", message);
227 return 0;
228 prune:
229 if (!cb->newlog || cb->cmd->verbose)
232 - printf("%sprune %s", cb->newlog ? "" : "would ", data);
230 + printf("%sprune %s", cb->newlog ? "" : "would ", message);
231 return 0;
232 }
233
fsck-objects.c
+3 -1
@@ -399,7 +399,9 @@ static void fsck_dir(int i, char *path)
399
400 static int default_refs;
401
402 -static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1, char *datail, void *cb_data)
402 +static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
403 + const char *email, unsigned long timestamp, int tz,
404 + const char *message, void *cb_data)
405 {
406 struct object *obj;
407
reachable.c
+3 -1
@@ -104,7 +104,9 @@ static void walk_commit_list(struct rev_info *revs)
104 }
105 }
106
107 -static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1, char *datail, void *cb_data)
107 +static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
108 + const char *email, unsigned long timestamp, int tz,
109 + const char *message, void *cb_data)
110 {
111 struct object *object;
112 struct rev_info *revs = (struct rev_info *)cb_data;
refs.c
+21 -5
@@ -1097,7 +1097,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1097 return 0;
1098 }
1099
1100 -void for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
1100 +int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
1101 {
1102 const char *logfile;
1103 FILE *logfp;
@@ -1106,19 +1106,35 @@ void for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
1106 logfile = git_path("logs/%s", ref);
1107 logfp = fopen(logfile, "r");
1108 if (!logfp)
1109 - return;
1109 + return -1;
1110 while (fgets(buf, sizeof(buf), logfp)) {
1111 unsigned char osha1[20], nsha1[20];
1112 - int len;
1112 + char *email_end, *message;
1113 + unsigned long timestamp;
1114 + int len, ret, tz;
1115
1116 /* old SP new SP name <email> SP time TAB msg LF */
1117 len = strlen(buf);
1118 if (len < 83 || buf[len-1] != '\n' ||
1119 get_sha1_hex(buf, osha1) || buf[40] != ' ' ||
1118 - get_sha1_hex(buf + 41, nsha1) || buf[81] != ' ')
1120 + get_sha1_hex(buf + 41, nsha1) || buf[81] != ' ' ||
1121 + !(email_end = strchr(buf + 82, '>')) ||
1122 + email_end[1] != ' ' ||
1123 + !(timestamp = strtoul(email_end + 2, &message, 10)) ||
1124 + !message || message[0] != ' ' ||
1125 + (message[1] != '+' && message[1] != '-') ||
1126 + !isdigit(message[2]) || !isdigit(message[3]) ||
1127 + !isdigit(message[4]) || !isdigit(message[5]) ||
1128 + message[6] != '\t')
1129 continue; /* corrupt? */
1120 - fn(osha1, nsha1, buf+82, cb_data);
1130 + email_end[1] = '\0';
1131 + tz = strtol(message + 1, NULL, 10);
1132 + message += 7;
1133 + ret = fn(osha1, nsha1, buf+82, timestamp, tz, message, cb_data);
1134 + if (ret)
1135 + return ret;
1136 }
1137 fclose(logfp);
1138 + return 0;
1139 }
1140
refs.h
+2 -2
@@ -45,8 +45,8 @@ extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, cons
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);
48 +typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *);
49 +int 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);
revision.c
+3 -1
@@ -505,7 +505,9 @@ static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
505 }
506 }
507
508 -static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1, char *detail, void *cb_data)
508 +static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
509 + const char *email, unsigned long timestamp, int tz,
510 + const char *message, void *cb_data)
511 {
512 handle_one_reflog_commit(osha1, cb_data);
513 handle_one_reflog_commit(nsha1, cb_data);