Top Description Inners Fields Constructors Methods
org.eclipse.jgit.api

public Class ArchiveCommand

extends GitCommand<OutputStream>
Class Inheritance
Imports
java.io.Closeable, .IOException, .OutputStream, java.text.MessageFormat, java.util.ArrayList, .Arrays, .HashMap, .List, .Map, java.util.concurrent.ConcurrentHashMap, org.eclipse.jgit.api.errors.GitAPIException, .JGitInternalException, org.eclipse.jgit.errors.IncorrectObjectTypeException, org.eclipse.jgit.internal.JGitText, org.eclipse.jgit.lib.Constants, .FileMode, .MutableObjectId, .ObjectId, .ObjectLoader, .ObjectReader, .Repository, org.eclipse.jgit.revwalk.RevCommit, .RevObject, .RevTree, .RevWalk, org.eclipse.jgit.treewalk.TreeWalk, org.eclipse.jgit.treewalk.filter.PathFilterGroup

Create an archive of files from a named tree.

Examples (git is a org.eclipse.jgit.api.Git instance):

Create a tarball from HEAD:

 ArchiveCommand.registerFormat("tar", new TarFormat());
 try {
 	git.archive().setTree(db.resolve("HEAD")).setOutputStream(out).call();
 } finally {
 	ArchiveCommand.unregisterFormat("tar");
 }
 

Create a ZIP file from master:

 ArchiveCommand.registerFormat("zip", new ZipFormat());
 try {
	git.archive().
		.setTree(db.resolve("master"))
		.setFormat("zip")
		.setOutputStream(out)
		.call();
 } finally {
	ArchiveCommand.unregisterFormat("zip");
 }
 
Since
3.1
See Also
Git documentation about archive

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ArchiveCommand.Format<
type representing an archive being created.
T extends Closeable
>

Archival format.

private static class
public static class
ArchiveCommand.UnsupportedFormatException

Signals an attempt to use an archival format that ArchiveCommand doesn't know about (for example due to a typo).

Field Summary

Modifier and TypeField and Description
private String
private Map<String, Object>
private static final Map<String, ArchiveCommand.FormatEntry>
formats

Available archival formats (corresponding to values for the --format= option)

private OutputStream
private List<String>
private String
private String
suffix

Filename suffix, for automatically choosing a format.

private ObjectId
Inherited from org.eclipse.jgit.api.GitCommand:
repo

Constructor Summary

AccessConstructor and Description
public
ArchiveCommand(Repository repo)

Constructor for ArchiveCommand

Method Summary

Modifier and TypeMethod and Description
public OutputStream
call()

Implements abstract org.eclipse.jgit.api.GitCommand.call.

Implements java.util.concurrent.Callable.call.

Computes a result, or throws an exception if unable to do so.

private static ArchiveCommand.Format<?>
formatBySuffix(String filenameSuffix)

private RevTree
private static ArchiveCommand.Format<?>
lookupFormat(String formatName)

public static void
registerFormat(String
name of a format (e.g., "tar" or "zip").
name
,
ArchiveCommand.Format<?>
archiver for that format
fmt
)

Adds support for an additional archival format.

private static <K, V> boolean

Returns:

true if the value was replaced
replace
(Map<K, V>
a map
map
,
K
key with which the specified value is associated
key
,
V
expected value for the key (null if should be absent).
oldValue
,
V
value to be associated with the key (null to remove).
newValue
)

Replaces the entry for a key only if currently mapped to a given value.

public ArchiveCommand

Returns:

this
setFilename
(String
intended filename for the archive
filename
)

Set the intended filename for the produced archive.

public ArchiveCommand

Returns:

this
setFormat
(String
archive format (e.g., "tar" or "zip"). null means to choose automatically based on the archive filename.
fmt
)

Set archive format

public ArchiveCommand

Returns:

this
setFormatOptions
(Map<String, Object>
archive format options (e.g., level=9 for zip compression).
options
)

Set archive format options

public ArchiveCommand

Returns:

this
setOutputStream
(OutputStream
the stream to which to write the archive
out
)

Set output stream

public ArchiveCommand

Returns:

this
setPaths
(String...
file names (e.g file1.c) or directory names (e.g. dir to add dir/file1 and dir/file2) can also be given to add all files in the directory, recursively. Fileglobs (e.g. *.c) are not yet supported.
paths
)

Set an optional parameter path.

public ArchiveCommand

Returns:

this
setPrefix
(String
string prefixed to filenames in archive (e.g., "master/"). null means to not use any leading prefix.
prefix
)

Set string prefixed to filenames in archive

public ArchiveCommand

Returns:

this
setTree
(ObjectId
the tag, commit, or tree object to produce an archive for
tree
)

Set the tag, commit, or tree object to produce an archive for

public static void
unregisterFormat(String
name of format (e.g., "tar" or "zip").
name
)

Marks support for an archival format as no longer needed so its Format can be garbage collected if no one else is using it either.

