aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java
blob: 4028105b8705ddf83bbdd62f4d66680163e5233b (plain)
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
/*
 * Copyright (C) 2012 Google Inc. and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.pgm;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.eclipse.jgit.api.ArchiveCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.archive.ArchiveFormats;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

@Command(common = true, usage = "usage_archive")
class Archive extends TextBuiltin {
	static {
		ArchiveFormats.registerAll();
	}

	@Argument(index = 0, metaVar = "metaVar_treeish")
	private ObjectId tree;

	@Option(name = "--format", metaVar = "metaVar_archiveFormat", usage = "usage_archiveFormat")
	private String format;

	@Option(name = "--prefix", metaVar = "metaVar_archivePrefix", usage = "usage_archivePrefix")
	private String prefix;

	@Option(name = "--output", aliases = { "-o" }, metaVar = "metaVar_file", usage = "usage_archiveOutput")
	private String output;

	/** {@inheritDoc} */
	@Override
	protected void run() throws Exception {
		if (tree == null)
			throw die(CLIText.get().treeIsRequired);

		OutputStream stream = null;
		try {
			if (output != null)
				stream = new FileOutputStream(output);
			else
				stream = outs;

			try (Git git = new Git(db)) {
				ArchiveCommand cmd = git.archive()
					.setTree(tree)
					.setFormat(format)
					.setPrefix(prefix)
					.setOutputStream(stream);
				if (output != null)
					cmd.setFilename(output);
				cmd.call();
			} catch (GitAPIException e) {
				throw die(e.getMessage(), e);
			}
		} catch (FileNotFoundException e) {
			throw die(e.getMessage(), e);
		} finally {
			if (output != null && stream != null)
				stream.close();
		}
	}
}