Skip to content

Commit d5940a5

Browse files
authored
Merge pull request #1697 from vladak/git_date_parsing
Git date parsing
2 parents d3dbeac + f650933 commit d5940a5

File tree

4 files changed

+26
-38
lines changed

4 files changed

+26
-38
lines changed

src/org/opensolaris/opengrok/history/GitRepository.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,11 @@ public class GitRepository extends Repository {
8787
public GitRepository() {
8888
type = "git";
8989
/*
90-
* Formatter which allows the optional day at the beginning as per
91-
* RFC 2822 , section 3.3. Date and Time Specification:
92-
*
93-
* date-time = [ day-of-week "," ] date FWS time [CFWS]
90+
* This should match the 'iso-strict' format used by
91+
* {@code getHistoryLogExecutor}.
9492
*/
95-
datePatterns = new String[]{
96-
"EE, d MMM yyyy HH:mm:ss Z",
97-
"d MMM yyyy HH:mm:ss Z"
93+
datePatterns = new String[] {
94+
"yyyy-MM-dd'T'HH:mm:ssXXX"
9895
};
9996

10097
ignoredDirs.add(".git");
@@ -126,7 +123,7 @@ Executor getHistoryLogExecutor(final File file, String sinceRevision)
126123
cmd.add(ABBREV_LOG);
127124
cmd.add("--name-only");
128125
cmd.add("--pretty=fuller");
129-
cmd.add("--date=rfc");
126+
cmd.add("--date=iso8601-strict");
130127

131128
if (file.isFile() && RuntimeEnvironment.getInstance().isHandleHistoryOfRenamedFiles()) {
132129
cmd.add("--follow");

src/org/opensolaris/opengrok/history/Repository.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,13 @@ boolean supportsSubRepositories() {
417417

418418
public DateFormat getDateFormat() {
419419
return new DateFormat() {
420+
private final Locale locale = Locale.ENGLISH;
420421
private final SimpleDateFormat[] formatters = new SimpleDateFormat[datePatterns.length];
421422

422423
{
423424
// initialize date formatters
424425
for (int i = 0; i < datePatterns.length; i++) {
425-
formatters[i] = new SimpleDateFormat(datePatterns[i]);
426+
formatters[i] = new SimpleDateFormat(datePatterns[i], locale);
426427
/*
427428
* TODO: the following would be nice - but currently it
428429
* could break the compatibility with some repository dates
@@ -454,7 +455,7 @@ public Date parse(String source) throws ParseException {
454455
String.format("%s with format \"%s\" and locale \"%s\"",
455456
ex1.getMessage(),
456457
formatter.toPattern(),
457-
Locale.getDefault().toString()),
458+
locale),
458459
ex1.getErrorOffset()
459460
);
460461
if (head == null || tail == null) {

test/org/opensolaris/opengrok/history/GitHistoryParserTest.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
*/
4242
public class GitHistoryParserTest {
4343

44+
private final String gitISODatePattern = "yyyy-MM-dd'T'HH:mm:ssXXX";
4445
GitHistoryParser instance;
4546
private static TestRepository repository = new TestRepository();
4647

@@ -89,8 +90,8 @@ public void parseALaMemcached() throws Exception {
8990
String commitId3 = "3asdfq234242871934g2sadfsa327894234sa2389";
9091
String author1 = "username <username@asfdsaf-23412-sadf-cxvdsfg3123-sfasdf>";
9192
String author2 = "username2 <username2@as345af-23412-sadf-cxvdsfg3123-sfasdf>";
92-
String date1 = "Sat, 1 Apr 2008 15:12:51 +0000";
93-
String date2 = "Wed, 22 Mar 2006 15:23:15 +0000";
93+
String date1 = "2008-04-01T15:12:51+00:00";
94+
String date2 = "2006-05-22T15:23:15+00:00";
9495
String output =
9596
"commit " + commitId1 + "\n" +
9697
"Author: " + author1 + "\n" +
@@ -133,17 +134,17 @@ public void parseALaMemcached() throws Exception {
133134
HistoryEntry e0 = result.getHistoryEntries().get(0);
134135
assertEquals(commitId1, e0.getRevision());
135136
assertEquals(author1, e0.getAuthor());
136-
assertEquals(new SimpleDateFormat("EE, d MMM yyyy HH:mm:ss Z").parse(date1), e0.getDate());
137+
assertEquals(new SimpleDateFormat(gitISODatePattern).parse(date1), e0.getDate());
137138
assertEquals(0, e0.getFiles().size());
138139
HistoryEntry e1 = result.getHistoryEntries().get(1);
139140
assertEquals(commitId2, e1.getRevision());
140141
assertEquals(author2, e1.getAuthor());
141-
assertEquals(new SimpleDateFormat("EE, d MMM yyyy HH:mm:ss Z").parse(date2), e1.getDate());
142+
assertEquals(new SimpleDateFormat(gitISODatePattern).parse(date2), e1.getDate());
142143
assertEquals(0, e1.getFiles().size());
143144
HistoryEntry e2 = result.getHistoryEntries().get(2);
144145
assertEquals(commitId3, e2.getRevision());
145146
assertEquals(author1, e2.getAuthor());
146-
assertEquals(new SimpleDateFormat("EE, d MMM yyyy HH:mm:ss Z").parse(date1), e2.getDate());
147+
assertEquals(new SimpleDateFormat(gitISODatePattern).parse(date1), e2.getDate());
147148
assertEquals(0, e2.getFiles().size());
148149
}
149150

@@ -158,7 +159,7 @@ public void parseALaGit() throws Exception {
158159
String commitId2 = "2a2323487092314kjsdafsad7829342kjhsdf3289";
159160
String author1 = "username <[email protected]>";
160161
String author2 = "username2 <[email protected]>";
161-
String date1 = "Sun, 13 Jan 2008 01:12:05 -0700";
162+
String date1 = "2008-01-13T01:12:05-07:00";
162163
String filename = "filename.c";
163164

164165
String output = "commit " + commitId1 + "\n" +
@@ -199,15 +200,15 @@ public void parseALaGit() throws Exception {
199200
HistoryEntry e0 = result.getHistoryEntries().get(0);
200201
assertEquals(commitId1, e0.getRevision());
201202
assertEquals(author1, e0.getAuthor());
202-
assertEquals(new SimpleDateFormat("EE, d MMM yyyy HH:mm:ss Z").parse(date1), e0.getDate());
203+
assertEquals(new SimpleDateFormat(gitISODatePattern).parse(date1), e0.getDate());
203204
assertEquals(1, e0.getFiles().size());
204205
assertEquals("/" + filename, e0.getFiles().first());
205206
assertTrue(e0.getMessage().contains("Some heading"));
206207
assertTrue(e0.getMessage().contains("Signed-off-by"));
207208
HistoryEntry e1 = result.getHistoryEntries().get(1);
208209
assertEquals(commitId2, e1.getRevision());
209210
assertEquals(author2, e1.getAuthor());
210-
assertEquals(new SimpleDateFormat("EE, d MMM yyyy HH:mm:ss Z").parse(date1), e1.getDate());
211+
assertEquals(new SimpleDateFormat(gitISODatePattern).parse(date1), e1.getDate());
211212
assertEquals(1, e1.getFiles().size());
212213
assertEquals("/" + filename, e1.getFiles().first());
213214
assertTrue(e1.getMessage().contains("paragraph of text"));
@@ -226,8 +227,8 @@ public void parseALaLK() throws Exception {
226227
String author1 = "username <[email protected]>";
227228
String author2 = "username2 <[email protected]>";
228229
String committer = "committer <[email protected]>";
229-
String date1 = "Sun, 13 Jan 2008 01:12:05 -0700";
230-
String date2 = "Mon, 14 Jan 2008 01:12:05 -0800";
230+
String date1 = "2008-01-13T01:12:05-07:00";
231+
String date2 = "2008-01-14T01:12:05-08:00";
231232
String filename1 = "directory/filename.c";
232233
String filename2 = "directory/filename.h";
233234

@@ -286,15 +287,15 @@ public void parseALaLK() throws Exception {
286287
HistoryEntry e0 = result.getHistoryEntries().get(0);
287288
assertEquals(commitId1, e0.getRevision());
288289
assertEquals(author1, e0.getAuthor());
289-
assertEquals(new SimpleDateFormat("EE, d MMM yyyy HH:mm:ss Z").parse(date1), e0.getDate());
290+
assertEquals(new SimpleDateFormat(gitISODatePattern).parse(date1), e0.getDate());
290291
assertEquals(1, e0.getFiles().size());
291292
assertEquals("/" + filename1, e0.getFiles().first());
292293
assertTrue(e0.getMessage().contains("subject title"));
293294
assertTrue(e0.getMessage().contains("Signed-off-by"));
294295
HistoryEntry e1 = result.getHistoryEntries().get(1);
295296
assertEquals(commitId2, e1.getRevision());
296297
assertEquals(author2, e1.getAuthor());
297-
assertEquals(new SimpleDateFormat("EE, d MMM yyyy HH:mm:ss Z").parse(date1), e1.getDate());
298+
assertEquals(new SimpleDateFormat(gitISODatePattern).parse(date1), e1.getDate());
298299
assertEquals(2, e1.getFiles().size());
299300
assertEquals("/" + filename1, e1.getFiles().first());
300301
assertEquals("/" + filename2, e1.getFiles().last());
@@ -305,10 +306,8 @@ public void parseALaLK() throws Exception {
305306
@Test
306307
public void testDateFormats() {
307308
String[][] dates = new String[][]{
308-
new String[]{"Sat, 1 Apr 2008 15:12:51 +0000", "EE, d MMM yyyy HH:mm:ss Z"},
309-
new String[]{"Sun, 02 Apr 2008 15:12:51 +0730", "EE, d MMM yyyy HH:mm:ss Z"},
310-
new String[]{"1 Apr 2008 15:12:51 +0300", "d MMM yyyy HH:mm:ss Z"},
311-
new String[]{"2 Apr 2008 15:12:51 GMT", "d MMM yyyy HH:mm:ss Z"},};
309+
new String[]{"2017-07-25T13:17:44+02:00", gitISODatePattern},
310+
};
312311

313312
for (int i = 0; i < dates.length; i++) {
314313
try {
@@ -340,9 +339,9 @@ public void testDateFormats() {
340339
assertEquals("The date " + parsedDate + " should be equal to the parsed date " + e0.getDate(), parsedDate, e0.getDate());
341340
assertEquals(0, e0.getFiles().size());
342341
} catch (ParseException ex) {
343-
fail("Should not throw a parse exception");
342+
fail("Should not throw a parse exception" + ex);
344343
} catch (IOException ex) {
345-
fail("Should not throw an IO exception");
344+
fail("Should not throw an IO exception" + ex);
346345
}
347346
}
348347
}

test/org/opensolaris/opengrok/history/GitRepositoryTest.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,7 @@ public void testDateFormats() {
135135
{"abcd", "expected exception"},
136136
{"2016-01-01 10:00:00", "expected exception"},
137137
{"2016 Sat, 5 Apr 2008 15:12:51 +0000", "expected exception"},
138-
{"Sat, 5 Dub 2008 15:12:51 +0000", "expected exception"},
139-
{"Ned, 06 Apr 2008 15:12:51 +0730", "expected exception"},
140-
{"Sat, 1 Apr 2008 15:12:51 +0000", null}, // lenient - wrong date vs. day
141-
{"Sat, 40 Apr 2008 15:12:51 +0000", null}, // lenient - wrong day
142-
{"Sat, 5 Apr 2008 28:12:51 +0000", null}, // lenient - wrong hour
143-
{"Sat, 5 Apr 2008 15:63:51 +0000", null}, // lenient - wrong minute
144-
{"Sat, 5 Apr 2008 15:12:51 +0000", null},
145-
{"Sun, 06 Apr 2008 15:12:51 +0730", null},
146-
{"1 Apr 2008 15:12:51 +0300", null},
147-
{"2 Apr 2008 15:12:51 GMT", null}
138+
{"2017-07-25T13:17:44+02:00", null},
148139
};
149140

150141
DateFormat format = new GitRepository().getDateFormat();

0 commit comments

Comments
 (0)