From 14aa0c8c881f71db79c520b91dc74d3be4325f3f Mon Sep 17 00:00:00 2001 From: Hugo Huijser Date: Fri, 4 Sep 2015 10:03:21 +0800 Subject: [PATCH] LPS-58330 Move git logic to new class --- .../source/formatter/SourceFormatter.java | 76 +------------ .../source/formatter/util/GitUtil.java | 102 ++++++++++++++++++ 2 files changed, 104 insertions(+), 74 deletions(-) create mode 100644 modules/util/source-formatter/src/com/liferay/source/formatter/util/GitUtil.java diff --git a/modules/util/source-formatter/src/com/liferay/source/formatter/SourceFormatter.java b/modules/util/source-formatter/src/com/liferay/source/formatter/SourceFormatter.java index 523b5e736660c7..4dbcb0f65369f6 100644 --- a/modules/util/source-formatter/src/com/liferay/source/formatter/SourceFormatter.java +++ b/modules/util/source-formatter/src/com/liferay/source/formatter/SourceFormatter.java @@ -19,9 +19,7 @@ import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.portal.tools.ArgumentsUtil; - -import java.io.File; -import java.io.IOException; +import com.liferay.source.formatter.util.GitUtil; import java.util.ArrayList; import java.util.Arrays; @@ -66,7 +64,7 @@ public static void main(String[] args) throws Exception { if (formatLocalChanges) { sourceFormatterArgs.setLocalChangesFileNames( - getLocalChangesFileNames(baseDirName)); + GitUtil.getLocalChangesFileNames(baseDirName)); } String copyrightFileName = GetterUtil.getString( @@ -203,76 +201,6 @@ public SourceMismatchException getSourceMismatchException() { return _firstSourceMismatchException; } - protected static List 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 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 { diff --git a/modules/util/source-formatter/src/com/liferay/source/formatter/util/GitUtil.java b/modules/util/source-formatter/src/com/liferay/source/formatter/util/GitUtil.java new file mode 100644 index 00000000000000..eca1ca8a250d17 --- /dev/null +++ b/modules/util/source-formatter/src/com/liferay/source/formatter/util/GitUtil.java @@ -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 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 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; + } + +} \ No newline at end of file