diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java new file mode 100644 index 000000000..583beefed --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2012 Google Inc. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.pgm; + +import static org.junit.Assert.assertArrayEquals; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.InputStreamReader; +import java.io.IOException; + +import java.lang.String; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.lib.CLIRepositoryTestCase; +import org.eclipse.jgit.pgm.CLIGitCommand; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +public class ArchiveTest extends CLIRepositoryTestCase { + private Git git; + private String emptyTree; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + git = new Git(db); + git.commit().setMessage("initial commit").call(); + emptyTree = db.resolve("HEAD^{tree}").abbreviate(12).name(); + } + + @Ignore("Some versions of java.util.zip refuse to write an empty ZIP") + @Test + public void testEmptyArchive() throws Exception { + final byte[] result = CLIGitCommand.rawExecute( // + "git archive " + emptyTree, db); + assertArrayEquals(new String[0], listZipEntries(result)); + } + + @Test + public void testArchiveWithFiles() throws Exception { + writeTrashFile("a", "a file with content!"); + writeTrashFile("c", ""); // empty file + writeTrashFile("unrelated", "another file, just for kicks"); + git.add().addFilepattern("a").call(); + git.add().addFilepattern("c").call(); + git.commit().setMessage("populate toplevel").call(); + + final byte[] result = CLIGitCommand.rawExecute( // + "git archive HEAD", db); + assertArrayEquals(new String[] { "a", "c" }, // + listZipEntries(result)); + } + + @Test + public void testArchiveWithSubdir() throws Exception { + writeTrashFile("a", "a file with content!"); + writeTrashFile("b.c", "before subdir in git sort order"); + writeTrashFile("b0c", "after subdir in git sort order"); + writeTrashFile("c", ""); + git.add().addFilepattern("a").call(); + git.add().addFilepattern("b.c").call(); + git.add().addFilepattern("b0c").call(); + git.add().addFilepattern("c").call(); + git.commit().setMessage("populate toplevel").call(); + writeTrashFile("b/b", "file in subdirectory"); + writeTrashFile("b/a", "another file in subdirectory"); + git.add().addFilepattern("b").call(); + git.commit().setMessage("add subdir").call(); + + final byte[] result = CLIGitCommand.rawExecute( // + "git archive master", db); + String[] expect = { "a", "b.c", "b0c", "b/a", "b/b", "c" }; + String[] actual = listZipEntries(result); + + Arrays.sort(expect); + Arrays.sort(actual); + assertArrayEquals(expect, actual); + } + + @Test + public void testArchivePreservesContent() throws Exception { + final String payload = "“The quick brown fox jumps over the lazy dog!”"; + writeTrashFile("xyzzy", payload); + git.add().addFilepattern("xyzzy").call(); + git.commit().setMessage("add file with content").call(); + + final byte[] result = CLIGitCommand.rawExecute( // + "git archive HEAD", db); + assertArrayEquals(new String[] { payload }, // + zipEntryContent(result, "xyzzy")); + } + + private static String[] listZipEntries(byte[] zipData) throws IOException { + final List l = new ArrayList(); + final ZipInputStream in = new ZipInputStream( // + new ByteArrayInputStream(zipData)); + + ZipEntry e; + while ((e = in.getNextEntry()) != null) + l.add(e.getName()); + in.close(); + return l.toArray(new String[l.size()]); + } + + private static String[] zipEntryContent(byte[] zipData, String path) // + throws IOException { + final ZipInputStream in = new ZipInputStream( // + new ByteArrayInputStream(zipData)); + ZipEntry e; + while ((e = in.getNextEntry()) != null) { + if (!e.getName().equals(path)) + continue; + + // found! + final List l = new ArrayList(); + final BufferedReader reader = new BufferedReader( // + new InputStreamReader(in, "UTF-8")); + String line; + while ((line = reader.readLine()) != null) + l.add(line); + return l.toArray(new String[l.size()]); + } + + // not found + return null; + } +} diff --git a/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin b/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin index 1a6cc8551..24dfa7b7e 100644 --- a/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin +++ b/org.eclipse.jgit.pgm/META-INF/services/org.eclipse.jgit.pgm.TextBuiltin @@ -1,5 +1,6 @@ org.eclipse.jgit.pgm.Add org.eclipse.jgit.pgm.AmazonS3Client +org.eclipse.jgit.pgm.Archive org.eclipse.jgit.pgm.Blame org.eclipse.jgit.pgm.Branch org.eclipse.jgit.pgm.Checkout diff --git a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties index a75bb001d..2afaa12f9 100644 --- a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties +++ b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties @@ -8,6 +8,7 @@ N=N IPZillaPasswordPrompt=IPZilla Password alreadyOnBranch=Already on ''{0}'' alreadyUpToDate=Already up-to-date. +archiveEntryModeIgnored=warning: mode of {0} ignored authorInfo=Author: {0} <{1}> averageMSPerRead=average {0} ms/read branchAlreadyExists=A branch named ''{0}'' already exists. @@ -156,6 +157,7 @@ tagAlreadyExists=tag ''{0}'' already exists tagLabel=tag taggerInfo=Tagger: {0} <{1}> timeInMilliSeconds={0} ms +treeIsRequired=argument tree is required tooManyRefsGiven=Too many refs given unknownIoErrorStdout=An unknown I/O error occurred on standard output unknownMergeStrategy=unknown merge strategy {0} specified @@ -193,6 +195,7 @@ usage_actOnRemoteTrackingBranches=act on remote-tracking branches usage_addFileContentsToTheIndex=Add file contents to the index usage_alterTheDetailShown=alter the detail shown usage_approveDestructionOfRepository=approve destruction of repository +usage_archive=zip up files from the named tree usage_blameLongRevision=show long revision usage_blameRange=annotate only the given range usage_blameRawTimestamp=show raw timestamp diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java new file mode 100644 index 000000000..786524eca --- /dev/null +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2012 Google Inc. + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.eclipse.jgit.pgm; + +import java.lang.String; +import java.lang.System; +import java.text.MessageFormat; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import org.eclipse.jgit.lib.FileMode; +import org.eclipse.jgit.lib.MutableObjectId; +import org.eclipse.jgit.lib.ObjectLoader; +import org.eclipse.jgit.lib.ObjectReader; +import org.eclipse.jgit.pgm.CLIText; +import org.eclipse.jgit.pgm.TextBuiltin; +import org.eclipse.jgit.treewalk.AbstractTreeIterator; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.kohsuke.args4j.Argument; + +@Command(common = true, usage = "usage_archive") +class Archive extends TextBuiltin { + @Argument(index = 0, metaVar = "metaVar_treeish") + private AbstractTreeIterator tree; + + @Override + protected void run() throws Exception { + final TreeWalk walk = new TreeWalk(db); + final ObjectReader reader = walk.getObjectReader(); + final MutableObjectId idBuf = new MutableObjectId(); + final ZipOutputStream out = new ZipOutputStream(outs); + + if (tree == null) + throw die(CLIText.get().treeIsRequired); + + walk.reset(); + walk.addTree(tree); + walk.setRecursive(true); + while (walk.next()) { + final String name = walk.getPathString(); + final FileMode mode = walk.getFileMode(0); + + if (mode == FileMode.TREE) + // ZIP entries for directories are optional. + // Leave them out, mimicking "git archive". + continue; + + walk.getObjectId(idBuf, 0); + final ZipEntry entry = new ZipEntry(name); + final ObjectLoader loader = reader.open(idBuf); + entry.setSize(loader.getSize()); + out.putNextEntry(entry); + loader.copyTo(out); + + if (mode != FileMode.REGULAR_FILE) + System.err.println(MessageFormat.format( // + CLIText.get().archiveEntryModeIgnored, // + name)); + } + + out.close(); + } +} diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java index 2fe46c4a5..0ed6a78a1 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java @@ -77,6 +77,7 @@ public static String formatLine(String line) { /***/ public String IPZillaPasswordPrompt; /***/ public String alreadyOnBranch; /***/ public String alreadyUpToDate; + /***/ public String archiveEntryModeIgnored; /***/ public String authorInfo; /***/ public String averageMSPerRead; /***/ public String branchAlreadyExists; @@ -220,6 +221,7 @@ public static String formatLine(String line) { /***/ public String taggerInfo; /***/ public String timeInMilliSeconds; /***/ public String tooManyRefsGiven; + /***/ public String treeIsRequired; /***/ public char[] unknownIoErrorStdout; /***/ public String unknownMergeStrategy; /***/ public String unmergedPaths;