Skip to content

Commit

Permalink
LPS-58330 Add method that retrieves files that are untracked/modified…
Browse files Browse the repository at this point in the history
… in git
  • Loading branch information
hhuijser authored and brianchandotcom committed Sep 2, 2015
1 parent 05c01f6 commit 269b380
Showing 1 changed file with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.tools.ArgumentsUtil;

import java.io.IOException;
import java.io.File;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -189,6 +192,76 @@ public SourceMismatchException getSourceMismatchException() {
return _firstSourceMismatchException;
}

protected static List<String> getLocalChangesFileNames(String baseDirName)
throws Exception {

Runtime runtime = Runtime.getRuntime();

Process process = null;

try {
process = runtime.exec("git add . --dry-run");
}
catch (IOException ioe) {
String errorMessage = ioe.getMessage();

if (errorMessage.contains("Cannot run program")) {
System.out.println(
"Add Git to your PATH system variable before executing " +
"'ant format-source-local-changes'.");

System.exit(0);
}

throw ioe;
}

String content = StringUtil.read(process.getInputStream());

int gitLevel = -1;

for (int i = 0; i < BaseSourceProcessor.PORTAL_MAX_DIR_LEVEL; i++) {
File file = new File(baseDirName + ".git");

if (file.exists()) {
gitLevel = i;

break;
}

baseDirName = "../" + baseDirName;
}

if (gitLevel == -1) {
System.out.println(
"Cannot retrieve files because .git directory is missing.");

System.exit(1);
}

List<String> localChangesFileNames = new ArrayList<>();

for (String line : StringUtil.splitLines(content)) {
if (!line.startsWith("add '") ||
(StringUtil.count(line, StringPool.SLASH) < gitLevel)) {

continue;
}

line = line.substring(5, line.length() - 1);

for (int i = 0; i < gitLevel; i++) {
int x = line.indexOf(StringPool.SLASH);

line = line.substring(x + 1);
}

localChangesFileNames.add(line);
}

return localChangesFileNames;
}

private void _runSourceProcessor(SourceProcessor sourceProcessor)
throws Exception {

Expand Down

0 comments on commit 269b380

Please sign in to comment.