private <T extends Closeable> OutputStream
Inherited from org.eclipse.jgit.api.GitCommand:
checkCallablegetRepositorysetCallable

Field Detail

formatback to summary
private String format
formatOptionsback to summary
private Map<String, Object> formatOptions
formatsback to summary
private static final Map<String, ArchiveCommand.FormatEntry> formats

Available archival formats (corresponding to values for the --format= option)

outback to summary
private OutputStream out
pathsback to summary
private List<String> paths
prefixback to summary
private String prefix
suffixback to summary
private String suffix

Filename suffix, for automatically choosing a format.

treeback to summary
private ObjectId tree

Constructor Detail

ArchiveCommandback to summary
public ArchiveCommand(Repository repo)

Constructor for ArchiveCommand

Parameters
repo:Repository

the org.eclipse.jgit.lib.Repository

Method Detail

callback to summary
public OutputStream call() throws GitAPIException

Implements abstract org.eclipse.jgit.api.GitCommand.call.

Implements java.util.concurrent.Callable.call.

Doc from org.eclipse.jgit.api.GitCommand.call.

Computes a result, or throws an exception if unable to do so.

Execute the command

Returns:OutputStream

Doc from java.util.concurrent.Callable.call.

computed result

Annotations
@Override
formatBySuffixback to summary
private static ArchiveCommand.Format<?> formatBySuffix(String filenameSuffix) throws UnsupportedFormatException
getTreeback to summary
private RevTree getTree(RevObject o) throws IncorrectObjectTypeException
lookupFormatback to summary
private static ArchiveCommand.Format<?> lookupFormat(String formatName) throws UnsupportedFormatException
registerFormatback to summary
public static void registerFormat(String name, ArchiveCommand.Format<?> fmt)

Adds support for an additional archival format. To avoid unnecessary dependencies, ArchiveCommand does not have support for any formats built in; use this function to add them.

OSGi plugins providing formats should call this function at bundle activation time.

It is okay to register the same archive format with the same name multiple times, but don't forget to unregister it that same number of times, too.

Registering multiple formats with different names and the same or overlapping suffixes results in undefined behavior.

Todo

check that suffixes don't overlap.

Parameters
name:String

name of a format (e.g., "tar" or "zip").

fmt:ArchiveCommand.Format<?>

archiver for that format

Exceptions
JGitInternalException:
A different archival format with that name was already registered.
replaceback to summary
private static <K, V> boolean replace(Map<K, V> map, K key, V oldValue, V newValue)

Replaces the entry for a key only if currently mapped to a given value.

Parameters
map:Map<K, V>

a map

key:K

key with which the specified value is associated

oldValue:V

expected value for the key (null if should be absent).

newValue:V

value to be associated with the key (null to remove).

Returns:boolean

true if the value was replaced

setFilenameback to summary
public ArchiveCommand setFilename(String filename)

Set the intended filename for the produced archive. Currently the only effect is to determine the default archive format when none is specified with setFormat(String).

Parameters
filename:String

intended filename for the archive

Returns:ArchiveCommand

this

setFormatback to summary
public ArchiveCommand setFormat(String fmt)

Set archive format

Parameters
fmt:String

archive format (e.g., "tar" or "zip"). null means to choose automatically based on the archive filename.

Returns:ArchiveCommand

this

setFormatOptionsback to summary
public ArchiveCommand setFormatOptions(Map<String, Object> options)

Set archive format options

Parameters
options:Map<String, Object>

archive format options (e.g., level=9 for zip compression).

Returns:ArchiveCommand

this

Since
4.0
setOutputStreamback to summary
public ArchiveCommand setOutputStream(OutputStream out)

Set output stream

Parameters
out:OutputStream

the stream to which to write the archive

Returns:ArchiveCommand

this

setPathsback to summary
public ArchiveCommand setPaths(String... paths)

Set an optional parameter path. without an optional path parameter, all files and subdirectories of the current working directory are included in the archive. If one or more paths are specified, only these are included.

Parameters
paths:String[]

file names (e.g file1.c) or directory names (e.g. dir to add dir/file1 and dir/file2) can also be given to add all files in the directory, recursively. Fileglobs (e.g. *.c) are not yet supported.

Returns:ArchiveCommand

this

Since
3.4
setPrefixback to summary
public ArchiveCommand setPrefix(String prefix)

Set string prefixed to filenames in archive

Parameters
prefix:String

string prefixed to filenames in archive (e.g., "master/"). null means to not use any leading prefix.

Returns:ArchiveCommand

this

Since
3.3
setTreeback to summary
public ArchiveCommand setTree(ObjectId tree)

Set the tag, commit, or tree object to produce an archive for

Parameters
tree:ObjectId

the tag, commit, or tree object to produce an archive for

Returns:ArchiveCommand

this

unregisterFormatback to summary
public static void unregisterFormat(String name)

Marks support for an archival format as no longer needed so its Format can be garbage collected if no one else is using it either.

In other words, this decrements the reference count for an archival format. If the reference count becomes zero, removes support for that format.

Parameters
name:String

