@matheusbsbs / matheus-bagliano / commits / ab2a1a32ff
Fork 0

ref-log: allow ref@{count} syntax.

Often I find myself wanting to say 'tip of "next" before I merged the last three topics'. Now I can say that with: git log next@{3}..next Since small integers alone are invalid input strings to approxidate, there is no fear of confusion. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 5, 2006 at 23:16 UTC ab2a1a32ffa5a39aaf4204bd717562bce49e0a36
3 files changed +32 -20
refs.c
+4 -2
@@ -795,7 +795,7 @@ int write_ref_sha1(struct ref_lock *lock,
795 return 0;
796 }
797
798 -int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
798 +int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1)
799 {
800 const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
801 char *tz_c;
@@ -828,7 +828,7 @@ int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
828 if (!lastgt)
829 die("Log %s is corrupt.", logfile);
830 date = strtoul(lastgt + 1, &tz_c, 10);
831 - if (date <= at_time) {
831 + if (date <= at_time || cnt == 0) {
832 if (lastrec) {
833 if (get_sha1_hex(lastrec, logged_sha1))
834 die("Log %s is corrupt.", logfile);
@@ -859,6 +859,8 @@ int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
859 return 0;
860 }
861 lastrec = rec;
862 + if (cnt > 0)
863 + cnt--;
864 }
865
866 rec = logdata;
refs.h
+1 -1
@@ -39,7 +39,7 @@ extern void unlock_ref(struct ref_lock *lock);
39 extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
40
41 /** Reads log for the value of ref during at_time. **/
42 -extern int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1);
42 +extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1);
43
44 /** Returns 0 if target has the right format for a ref. **/
45 extern int check_ref_format(const char *target);
sha1_name.c
+27 -17
@@ -249,24 +249,23 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
249 static const char *warning = "warning: refname '%.*s' is ambiguous.\n";
250 const char **p, *ref;
251 char *real_ref = NULL;
252 - int refs_found = 0, am;
253 - unsigned long at_time = (unsigned long)-1;
252 + int refs_found = 0;
253 + int at, reflog_len;
254 unsigned char *this_result;
255 unsigned char sha1_from_ref[20];
256
257 if (len == 40 && !get_sha1_hex(str, sha1))
258 return 0;
259
260 - /* At a given period of time? "@{2 hours ago}" */
261 - for (am = 1; am < len - 1; am++) {
262 - if (str[am] == '@' && str[am+1] == '{' && str[len-1] == '}') {
263 - int date_len = len - am - 3;
264 - char *date_spec = xmalloc(date_len + 1);
265 - strlcpy(date_spec, str + am + 2, date_len + 1);
266 - at_time = approxidate(date_spec);
267 - free(date_spec);
268 - len = am;
269 - break;
260 + /* basic@{time or number} format to query ref-log */
261 + reflog_len = 0;
262 + if (str[len-1] == '}') {
263 + for (at = 1; at < len - 1; at++) {
264 + if (str[at] == '@' && str[at+1] == '{') {
265 + reflog_len = (len-1) - (at+2);
266 + len = at;
267 + break;
268 + }
269 }
270 }
271
@@ -291,11 +290,22 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
290 if (warn_ambiguous_refs && refs_found > 1)
291 fprintf(stderr, warning, len, str);
292
294 - if (at_time != (unsigned long)-1) {
295 - read_ref_at(
296 - real_ref,
297 - at_time,
298 - sha1);
293 + if (reflog_len) {
294 + /* Is it asking for N-th entry, or approxidate? */
295 + int nth, i;
296 + unsigned long at_time;
297 + for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
298 + char ch = str[at+2+i];
299 + if ('0' <= ch && ch <= '9')
300 + nth = nth * 10 + ch - '0';
301 + else
302 + nth = -1;
303 + }
304 + if (0 <= nth)
305 + at_time = 0;
306 + else
307 + at_time = approxidate(str + at + 2);
308 + read_ref_at(real_ref, at_time, nth, sha1);
309 }
310
311 free(real_ref);