Skip to content

Commit cce91a2

Browse files
spearceJunio C Hamano
authored and
Junio C Hamano
committed
Change 'master@noon' syntax to 'master@{noon}'.
Its ambiguous to parse "master@2006-05-17 18:30:foo" when foo is meant as a file name and ":30" is meant as 30 minutes past 6 pm. Therefore all date specifications in a sha1 expression must now appear within brackets and the ':' splitter used for the path name in a sha1 expression ignores ':' appearing within brackets. Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0740d9 commit cce91a2

File tree

3 files changed

+62
-31
lines changed

3 files changed

+62
-31
lines changed

Documentation/git-rev-parse.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ syntax.
124124
happen to have both heads/master and tags/master, you can
125125
explicitly say 'heads/master' to tell git which one you mean.
126126

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>).
127+
* A suffix '@' followed by a date specification enclosed in a brace
128+
pair (e.g. '\{yesterday\}', '\{1 month 2 weeks 3 days 1 hour 1
129+
second ago\}' or '\{1979-02-26 18:30:00\}') to specify the value
130+
of the ref at a prior point in time. This suffix may only be
131+
used immediately following a ref name and the ref must have an
132+
existing log ($GIT_DIR/logs/<ref>).
132133

133134
* A suffix '{caret}' to a revision parameter means the first parent of
134135
that commit object. '{caret}<n>' means the <n>th parent (i.e.

sha1_name.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,24 +249,24 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
249249
static const char *warning = "warning: refname '%.*s' is ambiguous.\n";
250250
const char **p, *pathname;
251251
char *real_path = NULL;
252-
int refs_found = 0, at_mark;
252+
int refs_found = 0, am;
253253
unsigned long at_time = (unsigned long)-1;
254254
unsigned char *this_result;
255255
unsigned char sha1_from_ref[20];
256256

257257
if (len == 40 && !get_sha1_hex(str, sha1))
258258
return 0;
259259

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;
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;
264264
char *date_spec = xmalloc(date_len + 1);
265-
strncpy(date_spec, str + at_mark + 1, date_len);
265+
strncpy(date_spec, str + am + 2, date_len);
266266
date_spec[date_len] = 0;
267267
at_time = approxidate(date_spec);
268268
free(date_spec);
269-
len = at_mark;
269+
len = am;
270270
break;
271271
}
272272
}
@@ -482,7 +482,7 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
482482
*/
483483
int get_sha1(const char *name, unsigned char *sha1)
484484
{
485-
int ret;
485+
int ret, bracket_depth;
486486
unsigned unused;
487487
int namelen = strlen(name);
488488
const char *cp;
@@ -528,8 +528,15 @@ int get_sha1(const char *name, unsigned char *sha1)
528528
}
529529
return -1;
530530
}
531-
cp = strchr(name, ':');
532-
if (cp) {
531+
for (cp = name, bracket_depth = 0; *cp; cp++) {
532+
if (*cp == '{')
533+
bracket_depth++;
534+
else if (bracket_depth && *cp == '}')
535+
bracket_depth--;
536+
else if (!bracket_depth && *cp == ':')
537+
break;
538+
}
539+
if (*cp == ':') {
533540
unsigned char tree_sha1[20];
534541
if (!get_sha1_1(name, cp-name, tree_sha1))
535542
return get_tree_entry(tree_sha1, cp+1, sha1,

t/t1400-update-ref.sh

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,52 +125,75 @@ ed="Thu, 26 May 2005 18:32:00 -0500"
125125
gd="Thu, 26 May 2005 18:33:00 -0500"
126126
ld="Thu, 26 May 2005 18:43:00 -0500"
127127
test_expect_success \
128-
'Query "master@May 25 2005" (before history)' \
128+
'Query "master@{May 25 2005}" (before history)' \
129129
'rm -f o e
130-
git-rev-parse --verify "master@May 25 2005" >o 2>e &&
130+
git-rev-parse --verify "master@{May 25 2005}" >o 2>e &&
131131
test $C = $(cat o) &&
132132
test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"'
133133
test_expect_success \
134-
"Query master@2005-05-25 (before history)" \
134+
"Query master@{2005-05-25} (before history)" \
135135
'rm -f o e
136-
git-rev-parse --verify master@2005-05-25 >o 2>e &&
136+
git-rev-parse --verify master@{2005-05-25} >o 2>e &&
137137
test $C = $(cat o) &&
138138
echo test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"'
139139
test_expect_success \
140-
'Query "master@May 26 2005 23:31:59" (1 second before history)' \
140+
'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \
141141
'rm -f o e
142-
git-rev-parse --verify "master@May 26 2005 23:31:59" >o 2>e &&
142+
git-rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e &&
143143
test $C = $(cat o) &&
144144
test "warning: Log .git/logs/$m only goes back to $ed." = "$(cat e)"'
145145
test_expect_success \
146-
'Query "master@May 26 2005 23:32:00" (exactly history start)' \
146+
'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \
147147
'rm -f o e
148-
git-rev-parse --verify "master@May 26 2005 23:32:00" >o 2>e &&
148+
git-rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e &&
149149
test $A = $(cat o) &&
150150
test "" = "$(cat e)"'
151151
test_expect_success \
152-
'Query "master@2005-05-26 23:33:01" (middle of history with gap)' \
152+
'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \
153153
'rm -f o e
154-
git-rev-parse --verify "master@2005-05-26 23:33:01" >o 2>e &&
154+
git-rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e &&
155155
test $B = $(cat o) &&
156156
test "warning: Log .git/logs/$m has gap after $gd." = "$(cat e)"'
157157
test_expect_success \
158-
'Query "master@2005-05-26 23:33:01" (middle of history)' \
158+
'Query "master@{2005-05-26 23:38:00}" (middle of history)' \
159159
'rm -f o e
160-
git-rev-parse --verify "master@2005-05-26 23:38:00" >o 2>e &&
160+
git-rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e &&
161161
test $Z = $(cat o) &&
162162
test "" = "$(cat e)"'
163163
test_expect_success \
164-
'Query "master@2005-05-26 23:43:00" (exact end of history)' \
164+
'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \
165165
'rm -f o e
166-
git-rev-parse --verify "master@2005-05-26 23:43:00" >o 2>e &&
166+
git-rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e &&
167167
test $E = $(cat o) &&
168168
test "" = "$(cat e)"'
169169
test_expect_success \
170-
'Query "master@2005-05-28" (past end of history)' \
170+
'Query "master@{2005-05-28}" (past end of history)' \
171171
'rm -f o e
172-
git-rev-parse --verify "master@2005-05-28" >o 2>e &&
172+
git-rev-parse --verify "master@{2005-05-28}" >o 2>e &&
173173
test $D = $(cat o) &&
174174
test "warning: Log .git/logs/$m unexpectedly ended on $ld." = "$(cat e)"'
175175

176+
177+
rm -f .git/$m .git/logs/$m expect
178+
179+
test_expect_success \
180+
'creating initial files' \
181+
'cp ../../COPYING COPYING &&
182+
git-add COPYING &&
183+
GIT_COMMITTER_DATE="2005-05-26 23:30" git-commit -m add -a &&
184+
cp ../../Makefile COPYING &&
185+
GIT_COMMITTER_DATE="2005-05-26 23:41" git-commit -m change -a'
186+
187+
test_expect_success \
188+
'git-cat-file blob master:COPYING (expect Makefile)' \
189+
'git-cat-file blob master:COPYING | diff - ../../Makefile'
190+
test_expect_success \
191+
'git-cat-file blob master@{2005-05-26 23:30}:COPYING (expect COPYING)' \
192+
'git-cat-file blob "master@{2005-05-26 23:30}:COPYING" \
193+
| diff - ../../COPYING'
194+
test_expect_success \
195+
'git-cat-file blob master@{2005-05-26 23:42}:COPYING (expect Makefile)' \
196+
'git-cat-file blob "master@{2005-05-26 23:42}:COPYING" \
197+
| diff - ../../Makefile'
198+
176199
test_done

0 commit comments

Comments
 (0)