Skip to content

Commit 0ae5695

Browse files
committed
improve gitUtil
1 parent 0d7c910 commit 0ae5695

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

lib/utils/FileUtil.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,20 @@ public static Boolean copyFile2File(File srcFile, File dstFile) {
155155
return true;
156156
}
157157

158-
public static boolean deleteDirectory(String dirPath) {
159-
File directoryToBeDeleted = new File(dirPath);
158+
public static boolean deleteDirectory(Path dirPath) {
159+
File directoryToBeDeleted = dirPath.toFile();
160160
File[] allContents = directoryToBeDeleted.listFiles();
161161
if (allContents != null) {
162162
for (File file : allContents) {
163-
deleteDirectory(file.getAbsolutePath());
163+
deleteDirectory(file.toPath());
164164
}
165165
}
166166
return directoryToBeDeleted.delete();
167167
}
168168

169-
public static void copyDirectory(String srcDirStr, String destDirStr) {
170-
File source = new File(srcDirStr);
171-
File dest = new File(destDirStr);
169+
public static void copyDirectory(Path srcDirPath, Path destDirPath) {
170+
File source = srcDirPath.toFile();
171+
File dest = destDirPath.toFile();
172172
try {
173173
FileUtils.copyDirectory(source, dest);
174174
} catch (IOException e) {
@@ -263,7 +263,7 @@ public static List<String> readFileToLineList(String filePath) {
263263
public static void createFolder(String dirStr, Boolean deleteIfExist) {
264264
File dir = new File(dirStr);
265265
if (deleteIfExist && dir.exists()) {
266-
deleteDirectory(dir.getAbsolutePath());
266+
deleteDirectory(dir.toPath());
267267
dir.mkdirs();
268268
} else if (!dir.exists()) {
269269
dir.mkdirs();
@@ -280,11 +280,11 @@ public static boolean deleteDirectory(File directoryToBeDeleted) {
280280
return directoryToBeDeleted.delete();
281281
}
282282

283-
public static List<File> findFilePathofSpecifcTypeRecusive(String tarDir, String extension) {
283+
public static List<File> findFilePathofSpecifcTypeRecusive(Path tarDir, String extension) {
284284

285285
List<File> fileList = new ArrayList<File>();
286286
try {
287-
Files.walk(Paths.get(tarDir)).filter(Files::isRegularFile).forEach((f) -> {
287+
Files.walk(tarDir).filter(Files::isRegularFile).forEach((f) -> {
288288
String filepath = f.toString();
289289
if (filepath.endsWith(extension))
290290
// System.out.println(file + " found!");

lib/utils/GitUtil.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static Boolean clone(String repoName, String usrName, Path targetDir) {
1717
}
1818
targetDir.toFile().mkdirs();
1919

20-
String cmd = "timeout 300 git clone https://github.com/" + repoName + "/" + usrName + " " + targetDir;
20+
String cmd = "timeout 600 git clone https://github.com/" + repoName + "/" + usrName + " " + targetDir;
2121

2222
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, targetDir, 0);
2323
if (pr.exitCode == 0) {
@@ -169,13 +169,18 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
169169
ProcessUtil.ProcessReporter pr = new ProcessUtil.ProcessReporter();
170170

171171
if (ifForce) {
172-
String resetCMD = "timeout 300 git reset --hard";
172+
String resetCMD = "timeout 600 git reset --hard";
173173
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir, 0);
174174
}
175175

176+
String defaultBranchNameString = getDefaultBranch(repoDir);
177+
if (defaultBranchNameString == null || defaultBranchNameString == "") {
178+
defaultBranchNameString = "master";
179+
}
180+
176181
String checkoutCMD = null;
177182
if (com == null) { // checkout latest if null
178-
checkoutCMD = "timeout 300 git checkout master";
183+
checkoutCMD = "timeout 300 git checkout " + defaultBranchNameString;
179184
} else {
180185
checkoutCMD = "timeout 300 git checkout " + com;
181186
}
@@ -199,4 +204,13 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
199204
}
200205
}
201206

207+
private static String getDefaultBranch(Path gitDirPath) {
208+
// TODO Auto-generated method stub
209+
String cmdString = "git symbolic-ref refs/remotes/origin/HEAD";
210+
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmdString, null, gitDirPath, 0);
211+
// output refs/remotes/origin/master
212+
return pr.out.trim().split("refs/remotes/origin/")[1].trim();
213+
214+
}
215+
202216
}

0 commit comments

Comments
 (0)