/
GitClone.java
191 lines (173 loc) · 8.45 KB
/
GitClone.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package reposense.git;
import static reposense.system.CommandRunner.runCommand;
import static reposense.util.StringsUtil.addQuotes;
import static reposense.util.StringsUtil.addQuotesForFilePath;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.logging.Level;
import java.util.logging.Logger;
import reposense.git.exception.GitCloneException;
import reposense.model.RepoConfiguration;
import reposense.system.CommandRunner;
import reposense.system.CommandRunnerProcess;
import reposense.system.LogsManager;
import reposense.util.FileUtil;
import reposense.util.SystemUtil;
/**
* Contains git clone related functionalities.
* Git clone is responsible for cloning a local/remote repository into a new directory.
*/
public class GitClone {
private static final Logger logger = LogsManager.getLogger(GitClone.class);
/**
* Runs "git clone --bare" command asynchronously with {@code rootPath} as working directory to clone a bare repo
* specified in the {@code config} into the folder {@code outputFolderName}.
*
* @return an instance of {@link CommandRunnerProcess} to allow tracking the status of the cloning process.
* @throws GitCloneException when an error occurs while attempting to clone the repo.
*/
public static CommandRunnerProcess cloneBareAsync(RepoConfiguration config, Path rootPath, String outputFolderName)
throws GitCloneException {
try {
return CommandRunner.runCommandAsync(rootPath, getCloneBareCommand(config, outputFolderName));
} catch (RuntimeException rte) {
throw new GitCloneException(rte);
}
}
/**
* Runs "git clone --bare --shallow-since=" command asynchronously with {@code rootPath} as working directory
* to clone a bare repo specified in the {@code config} into the folder {@code outputFolderName}.
* Uses {@code sinceDate} for the "--shallow-since=" flag.
*
* @return an instance of {@link CommandRunnerProcess} to allow tracking the status of the cloning process.
* @throws GitCloneException when an error occurs while attempting to clone the repo.
*/
public static CommandRunnerProcess cloneShallowBareAsync(RepoConfiguration config, Path rootPath,
String outputFolderName, LocalDateTime sinceDate) throws GitCloneException {
try {
return CommandRunner.runCommandAsync(rootPath,
getCloneShallowBareCommand(config, outputFolderName, sinceDate));
} catch (RuntimeException rte) {
throw new GitCloneException(rte);
}
}
/**
* Runs "git clone --bare" command asynchronously with {@code rootPath} as working directory to clone a bare repo
* specified in the {@code config} into the folder {@code outputFolderName}.
*
* @throws GitCloneException when an error occurs while attempting to clone the repo.
*/
public static void clonePartialBare(RepoConfiguration config, Path rootPath, String outputFolderName)
throws GitCloneException {
try {
CommandRunner.runCommand(rootPath, getClonePartialBareCommand(config, outputFolderName));
return;
} catch (RuntimeException rte) {
throw new GitCloneException(rte);
}
}
/**
* Runs "git clone --bare" command asynchronously with {@code rootPath} as working directory to clone a bare repo
* specified in the {@code config} into the folder {@code outputFolderName}.
* Uses {@code sinceDate} for the "--shallow-since=" flag.
*
* @throws GitCloneException when an error occurs while attempting to clone the repo.
*/
public static void cloneShallowPartialBare(RepoConfiguration config, Path rootPath, String outputFolderName,
LocalDateTime sinceDate) throws GitCloneException {
try {
CommandRunner.runCommand(rootPath, getCloneShallowPartialBareCommand(config, outputFolderName, sinceDate));
return;
} catch (RuntimeException rte) {
throw new GitCloneException(rte);
}
}
/**
* Performs a full clone with {@code rootPath} as working directory relative to the location of the bare repo
* version of {@code config} into the folder {@code outputFolderName} and checks out the branch specified in
* {@code config}.
*
* @throws GitCloneException when an error occurs while attempting to clone the repo.
* @throws IOException if it fails to delete a directory.
*/
public static void cloneFromBareAndUpdateBranch(Path rootPath, RepoConfiguration config)
throws GitCloneException, IOException {
Path relativePath = FileUtil.getBareRepoPath(config);
String outputFolderName = Paths.get(config.getRepoFolderName(), config.getRepoName()).toString();
Path outputFolderPath = Paths.get(FileUtil.REPOS_ADDRESS, outputFolderName);
if (!SystemUtil.isTestEnvironment()) {
FileUtil.deleteDirectory(outputFolderPath.toString());
} else if (SystemUtil.isTestEnvironment() && Files.exists(outputFolderPath)) {
GitCheckout.checkoutBranch(outputFolderPath.toString(), config.getBranch());
return;
}
String command = getCloneBareAndBranchCommand(relativePath, config, outputFolderPath.toString());
try {
runCommand(rootPath, command);
} catch (RuntimeException rte) {
logger.log(Level.SEVERE, "Exception met while cloning or checking out " + config.getDisplayName() + "."
+ "Analysis terminated.", rte);
throw new GitCloneException(rte);
}
}
/**
* Constructs the command to clone a bare repo specified in the {@code config}
* into the folder {@code outputFolderName}.
*/
private static String getCloneBareCommand(RepoConfiguration config, String outputFolderName) {
String output = "git clone --bare "
+ addQuotesForFilePath(config.getLocation().toString()) + " "
+ addQuotesForFilePath(outputFolderName);
return output;
}
/**
* Constructs the command to clone from {@code repoPath} into {@code outputFolderName} and
* branch to the designated branch in {@code config}.
*
* @param repoPath Location of repo.
* @param config Config of the repo to be analyzed.
* @param outputFolderName Output directory for the cloned repo.
* @return Command to be used.
*/
private static String getCloneBareAndBranchCommand(Path repoPath, RepoConfiguration config,
String outputFolderName) {
return "git clone "
+ addQuotesForFilePath(repoPath.toString())
+ " --branch " + config.getBranch()
+ " " + addQuotesForFilePath(outputFolderName);
}
/**
* Constructs the command to shallow clone a bare repo specified in the {@code config}
* with {@code shallowSinceDate} into the folder {@code outputFolderName}.
*/
private static String getCloneShallowBareCommand(RepoConfiguration config, String outputFolderName,
LocalDateTime shallowSinceDate) {
return "git clone --bare --shallow-since="
+ addQuotes(shallowSinceDate.toString()) + " "
+ addQuotesForFilePath(config.getLocation().toString()) + " "
+ addQuotesForFilePath(outputFolderName);
}
/**
* Constructs the command to partial clone a bare repo specified in the {@code config}
* into the folder {@code outputFolderName}.
*/
private static String getClonePartialBareCommand(RepoConfiguration config, String outputFolderName) {
return "git clone --bare --filter=blob:none "
+ addQuotesForFilePath(config.getLocation().toString()) + " "
+ addQuotesForFilePath(outputFolderName);
}
/**
* Constructs the command to shallow partial clone a bare repo specified in the {@code config}
* with {@code shallowSinceDate} into the folder {@code outputFolderName}.
*/
private static String getCloneShallowPartialBareCommand(RepoConfiguration config, String outputFolderName,
LocalDateTime shallowSinceDate) {
return "git clone --bare --filter=blob:none --shallow-since="
+ addQuotes(shallowSinceDate.toString()) + " "
+ addQuotesForFilePath(config.getLocation().toString()) + " "
+ addQuotesForFilePath(outputFolderName);
}
}