-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitGet.java
executable file
·82 lines (69 loc) · 2.93 KB
/
GitGet.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
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.eclipse.jgit:org.eclipse.jgit:5.12.0.202106070339-r
//DEPS org.eclipse.jgit:org.eclipse.jgit.ssh.jsch:5.12.0.202106070339-r
//DEPS info.picocli:picocli:4.6.2
//DEPS commons-io:commons-io:2.8.0
//SOURCES AbstractGit.java
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.lib.RepositoryCache;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.util.FS;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
@Command(name = "GitGet", mixinStandardHelpOptions = true, version = "GitGet 0.1", description = "A command to get one or more files (or directories) from a Git repository")
class GitGet extends AbstractGit {
@Parameters(arity = "1", index = "1", description = "The name of a new directory where to store files")
File directory;
@Parameters(arity = "1", index = "2..n", description = "The file or directory paths to get from the repository")
String[] paths;
@Option(names = {"--fresh"}, description = "Make a fresh clone of the repository")
boolean fresh;
File cloneDirectory;
public static void main(final String... args) {
int exitCode = new CommandLine(new GitGet()).execute(args);
System.exit(exitCode);
}
@Override
public Integer call() throws Exception {
final Path clonePath = getCloneDirectory().toPath();
if (fresh) {
Files.deleteIfExists(clonePath);
}
Files.createDirectories(clonePath);
if (!RepositoryCache.FileKey.isGitRepository(new File(getCloneDirectory(), ".git"), FS.DETECTED)) {
super.call();
}
Arrays.stream(paths).forEach(path -> {
final File file = new File(path);
try {
final File destFile = new File(directory, file.getPath());
if (!destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
throw new IOException("Failed to create directory: " + destFile.getParentFile());
}
final File srcFile = new File(getCloneDirectory(), file.getPath());
if (path.endsWith("/")) {
FileUtils.copyDirectory(srcFile, destFile);
} else {
FileUtils.copyFile(srcFile, destFile);
}
} catch (final Exception exception) {
System.err.printf("Failed to copy %s to %s%n", path, directory);
}
});
return 0;
}
@Override
public File getCloneDirectory() throws Exception {
if (cloneDirectory == null) {
cloneDirectory = new File(System.getProperty("java.io.tmpdir"), new URIish(repository).getPath());
}
return cloneDirectory;
}
}