Skip to content

Commit 29068b6

Browse files
authored
Merge pull request #1314 from tulinkry/git-version
Supporting older git < 2.6 which does not have --date:format option
2 parents adfc516 + bdb4975 commit 29068b6

File tree

3 files changed

+105
-7
lines changed

3 files changed

+105
-7
lines changed

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
4949
import org.opensolaris.opengrok.logger.LoggerFactory;
5050
import org.opensolaris.opengrok.util.Executor;
51+
import org.opensolaris.opengrok.util.StringUtils;
5152

5253
/**
5354
* Access to a Git repository.
@@ -663,24 +664,38 @@ String determineBranch() throws IOException {
663664
return branch;
664665
}
665666

667+
private static final SimpleDateFormat outputDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm");
668+
666669
@Override
667670
String determineCurrentVersion() throws IOException {
668-
String line = null;
669671
File directory = new File(directoryName);
670-
671672
List<String> cmd = new ArrayList<>();
672673
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
673674
cmd.add(RepoCommand);
674675
cmd.add("log");
675676
cmd.add("-1");
676677
cmd.add("--pretty=%cd: %h %an %s");
677-
cmd.add("--date=format:%Y-%m-%d %H:%M");
678+
cmd.add("--date=rfc");
678679

679680
Executor executor = new Executor(cmd, directory);
680681
if (executor.exec(false) != 0) {
681682
throw new IOException(executor.getErrorString());
682683
}
683684

684-
return executor.getOutputString().trim();
685+
String output = executor.getOutputString().trim();
686+
int indexOf = StringUtils.nthIndexOf(output, ":", 3);
687+
if (indexOf < 0) {
688+
throw new IOException(
689+
String.format("Couldn't extract date from \"%s\".",
690+
new Object[]{output}));
691+
}
692+
693+
try {
694+
Date date = getDateFormat().parse(output.substring(0, indexOf));
695+
return String.format("%s%s",
696+
new Object[]{outputDateFormat.format(date), output.substring(indexOf)});
697+
} catch (ParseException ex) {
698+
throw new IOException(ex);
699+
}
685700
}
686701
}

src/org/opensolaris/opengrok/util/StringUtils.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323

2424
package org.opensolaris.opengrok.util;
@@ -107,4 +107,28 @@ public static String getReadableTime(long time_ms) {
107107

108108
return (output.length() == 0 ? "0" : output);
109109
}
110+
111+
/**
112+
* Finds n-th index of a given substring in a string.
113+
*
114+
* @param str an original string
115+
* @param substr a substring to match
116+
* @param n n-th occurrence
117+
* @return the index of the first character of the substring in the original
118+
* string where the substring occurred n-th times in the string. If the n-th
119+
* candidate does not exist, -1 is returned.
120+
*/
121+
public static int nthIndexOf(String str, String substr, int n) {
122+
int pos = -1;
123+
while (n > 0) {
124+
if (pos >= str.length()) {
125+
return -1;
126+
}
127+
if ((pos = str.indexOf(substr, pos + 1)) == -1) {
128+
break;
129+
}
130+
n--;
131+
}
132+
return pos;
133+
}
110134
}

test/org/opensolaris/opengrok/util/StringUtilsTest.java

+61-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
* CDDL HEADER END
1818
*/
1919

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

2525
import org.junit.Test;
26+
2627
import static org.junit.Assert.assertEquals;
2728

2829
/**
@@ -49,4 +50,62 @@ public void testValues() {
4950
assertEquals(expected[i], StringUtils.getReadableTime(values[i]));
5051
}
5152
}
53+
54+
@Test
55+
public void testNthIndexOf() {
56+
Object[][] tests = new Object[][]{
57+
{"", "", -1},
58+
{"", "", 0},
59+
{"", "", 1},
60+
{"", "", 2},
61+
{"foo", "foo", 0},
62+
{"foo", "foo", 1},
63+
{"foo", "foo", 2},
64+
{"foo", "foo", 3},
65+
{"foo", "f", 0},
66+
{"foo", "f", 1},
67+
{"foo", "f", 2},
68+
{"foo", "f", 3},
69+
{"foo", "o", 0},
70+
{"foo", "o", 1},
71+
{"foo", "o", 2},
72+
{"foo", "o", 3},
73+
{"This is an example string", "a", 2},
74+
{"This is an example string", "a", 3},
75+
{"This is an example string", "i", 1},
76+
{"This is an example string", "i", 2},
77+
{"This is an example string", "i", 3},
78+
{"This is an example string", "is", 1},
79+
{"This is an example string", "is", 2},
80+
{"aabbccddaabbccdd", "a", 1},
81+
{"aabbccddaabbccdd", "a", 2},
82+
{"aabbccddaabbccdd", "a", 3},
83+
{"aabbccddaabbccdd", "a", 4},
84+
{"aabbccddaabbccdd", "cd", 1},
85+
{"aabbccddaabbccdd", "cd", 2},
86+
{"aabbccddaabbccdd", "ccdd", 1},
87+
{"aabbccddaabbccdd", "ccdd", 2},};
88+
89+
int[] indices = new int[]{
90+
-1, -1, 0, -1,
91+
-1, 0, -1, -1,
92+
-1, 0, -1, -1,
93+
-1, 1, 2, -1,
94+
13, -1,
95+
2, 5, 22,
96+
2, 5,
97+
0, 1, 8, 9,
98+
5, 13,
99+
4, 12
100+
};
101+
102+
assertEquals(tests.length, indices.length);
103+
104+
for (int i = 0; i < tests.length; i++) {
105+
int index = StringUtils.nthIndexOf((String) tests[i][0], (String) tests[i][1], (Integer) tests[i][2]);
106+
assertEquals(String.format("%d-th occurrence of \"%s\" in \"%s\" should start at %d but started at %d",
107+
new Object[]{tests[i][2], tests[i][1], tests[i][0], indices[i], index}),
108+
index, indices[i]);
109+
}
110+
}
52111
}

0 commit comments

Comments
 (0)