Skip to content

Commit d2db363

Browse files
author
Vladimir Kotal
authored
Merge pull request #1336 from tulinkry/dedup
deduplication of date formats in repositories
2 parents 0675d08 + 945bcd9 commit d2db363

16 files changed

+431
-136
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

@@ -83,7 +83,9 @@ public class AccuRevRepository extends Repository {
8383

8484
public AccuRevRepository() {
8585
type = "AccuRev";
86-
datePattern = "yyyy/MM/dd hh:mm:ss";
86+
datePatterns = new String[]{
87+
"yyyy/MM/dd hh:mm:ss"
88+
};
8789
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
8890
}
8991

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

@@ -62,7 +62,9 @@ public class BazaarRepository extends Repository {
6262

6363
public BazaarRepository() {
6464
type = "Bazaar";
65-
datePattern = "EEE yyyy-MM-dd hh:mm:ss ZZZZ";
65+
datePatterns = new String[]{
66+
"EEE yyyy-MM-dd hh:mm:ss ZZZZ"
67+
};
6668
}
6769

6870
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

@@ -69,7 +69,9 @@ public class CVSRepository extends RCSRepository {
6969
public CVSRepository() {
7070
working = Boolean.FALSE;
7171
setType("CVS");
72-
setDatePattern("yyyy-MM-dd hh:mm:ss");
72+
datePatterns = new String[]{
73+
"yyyy-MM-dd hh:mm:ss"
74+
};
7375
}
7476

7577
@Override

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

@@ -35,7 +35,6 @@
3535
import java.util.logging.Level;
3636
import java.util.logging.Logger;
3737
import java.util.regex.Pattern;
38-
3938
import org.opensolaris.opengrok.logger.LoggerFactory;
4039
import org.opensolaris.opengrok.util.Executor;
4140
import org.opensolaris.opengrok.util.IOUtils;
@@ -63,7 +62,9 @@ public class ClearCaseRepository extends Repository {
6362

6463
public ClearCaseRepository() {
6564
type = "ClearCase";
66-
datePattern = "yyyyMMdd.HHmmss";
65+
datePatterns = new String[]{
66+
"yyyyMMdd.HHmmss"
67+
};
6768
}
6869

6970
/**

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

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,12 @@
3030
import java.io.InputStream;
3131
import java.io.InputStreamReader;
3232
import java.io.Reader;
33-
import java.text.DateFormat;
34-
import java.text.FieldPosition;
3533
import java.text.ParseException;
36-
import java.text.ParsePosition;
3734
import java.text.SimpleDateFormat;
3835
import java.util.ArrayList;
3936
import java.util.Date;
4037
import java.util.LinkedList;
4138
import java.util.List;
42-
import java.util.Locale;
4339
import java.util.TreeSet;
4440
import java.util.logging.Level;
4541
import java.util.logging.Logger;
@@ -81,8 +77,6 @@ public class GitRepository extends Repository {
8177
private static final String ABBREV_LOG = "--abbrev=" + CSET_LEN;
8278
private static final String ABBREV_BLAME = "--abbrev=" + (CSET_LEN - 1);
8379

84-
private static final String[] backupDatePatterns = new String[]{"d MMM yyyy HH:mm:ss Z"};
85-
8680
/**
8781
* Pattern used to extract author/revision from git blame.
8882
*/
@@ -91,7 +85,16 @@ public class GitRepository extends Repository {
9185

9286
public GitRepository() {
9387
type = "git";
94-
datePattern = "EE, d MMM yyyy HH:mm:ss Z";
88+
/*
89+
* Formatter which allows the optional day at the beginning as per
90+
* RFC 2822 , section 3.3. Date and Time Specification:
91+
*
92+
* date-time = [ day-of-week "," ] date FWS time [CFWS]
93+
*/
94+
datePatterns = new String[]{
95+
"EE, d MMM yyyy HH:mm:ss Z",
96+
"d MMM yyyy HH:mm:ss Z"
97+
};
9598
}
9699

97100
/**
@@ -178,54 +181,6 @@ Executor getHistoryLogExecutor(final File file, String sinceRevision)
178181
return new Executor(cmd, new File(getDirectoryName()), sinceRevision != null);
179182
}
180183

181-
/**
182-
* Formatter for which allows the optional day at the beginning as per
183-
* RFC 2822 , section 3.3. Date and Time Specification:
184-
*
185-
* date-time = [ day-of-week "," ] date FWS time [CFWS]
186-
*
187-
* @return DateFormat which accepts the optional day format
188-
*/
189-
@Override
190-
public DateFormat getDateFormat() {
191-
return new DateFormat() {
192-
193-
private DateFormat formatter = new SimpleDateFormat(datePattern, Locale.getDefault());
194-
private DateFormat[] backupFormatters = new DateFormat[backupDatePatterns.length];
195-
196-
{
197-
for (int i = 0; i < backupDatePatterns.length; i++) {
198-
backupFormatters[i] = new SimpleDateFormat(backupDatePatterns[i], Locale.getDefault());
199-
}
200-
}
201-
202-
@Override
203-
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
204-
return formatter.format(date, toAppendTo, fieldPosition);
205-
}
206-
207-
@Override
208-
public Date parse(String source) throws ParseException {
209-
try {
210-
return formatter.parse(source);
211-
} catch (ParseException ex) {
212-
for (int i = 0; i < backupFormatters.length; i++) {
213-
try {
214-
return backupFormatters[i].parse(source);
215-
} catch (ParseException ex1) {
216-
}
217-
}
218-
throw ex;
219-
}
220-
}
221-
222-
@Override
223-
public Date parse(String source, ParsePosition pos) {
224-
return formatter.parse(source, pos);
225-
}
226-
};
227-
}
228-
229184
/**
230185
* Create a {@code Reader} that reads an {@code InputStream} using the
231186
* correct character encoding.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

@@ -114,7 +114,9 @@ public class MercurialRepository extends Repository {
114114

115115
public MercurialRepository() {
116116
type = "Mercurial";
117-
datePattern = "yyyy-MM-dd hh:mm ZZZZ";
117+
datePatterns = new String[]{
118+
"yyyy-MM-dd hh:mm ZZZZ"
119+
};
118120
}
119121

120122
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

@@ -61,7 +61,9 @@ public class MonotoneRepository extends Repository {
6161

6262
public MonotoneRepository() {
6363
type = "Monotone";
64-
datePattern = "yyyy-MM-dd'T'hh:mm:ss";
64+
datePatterns = new String[]{
65+
"yyyy-MM-dd'T'hh:mm:ss"
66+
};
6567
}
6668

6769
@Override

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
/* Portions Copyright 2008 Peter Bray */
2424
package org.opensolaris.opengrok.history;
@@ -31,7 +31,6 @@
3131
import java.util.logging.Level;
3232
import java.util.logging.Logger;
3333
import java.util.zip.GZIPInputStream;
34-
3534
import org.opensolaris.opengrok.logger.LoggerFactory;
3635

3736
/**
@@ -148,7 +147,9 @@ public class RazorRepository extends Repository {
148147
public RazorRepository() {
149148
type = "Razor";
150149
working = Boolean.TRUE;
151-
datePattern = "yyyy/MM/dd,hh:mm:ss";
150+
datePatterns = new String[]{
151+
"yyyy/MM/dd,hh:mm:ss"
152+
};
152153
}
153154

154155
@Override

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

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

2525
import java.io.File;
2626
import java.io.IOException;
2727
import java.io.InputStream;
2828
import java.text.DateFormat;
29+
import java.text.FieldPosition;
30+
import java.text.ParseException;
31+
import java.text.ParsePosition;
2932
import java.text.SimpleDateFormat;
3033
import java.util.ArrayList;
34+
import java.util.Date;
3135
import java.util.Iterator;
3236
import java.util.List;
3337
import java.util.Locale;
@@ -388,7 +392,69 @@ boolean supportsSubRepositories() {
388392
}
389393

390394
public DateFormat getDateFormat() {
391-
return new SimpleDateFormat(datePattern, Locale.US);
395+
return new DateFormat() {
396+
private final SimpleDateFormat[] formatters = new SimpleDateFormat[datePatterns.length];
397+
398+
{
399+
// initialize date formatters
400+
for (int i = 0; i < datePatterns.length; i++) {
401+
formatters[i] = new SimpleDateFormat(datePatterns[i]);
402+
/*
403+
* TODO: the following would be nice - but currently it
404+
* could break the compatibility with some repository dates
405+
*/
406+
// formatters[i].setLenient(false);
407+
}
408+
}
409+
410+
@Override
411+
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
412+
for (DateFormat formatter : formatters) {
413+
return formatter.format(date, toAppendTo, fieldPosition);
414+
}
415+
return toAppendTo.append("(date null)");
416+
}
417+
418+
@Override
419+
public Date parse(String source) throws ParseException {
420+
ParseException head = null, tail = null;
421+
for (SimpleDateFormat formatter : formatters) {
422+
try {
423+
return formatter.parse(source);
424+
} catch (ParseException ex1) {
425+
/*
426+
* Adding all exceptions together to get some info in
427+
* the logs.
428+
*/
429+
ex1 = new ParseException(
430+
String.format("%s with format \"%s\" and locale \"%s\"",
431+
ex1.getMessage(),
432+
formatter.toPattern(),
433+
Locale.getDefault().toString()),
434+
ex1.getErrorOffset()
435+
);
436+
if (head == null || tail == null) {
437+
head = tail = ex1;
438+
} else {
439+
tail.initCause(ex1);
440+
tail = ex1;
441+
}
442+
}
443+
}
444+
throw head != null ? head : new ParseException(String.format("Unparseable date: \"%s\"", source), 0);
445+
}
446+
447+
@Override
448+
public Date parse(String source, ParsePosition pos) {
449+
Date d = null;
450+
for (DateFormat formatter : formatters) {
451+
if ((d = formatter.parse(source, pos)) != null) {
452+
return d;
453+
}
454+
}
455+
return d;
456+
}
457+
};
392458
}
393459

