Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Supporting older git < 2.6 which does not have --date:format option #1314

Merged
merged 1 commit into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/org/opensolaris/opengrok/history/GitRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
import org.opensolaris.opengrok.logger.LoggerFactory;
import org.opensolaris.opengrok.util.Executor;
import org.opensolaris.opengrok.util.StringUtils;

/**
* Access to a Git repository.
Expand Down Expand Up @@ -663,24 +664,38 @@ String determineBranch() throws IOException {
return branch;
}

private static final SimpleDateFormat outputDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm");

@Override
String determineCurrentVersion() throws IOException {
String line = null;
File directory = new File(directoryName);

List<String> cmd = new ArrayList<>();
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
cmd.add(RepoCommand);
cmd.add("log");
cmd.add("-1");
cmd.add("--pretty=%cd: %h %an %s");
cmd.add("--date=format:%Y-%m-%d %H:%M");
cmd.add("--date=rfc");

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

return executor.getOutputString().trim();
String output = executor.getOutputString().trim();
int indexOf = StringUtils.nthIndexOf(output, ":", 3);
if (indexOf < 0) {
throw new IOException(
String.format("Couldn't extract date from \"%s\".",
new Object[]{output}));
}

try {
Date date = getDateFormat().parse(output.substring(0, indexOf));
return String.format("%s%s",
new Object[]{outputDateFormat.format(date), output.substring(indexOf)});
} catch (ParseException ex) {
throw new IOException(ex);
}
}
}
26 changes: 25 additions & 1 deletion src/org/opensolaris/opengrok/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
*/

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

return (output.length() == 0 ? "0" : output);
}

/**
* Finds n-th index of a given substring in a string.
*
* @param str an original string
* @param substr a substring to match
* @param n n-th occurrence
* @return the index of the first character of the substring in the original
* string where the substring occurred n-th times in the string. If the n-th
* candidate does not exist, -1 is returned.
*/
public static int nthIndexOf(String str, String substr, int n) {
int pos = -1;
while (n > 0) {
if (pos >= str.length()) {
return -1;
}
if ((pos = str.indexOf(substr, pos + 1)) == -1) {
break;
}
n--;
}
return pos;
}
}
63 changes: 61 additions & 2 deletions test/org/opensolaris/opengrok/util/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
* CDDL HEADER END
*/

/*
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.util;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
Expand All @@ -49,4 +50,62 @@ public void testValues() {
assertEquals(expected[i], StringUtils.getReadableTime(values[i]));
}
}

@Test
public void testNthIndexOf() {
Object[][] tests = new Object[][]{
{"", "", -1},
{"", "", 0},
{"", "", 1},
{"", "", 2},
{"foo", "foo", 0},
{"foo", "foo", 1},
{"foo", "foo", 2},
{"foo", "foo", 3},
{"foo", "f", 0},
{"foo", "f", 1},
{"foo", "f", 2},
{"foo", "f", 3},
{"foo", "o", 0},
{"foo", "o", 1},
{"foo", "o", 2},
{"foo", "o", 3},
{"This is an example string", "a", 2},
{"This is an example string", "a", 3},
{"This is an example string", "i", 1},
{"This is an example string", "i", 2},
{"This is an example string", "i", 3},
{"This is an example string", "is", 1},
{"This is an example string", "is", 2},
{"aabbccddaabbccdd", "a", 1},
{"aabbccddaabbccdd", "a", 2},
{"aabbccddaabbccdd", "a", 3},
{"aabbccddaabbccdd", "a", 4},
{"aabbccddaabbccdd", "cd", 1},
{"aabbccddaabbccdd", "cd", 2},
{"aabbccddaabbccdd", "ccdd", 1},
{"aabbccddaabbccdd", "ccdd", 2},};

int[] indices = new int[]{
-1, -1, 0, -1,
-1, 0, -1, -1,
-1, 0, -1, -1,
-1, 1, 2, -1,
13, -1,
2, 5, 22,
2, 5,
0, 1, 8, 9,
5, 13,
4, 12
};

assertEquals(tests.length, indices.length);

for (int i = 0; i < tests.length; i++) {
int index = StringUtils.nthIndexOf((String) tests[i][0], (String) tests[i][1], (Integer) tests[i][2]);
assertEquals(String.format("%d-th occurrence of \"%s\" in \"%s\" should start at %d but started at %d",
new Object[]{tests[i][2], tests[i][1], tests[i][0], indices[i], index}),
index, indices[i]);
}
}
}