Skip to content

The Unified and Autonomous Development Platform

Add Git.isTreeLink to determine if a symbol link points to a directory.

robin shinec46753012124
diff --git a/commons.bootstrap/src/main/java/com/pmease/commons/bootstrap/BootstrapUtils.java b/commons.bootstrap/src/main/java/com/pmease/commons/bootstrap/BootstrapUtils.java
index f479e7455a..65f0e4029b 100644
--- a/commons.bootstrap/src/main/java/com/pmease/commons/bootstrap/BootstrapUtils.java
+++ b/commons.bootstrap/src/main/java/com/pmease/commons/bootstrap/BootstrapUtils.java
@@ -6,12 +6,15 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class BootstrapUtils {
+ private static final int BUFFER_SIZE = 64*1024;
+
public static Object readObject(File file) {
ObjectInputStream ois = null;
try {
@@ -48,8 +51,22 @@ public class BootstrapUtils {
else
throw new RuntimeException(e);
}
+
+ public static void createDir(File dir) {
+ if (dir.exists()) {
+ if (dir.isFile()) {
+ throw new RuntimeException("Unable to create directory since the path " +
+ "is already used by a file: " + dir.getAbsolutePath());
+ }
+ } else if (!dir.mkdirs()) {
+ if (!dir.exists())
+ throw new RuntimeException("Unable to create directory: " + dir.getAbsolutePath());
+ }
+ }
/**
+ * Unzip files matching specified matcher from specified file to specified directory.
+ *
* @param srcFile
* zip file to extract from
* @param
@@ -59,42 +76,48 @@ public class BootstrapUtils {
* Use null if you want to extract all entries from the zip file.
*/
public static void unzip(File srcFile, File destDir, StringMatcher matcher) {
- int BUFFER = 8192;
-
- ZipInputStream zis = null;
+ try (InputStream is = new FileInputStream(srcFile);) {
+ unzip(is, destDir, matcher);
+ } catch (Exception e) {
+ throw unchecked(e);
+ }
+ }
+
+ /**
+ * Unzip files matching specified matcher from input stream to specified directory.
+ *
+ * @param is
+ * input stream to unzip files from. This method will not close the stream
+ * after using it. Caller should be responsible for closing
+ * @param destDir
+ * destination directory to extract files to
+ * @param matcher
+ * matcher to select matched files for extracting
+ */
+ public static void unzip(InputStream is, File destDir, StringMatcher matcher) {
+ ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is, BUFFER_SIZE));
try {
- zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(srcFile)));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
if (matcher == null || matcher.matches(entry.getName())) {
- BufferedOutputStream bos = null;
- try {
- bos = new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName())));
-
- int count;
- byte data[] = new byte[BUFFER];
- while ((count = zis.read(data, 0, BUFFER)) != -1)
- bos.write(data, 0, count);
- bos.flush();
- } finally {
- if (bos != null)
- bos.close();
- }
+ if (entry.getName().endsWith("/")) {
+ createDir(new File(destDir, entry.getName()));
+ } else {
+ try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName())), BUFFER_SIZE);) {
+ int count;
+ byte data[] = new byte[BUFFER_SIZE];
+ while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1)
+ bos.write(data, 0, count);
+ bos.flush();
+ }
+ }
}
}
} catch (Exception e) {
throw unchecked(e);
- } finally {
- if (zis != null) {
- try {
- zis.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
}
}
-
+
public static boolean visitDir(File dir, FileVisitor visitor) {
if (!dir.exists())
throw new RuntimeException("Directory '" + dir.getAbsolutePath() + "' does not exist.");
diff --git a/commons.git/src/main/java/com/pmease/commons/git/Git.java b/commons.git/src/main/java/com/pmease/commons/git/Git.java
index 7b444f0db2..3b4befa40c 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/Git.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/Git.java
@@ -23,6 +23,7 @@ import com.pmease.commons.git.command.FetchCommand;
import com.pmease.commons.git.command.InitCommand;
import com.pmease.commons.git.command.IsAncestorCommand;
import com.pmease.commons.git.command.IsBinaryCommand;
+import com.pmease.commons.git.command.IsTreeLinkCommand;
import com.pmease.commons.git.command.ListBranchesCommand;
import com.pmease.commons.git.command.ListChangedFilesCommand;
import com.pmease.commons.git.command.ListSubModulesCommand;
@@ -289,6 +290,10 @@ public class Git implements Serializable {
return new IsBinaryCommand(repoDir).file(file).revision(revision).call();
}
+ public boolean isTreeLink(String symlink, String revision) {
+ return new IsTreeLinkCommand(repoDir).symlink(symlink).revision(revision).call();
+ }
+
public Git addSubModule(String url, String path) {
new AddSubModuleCommand(repoDir).url(url).path(path).call();
return this;
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/AddCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/AddCommand.java
index 371b9a4209..4863fa98a8 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/AddCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/AddCommand.java
@@ -28,7 +28,7 @@ public class AddCommand extends GitCommand<Void> {
cmd.addArgs(path);
}
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/AddNoteCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/AddNoteCommand.java
index c820374f79..2d496d6953 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/AddNoteCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/AddNoteCommand.java
@@ -31,7 +31,7 @@ public class AddNoteCommand extends GitCommand<Void> {
Preconditions.checkNotNull(message, "message should be specified.");
Commandline cmd = cmd().addArgs("notes", "add", "-m", message, object);
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/AddSubModuleCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/AddSubModuleCommand.java
index 3cdb842de0..605a32d639 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/AddSubModuleCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/AddSubModuleCommand.java
@@ -31,7 +31,7 @@ public class AddSubModuleCommand extends GitCommand<Void> {
Preconditions.checkNotNull(path, "path should be specified.");
Commandline cmd = cmd().addArgs("submodule", "add", url, path);
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/BlameCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/BlameCommand.java
index 42edfa0b18..35f73f103b 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/BlameCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/BlameCommand.java
@@ -86,7 +86,7 @@ public class BlameCommand extends GitCommand<List<Blame>> {
}
}
- }, errorLogger()).checkReturnCode();
+ }, errorLogger).checkReturnCode();
if (!blameBuilder.lines.isEmpty())
blames.add(blameBuilder.build());
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/CalcMergeBaseCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/CalcMergeBaseCommand.java
index baf6d1351f..9b00ffb76a 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/CalcMergeBaseCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/CalcMergeBaseCommand.java
@@ -51,7 +51,7 @@ public class CalcMergeBaseCommand extends GitCommand<String> {
@Override
public void consume(String line) {
- LineConsumer.logger.error(line);
+ error(line);
}
}).checkReturnCode();
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/CheckoutCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/CheckoutCommand.java
index 74957ceeec..470decd2f4 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/CheckoutCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/CheckoutCommand.java
@@ -44,7 +44,7 @@ public class CheckoutCommand extends GitCommand<Void> {
cmd.addArgs(revision);
- cmd.execute(debugLogger(), new LineConsumer() {
+ cmd.execute(debugLogger, new LineConsumer() {
@Override
public void consume(String line) {
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/CloneCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/CloneCommand.java
index aaaf3b07df..50c526d20e 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/CloneCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/CloneCommand.java
@@ -63,7 +63,7 @@ public class CloneCommand extends GitCommand<Void> {
cmd.addArgs(from);
cmd.addArgs(".");
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/CommitCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/CommitCommand.java
index bc9624d491..dddd1f08f3 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/CommitCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/CommitCommand.java
@@ -44,7 +44,7 @@ public class CommitCommand extends GitCommand<Void> {
if (amend)
cmd.addArgs("--amend");
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/DeleteRefCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/DeleteRefCommand.java
index 53201c64da..5e3aeab129 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/DeleteRefCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/DeleteRefCommand.java
@@ -43,7 +43,7 @@ public class DeleteRefCommand extends GitCommand<Void> {
if (reason != null)
cmd.addArgs(reason);
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/DiffCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/DiffCommand.java
index 998c6a26f0..b578253606 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/DiffCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/DiffCommand.java
@@ -111,7 +111,7 @@ public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
}
- }, errorLogger()).checkReturnCode();
+ }, errorLogger).checkReturnCode();
if (changeBuilder.newPath != null)
fileChanges.add(changeBuilder.buildFileChange());
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/FetchCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/FetchCommand.java
index aa63bf0c91..bd8867b167 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/FetchCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/FetchCommand.java
@@ -33,7 +33,7 @@ public class FetchCommand extends GitCommand<Void> {
Commandline cmd = cmd().addArgs("fetch");
cmd.addArgs(from, refspec);
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.java
index bb6306cee5..18f8405e92 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/GitCommand.java
@@ -7,6 +7,8 @@ import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import com.pmease.commons.git.GitVersion;
import com.pmease.commons.util.FileUtils;
@@ -15,23 +17,60 @@ import com.pmease.commons.util.execution.LineConsumer;
public abstract class GitCommand<V> implements Callable<V> {
+ protected static final Logger logger = LoggerFactory.getLogger(GitCommand.class);
+
private static final String GIT_EXE = "git";
- private static final String MIN_VERSION = "1.0.0";
+ private static final String MIN_VERSION = "1.8.0";
protected final File repoDir;
private final Map<String, String> environments;
- private final LineConsumer debugLogger = new LineConsumer.DebugLogger();
+ protected static final LineConsumer debugLogger = new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.debug(line);
+ }
+
+ };
- private final LineConsumer infoLogger = new LineConsumer.InfoLogger();
+ protected static final LineConsumer infoLogger = new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.info(line);
+ }
+
+ };
- private final LineConsumer warnLogger = new LineConsumer.WarnLogger();
+ protected static final LineConsumer warnLogger = new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.warn(line);
+ }
+
+ };
- private final LineConsumer errorLogger = new LineConsumer.ErrorLogger();
+ protected static final LineConsumer errorLogger = new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.error(line);
+ }
+
+ };
- private final LineConsumer traceLogger = new LineConsumer.TraceLogger();
+ protected static final LineConsumer traceLogger = new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.trace(line);
+ }
+
+ };
public GitCommand(File repoDir, @Nullable Map<String, String> environments) {
this.repoDir = repoDir;
@@ -82,26 +121,6 @@ public abstract class GitCommand<V> implements Callable<V> {
}
}
- protected LineConsumer debugLogger() {
- return debugLogger;
- }
-
- protected LineConsumer infoLogger() {
- return infoLogger;
- }
-
- protected LineConsumer warnLogger() {
- return warnLogger;
- }
-
- protected LineConsumer errorLogger() {
- return errorLogger;
- }
-
- protected LineConsumer traceLogger() {
- return traceLogger;
- }
-
public Commandline cmd() {
Commandline cmd = new Commandline(GIT_EXE).workingDir(repoDir);
if (environments != null)
@@ -111,4 +130,25 @@ public abstract class GitCommand<V> implements Callable<V> {
@Override
public abstract V call();
+
+ protected void debug(String line) {
+ logger.debug(line);
+ }
+
+ protected void trace(String line) {
+ logger.trace(line);
+ }
+
+ protected void info(String line) {
+ logger.info(line);
+ }
+
+ protected void warn(String line) {
+ logger.warn(line);
+ }
+
+ protected void error(String line) {
+ logger.error(line);
+ }
+
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/InitCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/InitCommand.java
index 7c0a1d9b8a..b105c5e40b 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/InitCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/InitCommand.java
@@ -24,7 +24,7 @@ public class InitCommand extends GitCommand<Void> {
if (bare)
cmd.addArgs("--bare");
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/IsAncestorCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/IsAncestorCommand.java
index cdc81ae314..1bc5e8a755 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/IsAncestorCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/IsAncestorCommand.java
@@ -35,7 +35,7 @@ public class IsAncestorCommand extends GitCommand<Boolean> {
cmd.addArgs("merge-base", "--is-ancestor", ancestor, descendant);
- ExecuteResult result = cmd.execute(infoLogger(), errorLogger());
+ ExecuteResult result = cmd.execute(infoLogger, errorLogger);
if (result.getReturnCode() == 0)
return true;
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/IsBinaryCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/IsBinaryCommand.java
index bc9729320b..bfb3dc097b 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/IsBinaryCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/IsBinaryCommand.java
@@ -44,7 +44,7 @@ public class IsBinaryCommand extends GitCommand<Boolean> {
isBinary[0] = true;
}
- }, errorLogger()).checkReturnCode();
+ }, errorLogger).checkReturnCode();
return isBinary[0];
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/IsTreeLinkCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/IsTreeLinkCommand.java
new file mode 100644
index 0000000000..1c9cce121e
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/IsTreeLinkCommand.java
@@ -0,0 +1,65 @@
+package com.pmease.commons.git.command;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+
+import com.google.common.base.Preconditions;
+import com.pmease.commons.util.execution.Commandline;
+import com.pmease.commons.util.execution.ExecuteResult;
+import com.pmease.commons.util.execution.LineConsumer;
+
+public class IsTreeLinkCommand extends GitCommand<Boolean> {
+
+ private String revision;
+
+ private String symlink;
+
+ public IsTreeLinkCommand revision(String revision) {
+ this.revision = revision;
+ return this;
+ }
+
+ public IsTreeLinkCommand symlink(String symlink) {
+ this.symlink = symlink;
+ return this;
+ }
+
+ public IsTreeLinkCommand(File repoDir) {
+ super(repoDir);
+ }
+
+ @Override
+ public Boolean call() {
+ Preconditions.checkNotNull(revision, "revision has to be specified.");
+ Preconditions.checkNotNull(symlink, "symlink has to be specified.");
+
+ ShowCommand show = new ShowCommand(repoDir).path(symlink).revision(revision);
+ String targetPath;
+ try {
+ targetPath = new String(show.call(), "UTF8");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+
+ Commandline cmd = cmd().addArgs("ls-tree", revision + ":" + targetPath);
+ final boolean[] notTreeObject = new boolean[]{false};
+ ExecuteResult result = cmd.execute(traceLogger, new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ debug(line);
+ if (line.equals("fatal: not a tree object") || line.startsWith("fatal: Not a valid object name "))
+ notTreeObject[0] = true;
+ }
+
+ });
+
+ if (notTreeObject[0])
+ return false;
+
+ result.checkReturnCode();
+
+ return true;
+ }
+
+}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ListBranchesCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ListBranchesCommand.java
index d0e35f4885..5cb36130dd 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/ListBranchesCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ListBranchesCommand.java
@@ -29,7 +29,7 @@ public class ListBranchesCommand extends GitCommand<Collection<String>> {
branches.add(StringUtils.stripStart(line, "*").trim());
}
- }, errorLogger());
+ }, errorLogger);
return branches;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ListChangedFilesCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ListChangedFilesCommand.java
index f73f1178c8..a1d70664e7 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/ListChangedFilesCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ListChangedFilesCommand.java
@@ -48,7 +48,7 @@ public class ListChangedFilesCommand extends GitCommand<Collection<String>> {
changedFiles.add(line);
}
- }, errorLogger()).checkReturnCode();
+ }, errorLogger).checkReturnCode();
return changedFiles;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ListTagsCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ListTagsCommand.java
index 901ac7ffe1..4121fb137e 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/ListTagsCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ListTagsCommand.java
@@ -29,7 +29,7 @@ public class ListTagsCommand extends GitCommand<Collection<String>> {
branches.add(StringUtils.stripStart(line, "*").trim());
}
- }, errorLogger());
+ }, errorLogger);
return branches;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java
index 5c806ad068..a1649c3c2c 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java
@@ -71,7 +71,7 @@ public class ListTreeCommand extends GitCommand<List<TreeNode>> {
treeNodes.add(new TreeNode(repoDir, FileMode.fromBits(Integer.parseInt(mode, 8)), path, revision, hash, size));
}
- }, errorLogger()).checkReturnCode();
+ }, errorLogger).checkReturnCode();
return treeNodes;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java
index 60667d23c4..64f818b9ac 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java
@@ -168,7 +168,7 @@ public class LogCommand extends GitCommand<List<Commit>> {
}
}
- }, errorLogger()).checkReturnCode();
+ }, errorLogger).checkReturnCode();
if (commitBuilder.hash != null)
commits.add(commitBuilder.build());
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/MergeCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/MergeCommand.java
index 9e1b40e583..b6acdca38f 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/MergeCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/MergeCommand.java
@@ -67,7 +67,7 @@ public class MergeCommand extends GitCommand<Boolean> {
conflict[0] = true;
}
- }, errorLogger());
+ }, errorLogger);
if (conflict[0])
return false;
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.java
index 118e6874f6..8b8b07b484 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.java
@@ -33,7 +33,7 @@ public class PushCommand extends GitCommand<Void> {
Commandline cmd = cmd().addArgs("push");
cmd.addArgs(to, refspec);
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.java
index c10d61ddef..25109a398f 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.java
@@ -28,7 +28,7 @@ public class RemoveCommand extends GitCommand<Void> {
cmd.addArgs(path);
}
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ResetCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ResetCommand.java
index edb395b732..91771c8fbd 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/ResetCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ResetCommand.java
@@ -33,7 +33,7 @@ public class ResetCommand extends GitCommand<Void> {
if (commit != null)
cmd.addArgs(commit);
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ShowCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ShowCommand.java
index 0afc59d25a..a215b15ee8 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/ShowCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ShowCommand.java
@@ -35,7 +35,7 @@ public class ShowCommand extends GitCommand<byte[]> {
Commandline cmd = cmd().addArgs("show", revision + ":" + path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
- cmd.execute(baos, errorLogger()).checkReturnCode();
+ cmd.execute(baos, errorLogger).checkReturnCode();
return baos.toByteArray();
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/UpdateRefCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/UpdateRefCommand.java
index 088d795b9b..d16b9c0325 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/UpdateRefCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/UpdateRefCommand.java
@@ -51,7 +51,7 @@ public class UpdateRefCommand extends GitCommand<Void> {
if (reason != null)
cmd.addArgs(reason);
- cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+ cmd.execute(debugLogger, errorLogger).checkReturnCode();
return null;
}
diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/IsTreeLinkCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/IsTreeLinkCommandTest.java
new file mode 100644
index 0000000000..4c69b3d1a4
--- /dev/null
+++ b/commons.git/src/test/java/com/pmease/commons/git/command/IsTreeLinkCommandTest.java
@@ -0,0 +1,39 @@
+package com.pmease.commons.git.command;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.pmease.commons.git.Git;
+import com.pmease.commons.util.ClassUtils;
+import com.pmease.commons.util.FileUtils;
+import com.pmease.commons.util.ZipUtils;
+
+public class IsTreeLinkCommandTest {
+
+ @Test
+ public void test() throws IOException {
+ Assert.assertTrue(GitCommand.checkError() == null);
+
+ File tempDir = FileUtils.createTempDir();
+ try {
+ // Use a pre-built repo to test symbol link behaviors as Windows does not
+ // support symbol links.
+ try (InputStream is = ClassUtils.getResourceAsStream(IsTreeLinkCommandTest.class, "symlink_repo.zip");) {
+ ZipUtils.unzip(is, tempDir, null);
+
+ Git git = new Git(tempDir);
+ Assert.assertFalse(git.isTreeLink("file_link", "master"));
+ Assert.assertFalse(git.isTreeLink("link_link", "master"));
+ Assert.assertFalse(git.isTreeLink("external_link", "master"));
+ Assert.assertTrue(git.isTreeLink("dir_link", "master"));
+ }
+ } finally {
+ FileUtils.deleteDir(tempDir);
+ }
+ }
+
+}
diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/symlink_repo.zip b/commons.git/src/test/java/com/pmease/commons/git/command/symlink_repo.zip
new file mode 100644
index 0000000000..0b82a457c3
Binary files /dev/null and b/commons.git/src/test/java/com/pmease/commons/git/command/symlink_repo.zip differ
diff --git a/commons.util/src/main/java/com/pmease/commons/util/ClassUtils.java b/commons.util/src/main/java/com/pmease/commons/util/ClassUtils.java
index 04ee1d427e..fe759a895b 100644
--- a/commons.util/src/main/java/com/pmease/commons/util/ClassUtils.java
+++ b/commons.util/src/main/java/com/pmease/commons/util/ClassUtils.java
@@ -2,6 +2,7 @@ package com.pmease.commons.util;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Enumeration;
@@ -9,6 +10,8 @@ import java.util.HashSet;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
+import javax.annotation.Nullable;
+
import com.google.common.base.Preconditions;
public class ClassUtils extends org.apache.commons.lang3.ClassUtils {
@@ -107,4 +110,11 @@ public class ClassUtils extends org.apache.commons.lang3.ClassUtils {
return implementations;
}
+ public static @Nullable InputStream getResourceAsStream(@Nullable Class<?> locator, String path) {
+ path = StringUtils.stripStart(path, "/");
+ if (locator == null)
+ return ClassUtils.class.getClassLoader().getResourceAsStream(path);
+ else
+ return locator.getClassLoader().getResourceAsStream(locator.getPackage().getName().replace(".", "/") + "/" + path);
+ }
}
diff --git a/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java b/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
index 134f5a9187..4b84f7ab6b 100644
--- a/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
+++ b/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
@@ -236,10 +236,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
}
public static void createDir(File dir) {
- if (!dir.exists()) {
- if (!dir.mkdirs())
- throw new GeneralException("Unable to create directory: " + dir.getAbsolutePath());
- }
+ BootstrapUtils.createDir(dir);
}
public static void cleanDir(File dir) {
diff --git a/commons.util/src/main/java/com/pmease/commons/util/ZipUtils.java b/commons.util/src/main/java/com/pmease/commons/util/ZipUtils.java
new file mode 100644
index 0000000000..0c16887087
--- /dev/null
+++ b/commons.util/src/main/java/com/pmease/commons/util/ZipUtils.java
@@ -0,0 +1,43 @@
+package com.pmease.commons.util;
+
+import java.io.File;
+import java.io.InputStream;
+
+import javax.annotation.Nullable;
+
+import com.pmease.commons.bootstrap.BootstrapUtils;
+import com.pmease.commons.bootstrap.StringMatcher;
+
+public class ZipUtils {
+
+ /**
+ * Unzip files matching specified matcher from specified file to specified directory.
+ *
+ * @param srcFile
+ * zip file to extract from
+ * @param
+ * destDir destination directory to extract to
+ * @param matcher
+ * if not null, only entries with name matching this param in the zip will be extracted.
+ * Use null if you want to extract all entries from the zip file.
+ */
+ public static void unzip(File srcFile, File destDir, @Nullable StringMatcher matcher) {
+ BootstrapUtils.unzip(srcFile, destDir, matcher);
+ }
+
+ /**
+ * Unzip files matching specified matcher from input stream to specified directory.
+ *
+ * @param is
+ * input stream to unzip files from. This method will not close the stream
+ * after using it. Caller should be responsible for closing
+ * @param destDir
+ * destination directory to extract files to
+ * @param matcher
+ * matcher to select matched files for extracting
+ */
+ public static void unzip(InputStream is, File destDir, @Nullable StringMatcher matcher) {
+ BootstrapUtils.unzip(is, destDir, matcher);
+ }
+
+}
diff --git a/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.java b/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.java
index 817609b0e3..bfeabe793f 100644
--- a/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.java
+++ b/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.java
@@ -36,7 +36,7 @@ import org.slf4j.LoggerFactory;
*/
public abstract class LineConsumer extends OutputStream {
- public static final Logger logger = LoggerFactory.getLogger(LineConsumer.class);
+ private static final Logger logger = LoggerFactory.getLogger(LineConsumer.class);
/**
* If this property is not empty, output will be re-directed to this stream
@@ -101,26 +101,6 @@ public abstract class LineConsumer extends OutputStream {
*/
public abstract void consume(String line);
- protected void debug(String line) {
- logger.debug(line);
- }
-
- protected void trace(String line) {
- logger.trace(line);
- }
-
- protected void info(String line) {
- logger.info(line);
- }
-
- protected void warn(String line) {
- logger.warn(line);
- }
-
- protected void error(String line) {
- logger.error(line);
- }
-
@Override
public void flush() throws IOException {
if (buffer.size() > 0) {
diff --git a/commons.util/src/test/java/com/pmease/commons/util/ClassUtilsTest.java b/commons.util/src/test/java/com/pmease/commons/util/ClassUtilsTest.java
index 161f2bf8bf..304e014e21 100644
--- a/commons.util/src/test/java/com/pmease/commons/util/ClassUtilsTest.java
+++ b/commons.util/src/test/java/com/pmease/commons/util/ClassUtilsTest.java
@@ -1,5 +1,17 @@
package com.pmease.commons.util;
-public class ClassUtilsTest {
+import org.junit.Assert;
+import org.junit.Test;
+public class ClassUtilsTest {
+
+ @Test
+ public void testGetResourceAsStream() {
+ String packageName = ClassUtils.class.getPackage().getName();
+ Assert.assertNotNull(ClassUtils.getResourceAsStream(
+ null, packageName.replace('.', '/') + "/ClassUtils.class"));
+ Assert.assertNotNull(ClassUtils.getResourceAsStream(
+ ClassUtils.class, "ClassUtils.class"));
+ }
+
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultProjectManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultProjectManager.java
index 8c0bdc30e5..0f03af62ba 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultProjectManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultProjectManager.java
@@ -44,14 +44,11 @@ public class DefaultProjectManager extends AbstractGenericDao<Project> implement
this.storageManager = storageManager;
this.serverConfig = serverConfig;
- InputStream is = getClass().getClassLoader().getResourceAsStream("git-hook-template");
- Preconditions.checkNotNull(is);
- try {
+ try (InputStream is = getClass().getClassLoader().getResourceAsStream("git-hook-template")) {
+ Preconditions.checkNotNull(is);
hookTemplate = StringUtils.join(IOUtils.readLines(is), "\n");
} catch (IOException e) {
throw new RuntimeException(e);
- } finally {
- IOUtils.closeQuietly(is);
}
}