forked from liferay/liferay-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LPS-58330 Move git logic to new class
- Loading branch information
1 parent
27bbcea
commit 14aa0c8
Showing
2 changed files
with
104 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
modules/util/source-formatter/src/com/liferay/source/formatter/util/GitUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) 2000-present Liferay, Inc. All rights reserved. | ||
* | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation; either version 2.1 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
*/ | ||
|
||
package com.liferay.source.formatter.util; | ||
|
||
import com.liferay.portal.kernel.util.StringPool; | ||
import com.liferay.portal.kernel.util.StringUtil; | ||
import com.liferay.source.formatter.BaseSourceProcessor; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Hugo Huijser | ||
*/ | ||
public class GitUtil { | ||
|
||
public 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; | ||
} | ||
|
||
} |