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

General ref log reading improvements.

Corrected the log starting time displayed in the error message (as it was always showing the epoch due to a bad input to strtoul). Improved the log parser so we only scan backwards towards the '\n' from the end of the prior log; during this scan the last '>' is remembered to improve performance (rather than scanning forward to it). If the log record matched is the last log record in the file only use its new sha1 value if the date matches exactly; otherwise we leave the passed in sha1 alone as it already contains the current value of the ref. This way lookups of dates later than the log end to stick with the current ref value in case the ref was updated without logging. If it looks like someone changed the ref without logging it and we are going to return the sha1 which should have been valid during the missing period then warn the user that there might be log data missing and thus their query result may not be accurate. The check isn't perfect as its just based on comparing the old and new sha1 values between the two log records but its better than not checking at all. Implemented test cases based on git-rev-parse for most of the boundary conditions. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn Pearce committed May 19, 2006 at 03:28 UTC e52290428b5b6f55cb7bb7a4624751750241d5aa
2 files changed +104 -14
refs.c
+40 -14
@@ -432,11 +432,12 @@ int write_ref_sha1(struct ref_lock *lock,
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;
435 + const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
436 char *tz_c;
437 int logfd, tz;
438 struct stat st;
439 unsigned long date;
440 + unsigned char logged_sha1[20];
441
442 logfile = git_path("logs/%s", ref);
443 logfd = open(logfile, O_RDONLY, 0);
@@ -448,32 +449,57 @@ int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
449 logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
450 close(logfd);
451
452 + lastrec = NULL;
453 rec = logend = logdata + st.st_size;
454 while (logdata < rec) {
455 if (logdata < rec && *(rec-1) == '\n')
456 rec--;
455 - while (logdata < rec && *(rec-1) != '\n')
457 + lastgt = NULL;
458 + while (logdata < rec && *(rec-1) != '\n') {
459 rec--;
457 - c = rec;
458 - while (c < logend && *c != '>' && *c != '\n')
459 - c++;
460 - if (c == logend || *c == '\n')
460 + if (*rec == '>')
461 + lastgt = rec;
462 + }
463 + if (!lastgt)
464 die("Log %s is corrupt.", logfile);
462 - date = strtoul(c + 1, NULL, 10);
465 + date = strtoul(lastgt + 1, &tz_c, 10);
466 if (date <= at_time) {
464 - if (get_sha1_hex(rec + 41, sha1))
465 - die("Log %s is corrupt.", logfile);
467 + if (lastrec) {
468 + if (get_sha1_hex(lastrec, logged_sha1))
469 + die("Log %s is corrupt.", logfile);
470 + if (get_sha1_hex(rec + 41, sha1))
471 + die("Log %s is corrupt.", logfile);
472 + if (memcmp(logged_sha1, sha1, 20)) {
473 + tz = strtoul(tz_c, NULL, 10);
474 + fprintf(stderr,
475 + "warning: Log %s has gap after %s.\n",
476 + logfile, show_rfc2822_date(date, tz));
477 + }
478 + } else if (date == at_time) {
479 + if (get_sha1_hex(rec + 41, sha1))
480 + die("Log %s is corrupt.", logfile);
481 + } else {
482 + if (get_sha1_hex(rec + 41, logged_sha1))
483 + die("Log %s is corrupt.", logfile);
484 + if (memcmp(logged_sha1, sha1, 20)) {
485 + tz = strtoul(tz_c, NULL, 10);
486 + fprintf(stderr,
487 + "warning: Log %s unexpectedly ended on %s.\n",
488 + logfile, show_rfc2822_date(date, tz));
489 + }
490 + }
491 munmap((void*)logdata, st.st_size);
492 return 0;
493 }
494 + lastrec = rec;
495 }
496
471 - c = logdata;
472 - while (c < logend && *c != '>' && *c != '\n')
473 - c++;
474 - if (c == logend || *c == '\n')
497 + rec = logdata;
498 + while (rec < logend && *rec != '>' && *rec != '\n')
499 + rec++;
500 + if (rec == logend || *rec == '\n')
501 die("Log %s is corrupt.", logfile);
476 - date = strtoul(c, &tz_c, 10);
502 + date = strtoul(rec + 1, &tz_c, 10);
503 tz = strtoul(tz_c, NULL, 10);
504 if (get_sha1_hex(logdata, sha1))
505 die("Log %s is corrupt.", logfile);
t/t1400-update-ref.sh
+64
@@ -9,6 +9,10 @@ test_description='Test git-update-ref and basic ref logging'
9 Z=0000000000000000000000000000000000000000
10 A=1111111111111111111111111111111111111111
11 B=2222222222222222222222222222222222222222
12 +C=3333333333333333333333333333333333333333
13 +D=4444444444444444444444444444444444444444
14 +E=5555555555555555555555555555555555555555
15 +F=6666666666666666666666666666666666666666
16 m=refs/heads/master
17
18 test_expect_success \
@@ -109,4 +113,64 @@ test_expect_success \
113 'diff expect .git/logs/$m'
114 rm -f .git/$m .git/logs/$m expect
115
116 +git-update-ref $m $D
117 +cat >.git/logs/$m <<EOF
118 +$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
119 +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
120 +$F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
121 +$Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
122 +EOF
123 +
124 +ed="Thu, 26 May 2005 18:32:00 -0500"
125 +gd="Thu, 26 May 2005 18:33:00 -0500"
126 +ld="Thu, 26 May 2005 18:43:00 -0500"
127 +test_expect_success \
128 + 'Query "master@May 25 2005" (before history)' \
129 + 'rm -f o e
130 + git-rev-parse --verify "master@May 25 2005" >o 2>e &&
131 + test $C = $(cat o) &&
132 + test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"'
133 +test_expect_success \
134 + "Query master@2005-05-25 (before history)" \
135 + 'rm -f o e
136 + git-rev-parse --verify master@2005-05-25 >o 2>e &&
137 + test $C = $(cat o) &&
138 + echo test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"'
139 +test_expect_success \
140 + 'Query "master@May 26 2005 23:31:59" (1 second before history)' \
141 + 'rm -f o e
142 + git-rev-parse --verify "master@May 26 2005 23:31:59" >o 2>e &&
143 + test $C = $(cat o) &&
144 + test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"'
145 +test_expect_success \
146 + 'Query "master@May 26 2005 23:32:00" (exactly history start)' \
147 + 'rm -f o e
148 + git-rev-parse --verify "master@May 26 2005 23:32:00" >o 2>e &&
149 + test $A = $(cat o) &&
150 + test "" = "$(cat e)"'
151 +test_expect_success \
152 + 'Query "master@2005-05-26 23:33:01" (middle of history with gap)' \
153 + 'rm -f o e
154 + git-rev-parse --verify "master@2005-05-26 23:33:01" >o 2>e &&
155 + test $B = $(cat o) &&
156 + test "warning: Log .git/logs/$m has gap after $gd." = "$(cat e)"'
157 +test_expect_success \
158 + 'Query "master@2005-05-26 23:33:01" (middle of history)' \
159 + 'rm -f o e
160 + git-rev-parse --verify "master@2005-05-26 23:38:00" >o 2>e &&
161 + test $Z = $(cat o) &&
162 + test "" = "$(cat e)"'
163 +test_expect_success \
164 + 'Query "master@2005-05-26 23:43:00" (exact end of history)' \
165 + 'rm -f o e
166 + git-rev-parse --verify "master@2005-05-26 23:43:00" >o 2>e &&
167 + test $E = $(cat o) &&
168 + test "" = "$(cat e)"'
169 +test_expect_success \
170 + 'Query "master@2005-05-28" (past end of history)' \
171 + 'rm -f o e
172 + git-rev-parse --verify "master@2005-05-28" >o 2>e &&
173 + test $D = $(cat o) &&
174 + test "warning: Log .git/logs/$m unexpectedly ended on $ld." = "$(cat e)"'
175 +
176 test_done