|
@@ -19,26 +19,43 @@ import java.io.File;
|
|
|
public class GitUtil {
|
|
|
|
|
|
/**
|
|
|
- * @param url git 地址
|
|
|
- * @param username 用户名
|
|
|
- * @param password 密码
|
|
|
- * @param localEmptyDirectory 空目录
|
|
|
+ * @param url git 地址
|
|
|
+ * @param username 用户名
|
|
|
+ * @param password 密码
|
|
|
+ * @param directoryPath 空目录
|
|
|
+ * @param cover 如果目录内有文件,是否覆盖
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
- public static void clone(String url, String username, String password, String localEmptyDirectory) {
|
|
|
- File localEmptyDirectoryFile = new File(localEmptyDirectory);
|
|
|
- if (localEmptyDirectoryFile.exists()) {
|
|
|
- File[] files = localEmptyDirectoryFile.listFiles();
|
|
|
- if (files == null || files.length > 0) {
|
|
|
- throw new RuntimeException("GitUtil--clone 目标下载目录不是空目录。");
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ public static void clone(String url, String username, String password, String directoryPath, boolean cover) {
|
|
|
+ File directoryFile = new File(directoryPath);
|
|
|
+ if (directoryFile.exists()) {
|
|
|
+ File[] files = directoryFile.listFiles();
|
|
|
+ if (files == null || files.length == 0) {
|
|
|
+ clone(url, username, password, directoryFile);
|
|
|
+ } else {
|
|
|
+ if (cover) {
|
|
|
+ for (File file : files) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ clone(url, username, password, directoryFile);
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("GitUtil--clone 目标下载目录不是空目录。");
|
|
|
+ }
|
|
|
}
|
|
|
} else {
|
|
|
- localEmptyDirectoryFile.mkdirs();
|
|
|
+ directoryFile.mkdirs();
|
|
|
+ clone(url, username, password, directoryFile);
|
|
|
}
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ private static void clone(String url, String username, String password, File directoryFile) {
|
|
|
Git git = Git.cloneRepository()
|
|
|
.setURI(url)
|
|
|
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
|
|
|
- .setDirectory(localEmptyDirectoryFile)
|
|
|
+ .setDirectory(directoryFile)
|
|
|
.call();
|
|
|
}
|
|
|
}
|