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 package org.eclipse.jgit.pgm;
45
46 import java.io.FileNotFoundException;
47 import java.io.FileOutputStream;
48 import java.io.OutputStream;
49
50 import org.eclipse.jgit.api.ArchiveCommand;
51 import org.eclipse.jgit.api.Git;
52 import org.eclipse.jgit.api.errors.GitAPIException;
53 import org.eclipse.jgit.archive.ArchiveFormats;
54 import org.eclipse.jgit.lib.ObjectId;
55 import org.eclipse.jgit.pgm.TextBuiltin;
56 import org.eclipse.jgit.pgm.internal.CLIText;
57 import org.kohsuke.args4j.Argument;
58 import org.kohsuke.args4j.Option;
59
60 @Command(common = true, usage = "usage_archive")
61 class Archive extends TextBuiltin {
62 static {
63 ArchiveFormats.registerAll();
64 }
65
66 @Argument(index = 0, metaVar = "metaVar_treeish")
67 private ObjectId tree;
68
69 @Option(name = "--format", metaVar = "metaVar_archiveFormat", usage = "usage_archiveFormat")
70 private String format;
71
72 @Option(name = "--prefix", metaVar = "metaVar_archivePrefix", usage = "usage_archivePrefix")
73 private String prefix;
74
75 @Option(name = "--output", aliases = { "-o" }, metaVar = "metaVar_file", usage = "usage_archiveOutput")
76 private String output;
77
78 @Override
79 protected void run() throws Exception {
80 if (tree == null)
81 throw die(CLIText.get().treeIsRequired);
82
83 OutputStream stream = null;
84 try {
85 if (output != null)
86 stream = new FileOutputStream(output);
87 else
88 stream = outs;
89
90 try (Git git = new Git(db)) {
91 ArchiveCommand cmd = git.archive()
92 .setTree(tree)
93 .setFormat(format)
94 .setPrefix(prefix)
95 .setOutputStream(stream);
96 if (output != null)
97 cmd.setFilename(output);
98 cmd.call();
99 } catch (GitAPIException e) {
100 throw die(e.getMessage());
101 }
102 } catch (FileNotFoundException e) {
103 throw die(e.getMessage());
104 } finally {
105 if (output != null && stream != null)
106 stream.close();
107 }
108 }
109 }