@matheusbsbs / matheus-bagliano / commits / 16d7cc90dd
Fork 0

Extend read_ref_at() to be usable from places other than sha1_name.

You can pass an extra argument to the function to receive the reflog message information. Also when the log does not go back beyond the point the user asked, the cut-off time and count are given back to the caller for emitting the error messages as appropriately. We could later add configuration for get_sha1_basic() to make it an error instead of it being just a warning. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jan 19, 2007 at 01:19 UTC 16d7cc90dd6e68a170362ae6f0fbfbc504fd246b
4 files changed +52 -13
refs.c
+33 -8
@@ -1017,7 +1017,21 @@ int write_ref_sha1(struct ref_lock *lock,
1017 return 0;
1018 }
1019
1020 -int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1)
1020 +static char *ref_msg(const char *line, const char *endp)
1021 +{
1022 + const char *ep;
1023 + char *msg;
1024 +
1025 + line += 82;
1026 + for (ep = line; ep < endp && *ep != '\n'; ep++)
1027 + ;
1028 + msg = xmalloc(ep - line + 1);
1029 + memcpy(msg, line, ep - line);
1030 + msg[ep - line] = 0;
1031 + return msg;
1032 +}
1033 +
1034 +int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
1035 {
1036 const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
1037 char *tz_c;
@@ -1054,6 +1068,14 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1068 die("Log %s is corrupt.", logfile);
1069 date = strtoul(lastgt + 1, &tz_c, 10);
1070 if (date <= at_time || cnt == 0) {
1071 + if (msg)
1072 + *msg = ref_msg(rec, logend);
1073 + if (cutoff_time)
1074 + *cutoff_time = date;
1075 + if (cutoff_tz)
1076 + *cutoff_tz = tz;
1077 + if (cutoff_cnt)
1078 + *cutoff_cnt = reccnt;
1079 if (lastrec) {
1080 if (get_sha1_hex(lastrec, logged_sha1))
1081 die("Log %s is corrupt.", logfile);
@@ -1097,14 +1119,17 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1119 tz = strtoul(tz_c, NULL, 10);
1120 if (get_sha1_hex(logdata, sha1))
1121 die("Log %s is corrupt.", logfile);
1122 + if (msg)
1123 + *msg = ref_msg(logdata, logend);
1124 munmap(log_mapped, st.st_size);
1101 - if (at_time)
1102 - fprintf(stderr, "warning: Log %s only goes back to %s.\n",
1103 - logfile, show_rfc2822_date(date, tz));
1104 - else
1105 - fprintf(stderr, "warning: Log %s only has %d entries.\n",
1106 - logfile, reccnt);
1107 - return 0;
1125 +
1126 + if (cutoff_time)
1127 + *cutoff_time = date;
1128 + if (cutoff_tz)
1129 + *cutoff_tz = tz;
1130 + if (cutoff_cnt)
1131 + *cutoff_cnt = reccnt;
1132 + return 1;
1133 }
1134
1135 int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
refs.h
+1 -1
@@ -42,7 +42,7 @@ extern void unlock_ref(struct ref_lock *lock);
42 extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
43
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);
45 +extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
46
47 /* iterate over reflog entries */
48 typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *);
sha1_name.c
+15 -1
@@ -304,6 +304,9 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
304 /* Is it asking for N-th entry, or approxidate? */
305 int nth, i;
306 unsigned long at_time;
307 + unsigned long co_time;
308 + int co_tz, co_cnt;
309 +
310 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
311 char ch = str[at+2+i];
312 if ('0' <= ch && ch <= '9')
@@ -315,7 +318,18 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
318 at_time = 0;
319 else
320 at_time = approxidate(str + at + 2);
318 - read_ref_at(real_ref, at_time, nth, sha1);
321 + if (read_ref_at(real_ref, at_time, nth, sha1, NULL,
322 + &co_time, &co_tz, &co_cnt)) {
323 + if (at_time)
324 + fprintf(stderr,
325 + "warning: Log for '%.*s' only goes "
326 + "back to %s.\n", len, str,
327 + show_rfc2822_date(co_time, co_tz));
328 + else
329 + fprintf(stderr,
330 + "warning: Log for '%.*s' only has "
331 + "%d entries.\n", len, str, co_cnt);
332 + }
333 }
334
335 free(real_ref);
t/t1400-update-ref.sh
+3 -3
@@ -138,19 +138,19 @@ test_expect_success \
138 'rm -f o e
139 git-rev-parse --verify "master@{May 25 2005}" >o 2>e &&
140 test '"$C"' = $(cat o) &&
141 - test "warning: Log .git/logs/'"$m only goes back to $ed"'." = "$(cat e)"'
141 + test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
142 test_expect_success \
143 "Query master@{2005-05-25} (before history)" \
144 'rm -f o e
145 git-rev-parse --verify master@{2005-05-25} >o 2>e &&
146 test '"$C"' = $(cat o) &&
147 - echo test "warning: Log .git/logs/'"$m only goes back to $ed"'." = "$(cat e)"'
147 + echo test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
148 test_expect_success \
149 'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \
150 'rm -f o e
151 git-rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e &&
152 test '"$C"' = $(cat o) &&
153 - test "warning: Log .git/logs/'"$m only goes back to $ed"'." = "$(cat e)"'
153 + test "warning: Log for '\''master'\'' only goes back to $ed." = "$(cat e)"'
154 test_expect_success \
155 'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \
156 'rm -f o e