Support 'master@2 hours ago' syntax
Extended sha1 expressions may now include date specifications which indicate a point in time within the local repository's history. If the ref indicated to the left of '@' has a log in $GIT_DIR/logs/<ref> then the value of the ref at the time indicated by the specification is obtained from the ref's log. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn Pearce committed
May 17, 2006 at 05:56 UTC
d556fae2c0ea063f15ea4c3678fe931cc426f1ca
4 files changed
+104
-17
Documentation/git-rev-parse.txt
+6
@@ -124,6 +124,12 @@ syntax.
124
happen to have both heads/master and tags/master, you can
125
explicitly say 'heads/master' to tell git which one you mean.
126
127
+* A suffix '@' followed by a date specification such as 'yesterday'
128
+ (24 hours ago) or '1 month 2 weeks 3 days 1 hour 1 second ago'
129
+ to specify the value of the ref at a prior point in time.
130
+ This suffix may only be used immediately following a ref name
131
+ and the ref must have an existing log ($GIT_DIR/logs/<ref>).
132
+
133
* A suffix '{caret}' to a revision parameter means the first parent of
134
that commit object. '{caret}<n>' means the <n>th parent (i.e.
135
'rev{caret}'
refs.c
+53
@@ -429,3 +429,56 @@ int write_ref_sha1(struct ref_lock *lock,
429
unlock_ref(lock);
430
return 0;
431
}
432
+
433
+int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
434
+{
435
+ const char *logfile, *logdata, *logend, *rec, *c;
436
+ char *tz_c;
437
+ int logfd, tz;
438
+ struct stat st;
439
+ unsigned long date;
440
+
441
+ logfile = git_path("logs/%s", ref);
442
+ logfd = open(logfile, O_RDONLY, 0);
443
+ if (logfd < 0)
444
+ die("Unable to read log %s: %s", logfile, strerror(errno));
445
+ fstat(logfd, &st);
446
+ if (!st.st_size)
447
+ die("Log %s is empty.", logfile);
448
+ logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
449
+ close(logfd);
450
+
451
+ rec = logend = logdata + st.st_size;
452
+ while (logdata < rec) {
453
+ if (logdata < rec && *(rec-1) == '\n')
454
+ rec--;
455
+ while (logdata < rec && *(rec-1) != '\n')
456
+ rec--;
457
+ c = rec;
458
+ while (c < logend && *c != '>' && *c != '\n')
459
+ c++;
460
+ if (c == logend || *c == '\n')
461
+ die("Log %s is corrupt.", logfile);
462
+ date = strtoul(c, NULL, 10);
463
+ if (date <= at_time) {
464
+ if (get_sha1_hex(rec + 41, sha1))
465
+ die("Log %s is corrupt.", logfile);
466
+ munmap((void*)logdata, st.st_size);
467
+ return 0;
468
+ }
469
+ }
470
+
471
+ c = logdata;
472
+ while (c < logend && *c != '>' && *c != '\n')
473
+ c++;
474
+ if (c == logend || *c == '\n')
475
+ die("Log %s is corrupt.", logfile);
476
+ date = strtoul(c, &tz_c, 10);
477
+ tz = strtoul(tz_c, NULL, 10);
478
+ if (get_sha1_hex(logdata, sha1))
479
+ die("Log %s is corrupt.", logfile);
480
+ munmap((void*)logdata, st.st_size);
481
+ fprintf(stderr, "warning: Log %s only goes back to %s.\n",
482
+ logfile, show_rfc2822_date(date, tz));
483
+ return 0;
484
+}
refs.h
+3
@@ -34,6 +34,9 @@ extern void unlock_ref (struct ref_lock *lock);
34
/** Writes sha1 into the ref specified by the lock. **/
35
extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
36
37
+/** Reads log for the value of ref during at_time. **/
38
+extern int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1);
39
+
40
/** Returns 0 if target has the right format for a ref. **/
41
extern int check_ref_format(const char *target);
42
sha1_name.c
+42
-17
@@ -4,6 +4,7 @@
4
#include "tree.h"
5
#include "blob.h"
6
#include "tree-walk.h"
7
+#include "refs.h"
8
9
static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
10
{
@@ -245,36 +246,60 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
246
"refs/remotes/%.*s/HEAD",
247
NULL
248
};
248
- const char **p;
249
- const char *warning = "warning: refname '%.*s' is ambiguous.\n";
250
- char *pathname;
251
- int already_found = 0;
249
+ static const char *warning = "warning: refname '%.*s' is ambiguous.\n";
250
+ const char **p, *pathname;
251
+ char *real_path = NULL;
252
+ int refs_found = 0, at_mark;
253
+ unsigned long at_time = (unsigned long)-1;
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 (at_mark = 1; at_mark < len; at_mark++) {
262
+ if (str[at_mark] == '@') {
263
+ int date_len = len - at_mark - 1;
264
+ char *date_spec = xmalloc(date_len + 1);
265
+ strncpy(date_spec, str + at_mark + 1, date_len);
266
+ date_spec[date_len] = 0;
267
+ at_time = approxidate(date_spec);
268
+ free(date_spec);
269
+ len = at_mark;
270
+ }
271
+ }
272
+
273
/* Accept only unambiguous ref paths. */
274
if (ambiguous_path(str, len))
275
return -1;
276
277
for (p = fmt; *p; p++) {
263
- this_result = already_found ? sha1_from_ref : sha1;
264
- pathname = git_path(*p, len, str);
265
- if (!read_ref(pathname, this_result)) {
266
- if (warn_ambiguous_refs) {
267
- if (already_found)
268
- fprintf(stderr, warning, len, str);
269
- already_found++;
270
- }
271
- else
272
- return 0;
278
+ this_result = refs_found ? sha1_from_ref : sha1;
279
+ pathname = resolve_ref(git_path(*p, len, str), this_result, 1);
280
+ if (pathname) {
281
+ if (!refs_found++)
282
+ real_path = strdup(pathname);
283
+ if (!warn_ambiguous_refs)
284
+ break;
285
}
286
}
275
- if (already_found)
276
- return 0;
277
- return -1;
287
+
288
+ if (!refs_found)
289
+ return -1;
290
+
291
+ if (warn_ambiguous_refs && refs_found > 1)
292
+ fprintf(stderr, warning, len, str);
293
+
294
+ if (at_time != (unsigned long)-1) {
295
+ read_ref_at(
296
+ real_path + strlen(git_path(".")) - 1,
297
+ at_time,
298
+ sha1);
299
+ }
300
+
301
+ free(real_path);
302
+ return 0;
303
}
304
305
static int get_sha1_1(const char *name, int len, unsigned char *sha1);