name of format (e.g., "tar" or "zip").

Exceptions
JGitInternalException:
No such archival format was registered.
writeArchiveback to summary
private <T extends Closeable> OutputStream writeArchive(ArchiveCommand.Format<T> fmt)
org.eclipse.jgit.api back to summary

public Interface ArchiveCommand.Format<T extends Closeable>

Type Parameters
<T>
type representing an archive being created.

Archival format. Usage: Repository repo = git.getRepository(); T out = format.createArchiveOutputStream(System.out); try { for (...) { format.putEntry(out, path, mode, repo.open(objectId)); } out.close(); }

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

new archive object for use in putEntry
createArchiveOutputStream
(OutputStream
underlying output stream to which to write the archive.
s
)

Start a new archive.

public T

Returns:

new archive object for use in putEntry
createArchiveOutputStream
(OutputStream
underlying output stream to which to write the archive.
s
,
Map<String, Object>
options to apply to the underlying output stream. Keys are option names and values are option values.
o
)

Start a new archive.

public void
putEntry(T
archive object from createArchiveOutputStream
out
,
ObjectId
the tag, commit, or tree object to produce an archive for
tree
,
String
full filename relative to the root of the archive (with trailing '/' for directories)
path
,
FileMode
mode (for example FileMode.REGULAR_FILE or FileMode.SYMLINK)
mode
,
ObjectLoader
blob object with data for this entry (null for directories)
loader
)

Write an entry to an archive.

public Iterable<String>

Returns:

this format's suffixes
suffixes
()

Filename suffixes representing this format (e.g., { ".tar.gz", ".tgz" }).

Method Detail

createArchiveOutputStreamback to summary
public T createArchiveOutputStream(OutputStream s) throws IOException

Start a new archive. Entries can be included in the archive using the putEntry method, and then the archive should be closed using its close method.

Parameters
s:OutputStream

underlying output stream to which to write the archive.

Returns:T

new archive object for use in putEntry

Exceptions
IOException:
thrown by the underlying output stream for I/O errors
createArchiveOutputStreamback to summary
public T createArchiveOutputStream(OutputStream s, Map<String, Object> o) throws IOException

Start a new archive. Entries can be included in the archive using the putEntry method, and then the archive should be closed using its close method. In addition options can be applied to the underlying stream. E.g. compression level.

Parameters
s:OutputStream

underlying output stream to which to write the archive.

o:Map<String, Object>

options to apply to the underlying output stream. Keys are option names and values are option values.

Returns:T

new archive object for use in putEntry

Exceptions
IOException:
thrown by the underlying output stream for I/O errors
Since
4.0
putEntryback to summary
public void putEntry(T out, ObjectId tree, String path, FileMode mode, ObjectLoader loader) throws IOException

Write an entry to an archive.

Parameters
out:T

archive object from createArchiveOutputStream

tree:ObjectId

the tag, commit, or tree object to produce an archive for

path:String

full filename relative to the root of the archive (with trailing '/' for directories)

mode:FileMode

mode (for example FileMode.REGULAR_FILE or FileMode.SYMLINK)

loader:ObjectLoader

blob object with data for this entry (null for directories)

Exceptions
IOException:
thrown by the underlying output stream for I/O errors
Since
4.7
suffixesback to summary
public Iterable<String> suffixes()

Filename suffixes representing this format (e.g., { ".tar.gz", ".tgz" }). The behavior is undefined when suffixes overlap (if one format claims suffix ".7z", no other format should take ".tar.7z").

Returns:Iterable<String>

this format's suffixes

org.eclipse.jgit.api back to summary

private Class ArchiveCommand.FormatEntry

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final ArchiveCommand.Format<?>
pack-priv final int
refcnt

Number of times this format has been registered.

Constructor Summary

AccessConstructor and Description
public
FormatEntry(ArchiveCommand.Format<?> format, int refcnt)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

formatback to summary
pack-priv final ArchiveCommand.Format<?> format
refcntback to summary
pack-priv final int refcnt

Number of times this format has been registered.

Constructor Detail

FormatEntryback to summary
public FormatEntry(ArchiveCommand.Format<?> format, int refcnt)
org.eclipse.jgit.api back to summary

public Class ArchiveCommand.UnsupportedFormatException

extends GitAPIException
Class Inheritance

Signals an attempt to use an archival format that ArchiveCommand doesn't know about (for example due to a typo).

Field Summary

Modifier and TypeField and Description
private final String
private static final long

Constructor Summary

AccessConstructor and Description
public
UnsupportedFormatException(String
the problematic format name
format
)

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the problematic format name
getFormat
()

Get the problematic format name

Field Detail

formatback to summary
private final String format
serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.eclipse.jgit.api.errors.GitAPIException.serialVersionUID.

Constructor Detail

UnsupportedFormatExceptionback to summary
public UnsupportedFormatException(String format)
Parameters
format:String

the problematic format name

Method Detail

getFormatback to summary
public String getFormat()

Get the problematic format name

Returns:String

the problematic format name