394460
static Boolean checkCmd(String... args) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

@@ -39,7 +39,7 @@ public class RepositoryInfo implements Serializable {
3939
protected Boolean working;
4040
protected String type;
4141
protected boolean remote;
42-
protected String datePattern;
42+
protected String[] datePatterns = new String[0];
4343
protected String parent;
4444
protected String branch;
4545
protected String currentVersion;
@@ -56,7 +56,7 @@ public RepositoryInfo(RepositoryInfo orig) {
5656
this.type = orig.type;
5757
this.working = orig.isWorking();
5858
this.remote = orig.isRemote();
59-
this.datePattern = orig.datePattern;
59+
this.datePatterns = orig.datePatterns;
6060
this.parent = orig.parent;
6161
this.branch = orig.branch;
6262
this.currentVersion = orig.currentVersion;
@@ -154,12 +154,12 @@ public void setParent(String parent) {
154154
this.parent = parent;
155155
}
156156

157-
public void setDatePattern(String datePattern) {
158-
this.datePattern = datePattern;
157+
public void setDatePatterns(String[] datePatterns) {
158+
this.datePatterns = datePatterns;
159159
}
160160

161-
public String getDatePattern() {
162-
return datePattern;
161+
public String[] getDatePatterns() {
162+
return datePatterns;
163163
}
164164

165165
public String getBranch() {

0 commit comments

Comments
 (0)