Skip to contentSkip to conversation

Support 'master@2 hours ago' syntax

RFC In review Shawn Pearce 3 participants 7 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

RFC 5/5 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.

---

 Documentation/git-rev-parse.txt |    6 ++++
 refs.c                          |   53 +++++++++++++++++++++++++++++++++++
 refs.h                          |    3 ++
 sha1_name.c                     |   59 ++++++++++++++++++++++++++++-----------
 4 files changed, 104 insertions(+), 17 deletions(-)

efffa32e4896acd7978767b6a856fc7421060040

Documentation/git-rev-parse.txt +6 -0
index ab896fc..df308c3 100644
@@ -124,6 +124,12 @@ syntax.
   happen to have both heads/master and tags/master, you can
   explicitly say 'heads/master' to tell git which one you mean.
 
+* A suffix '@' followed by a date specification such as 'yesterday'
+  (24 hours ago) or '1 month 2 weeks 3 days 1 hour 1 second ago'
+  to specify the value of the ref at a prior point in time.
+  This suffix may only be used immediately following a ref name
+  and the ref must have an existing log ($GIT_DIR/logs/<ref>).
+
 * A suffix '{caret}' to a revision parameter means the first parent of
   that commit object.  '{caret}<n>' means the <n>th parent (i.e.
   'rev{caret}'
refs.c +53 -0

56 lines not on this page · raw message

refs.h +3 -0
index 43831e9..2c854de 100644
@@ -34,6 +34,9 @@ extern void unlock_ref (struct ref_lock 
 /** Writes sha1 into the ref specified by the lock. **/
 extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
 
+/** Reads log for the value of ref during at_time. **/
+extern int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1);
+
 /** Returns 0 if target has the right format for a ref. **/
 extern int check_ref_format(const char *target);
 
sha1_name.c +42 -17

84 lines not on this page · raw message

Signature · 1 line
1.3.2.g7278
#1 lore raw
Quoted 5 lines · Shawn Pearce

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.

This does not allow '2006-05-17 00:00:00' as the timespec, and the documentation carefully avoids giving that example, but I think it is better to spell that limitation out.

Documentation/git-rev-parse.txt syntax. · line 131

Quoted 5 lines
Documentation/git-rev-parse.txt +5 -0
+* A suffix '@' followed by a date specification such as 'yesterday'
+  (24 hours ago) or '1 month 2 weeks 3 days 1 hour 1 second ago'
+  to specify the value of the ref at a prior point in time.
+  This suffix may only be used immediately following a ref name
+  and the ref must have an existing log ($GIT_DIR/logs/<ref>).

+ fprintf(stderr, "warning: Log %s only goes back to %s.\n",
+ logfile, show_rfc2822_date(date, tz));
+ return 0;

I am not sure about this part. If the oldest log entry was 3 hours ago, the second oldest 2 hours ago, we can tell during that one hour period the ref was at that point. If the user asked "ref as of four hours ago", and if the oldest log entry had old SHA1 that is not 0{40} (because the log was not enabled before that record), it might make more sense to give that back.

Also I wonder how much complexity would we suffer and how much efficiency would we gain if we binary search the logdata (the committer info is variable length, so you would need to resync in each step).

#2 lore raw
Quoted 6 lines · Junio C Hamano

Shawn Pearce <[email protected]> writes:

refs.c +3 -0
+	fprintf(stderr, "warning: Log %s only goes back to %s.\n",
+		logfile, show_rfc2822_date(date, tz));
+	return 0;
Quoted 6 lines

I am not sure about this part. If the oldest log entry was 3 hours ago, the second oldest 2 hours ago, we can tell during that one hour period the ref was at that point. If the user asked "ref as of four hours ago", and if the oldest log entry had old SHA1 that is not 0{40} (because the log was not enabled before that record), it might make more sense to give that back.

If I understand my own code here what I'm doing is walking back in the log file, realizing I fell off the first line of it, then loading the old ref from the first line. This is the oldest ref I can find so I return it as a valid ref to the caller but I'm printing out this warning to tell the user that the oldest point in time I found in the log is effectively the update date as I have no idea when that old sha1 first became the value of the ref.

So I think I'm doing what you are expecting here. The log will start with the value in the ref at the time the log started, not 0{40} and that value is what gets returned when we have this warning come out.. That's the best anyone can expect...

Quoted 4 lines

Also I wonder how much complexity would we suffer and how much efficiency would we gain if we binary search the logdata (the committer info is variable length, so you would need to resync in each step).

I thought about doing this but did not think it would be worth the effort (either developer to code or CPU to execute) at this point in time. I don't think users will be pulling refs from the log very often and if they are they will probably be pulling from recent time, not very far back. Thus starting at the end and walking back is probably "good enough".

But if it proves to be too slow in practice I'm sure I can come up with a faster way to walk through the log. :-)

Signature · 1 line
Shawn.
#3 lore raw
Quoted 14 lines · Shawn Pearce
Quoted 5 lines · Junio C Hamano

Also I wonder how much complexity would we suffer and how much efficiency would we gain if we binary search the logdata (the committer info is variable length, so you would need to resync in each step).

I thought about doing this but did not think it would be worth the effort (either developer to code or CPU to execute) at this point in time. I don't think users will be pulling refs from the log very often and if they are they will probably be pulling from recent time, not very far back. Thus starting at the end and walking back is probably "good enough".

Quoted 2 lines

But if it proves to be too slow in practice I'm sure I can come up with a faster way to walk through the log. :-)

I just ran a test on my PowerBook: walking a 10,000 line log file and extracting the very oldest commit along. Each hit on git-rev-parse seems to took about 100 ms. Hardly worth worrying about for casual use. Further git-rev-parse is taking 73 ms just to run '--verify HEAD' so an extra 30 ms to read the 10k log is pretty much nothing.

[spearce@pb15 trash]$ wc -l .git/logs/refs/heads/master

     10000 .git/logs/refs/heads/master

[spearce@pb15 trash]$ head -n 1 .git/logs/refs/heads/master
b943559a305bdd6bdee2cef6e5df2413c3d30a00 0000000000000000000000000000000000000000 A U Thor <[email protected]> 1136091600 -0500 [spearce@pb15 trash]$ perl -e 'print scalar(localtime shift),"\n"' 1136091600
Sun Jan 1 00:00:00 2006
[spearce@pb15 trash]$ time ../../git-rev-parse --verify HEAD@'300 days'
warning: Log .git/logs/refs/heads/master only goes back to Thu, 1 Jan 1970 00:00:00 +0000. b943559a305bdd6bdee2cef6e5df2413c3d30a00

  real    0m0.112s
  user    0m0.029s
  sys     0m0.023s

[spearce@pb15 trash]$ time ../../git-rev-parse --verify HEAD@'300 days'
warning: Log .git/logs/refs/heads/master only goes back to Thu, 1 Jan 1970 00:00:00 +0000. b943559a305bdd6bdee2cef6e5df2413c3d30a00

  real    0m0.105s
  user    0m0.029s
  sys     0m0.023s
Signature · 1 line
Shawn.
#4 lore raw
In reply to Junio C Hamano
Quoted 4 lines · Junio C Hamano

This does not allow '2006-05-17 00:00:00' as the timespec, and the documentation carefully avoids giving that example, but I think it is better to spell that limitation out.

It doesn't? The "approxidate()" function should handle any reasonable date specifier, and the above is certainly more than reasonable.

Why doesn't approxidate handle it?

Linus

#5 lore raw
Quoted 10 lines · Linus Torvalds
Quoted 4 lines · Junio C Hamano

This does not allow '2006-05-17 00:00:00' as the timespec, and the documentation carefully avoids giving that example, but I think it is better to spell that limitation out.

It doesn't? The "approxidate()" function should handle any reasonable date specifier, and the above is certainly more than reasonable.

Why doesn't approxidate handle it?

The way I read the code is that get_sha1() would first do its magic at the first colon and feeds get_sha1_1() with prefix up to the first colon. This gets passed down to get_sha1_basic() and what approxidate() is fed is the suffix of that prefix. It ends up seeing stuff between '@' and ':'. I.e.

	"master@2006-05-17 00:00:00:cache.h"

would ask for "00:00:cache.h" file in the "master" branch as of timestamp "2006-05-17 00".

#6 lore raw
Quoted 19 lines · Junio C Hamano
Quoted 10 lines · Linus Torvalds
Quoted 4 lines · Junio C Hamano

This does not allow '2006-05-17 00:00:00' as the timespec, and the documentation carefully avoids giving that example, but I think it is better to spell that limitation out.

It doesn't? The "approxidate()" function should handle any reasonable date specifier, and the above is certainly more than reasonable.

Why doesn't approxidate handle it?

The way I read the code is that get_sha1() would first do its magic at the first colon and feeds get_sha1_1() with prefix up to the first colon. This gets passed down to get_sha1_basic() and what approxidate() is fed is the suffix of that prefix. It ends up seeing stuff between '@' and ':'. I.e.

Quoted 4 lines
	"master@2006-05-17 00:00:00:cache.h"

would ask for "00:00:cache.h" file in the "master" branch as of timestamp "2006-05-17 00".

Good catch. I'll see if I can deal with it later; probably early tomorrow morning before work. It may just come down to documenting this particular case as ambiguous and sure to parse the way you did not mean it to. :-)

I tested a bunch of other date formats but not the basic ISO. Argh. I'll send a test case soon for the expression parsing here, to be sure we pull stuff from the log as expected as well as parse the expression in a consistent way between releases. :-)

Signature · 1 line
Shawn.