Browse Source

Simplify development of commands: added main() to CLIGitCommand

This will execute git commands (with arguments) specified on the command
line, handy for developing/debugging a sequence of arbitrary git
commands working on same repository.

The git working dir path can be specified via Java system property
"git_work_tree". If not specified, current directory will be used.

Change-Id: I621a9ec198c31e28a383818efeb4b3f835ba1d6f
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
tags/v4.2.0.201601211800-r
Andrey Loskutov 8 years ago
parent
commit
241b50be31

+ 1
- 0
org.eclipse.jgit.pgm.test/META-INF/MANIFEST.MF View File

@@ -11,6 +11,7 @@ Import-Package: org.eclipse.jgit.api;version="[4.2.0,4.3.0)",
org.eclipse.jgit.api.errors;version="[4.2.0,4.3.0)",
org.eclipse.jgit.diff;version="[4.2.0,4.3.0)",
org.eclipse.jgit.dircache;version="[4.2.0,4.3.0)",
org.eclipse.jgit.internal.storage.file;version="4.2.0",
org.eclipse.jgit.junit;version="[4.2.0,4.3.0)",
org.eclipse.jgit.lib;version="[4.2.0,4.3.0)",
org.eclipse.jgit.merge;version="[4.2.0,4.3.0)",

+ 11
- 1
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java View File

@@ -70,10 +70,20 @@ public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
trash = db.getWorkTree();
}

/**
* Executes specified git commands (with arguments)
*
* @param cmds
* each string argument must be a valid git command line, e.g.
* "git branch -h"
* @return command output
* @throws Exception
*/
protected String[] execute(String... cmds) throws Exception {
List<String> result = new ArrayList<String>(cmds.length);
for (String cmd : cmds)
for (String cmd : cmds) {
result.addAll(CLIGitCommand.execute(cmd, db));
}
return result.toArray(new String[0]);
}


+ 31
- 0
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java View File

@@ -43,10 +43,12 @@
package org.eclipse.jgit.pgm;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.pgm.opt.CmdLineParser;
@@ -69,6 +71,35 @@ public class CLIGitCommand {
return arguments;
}

/**
* Executes git commands (with arguments) specified on the command line. The
* git repository (same for all commands) can be specified via system
* property "-Dgit_work_tree=path_to_work_tree". If the property is not set,
* current directory is used.
*
* @param args
* each element in the array must be a valid git command line,
* e.g. "git branch -h"
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String workDir = System.getProperty("git_work_tree");
if (workDir == null) {
workDir = ".";
System.out.println(
"System property 'git_work_tree' not specified, using current directory: "
+ new File(workDir).getAbsolutePath());
}
try (Repository db = new FileRepository(workDir + "/.git")) {
for (String cmd : args) {
List<String> result = execute(cmd, db);
for (String line : result) {
System.out.println(line);
}
}
}
}

public static List<String> execute(String str, Repository db)
throws Exception {
try {

+ 1
- 1
org.eclipse.jgit/META-INF/MANIFEST.MF View File

@@ -65,7 +65,7 @@ Export-Package: org.eclipse.jgit.annotations;version="4.2.0",
org.eclipse.jgit.junit,
org.eclipse.jgit.junit.http,
org.eclipse.jgit.http.server,
org.eclipse.jgit.java7.test,
org.eclipse.jgit.pgm.test,
org.eclipse.jgit.pgm",
org.eclipse.jgit.internal.storage.pack;version="4.2.0";x-friends:="org.eclipse.jgit.junit,org.eclipse.jgit.test,org.eclipse.jgit.pgm",
org.eclipse.jgit.lib;version="4.2.0";

Loading…
Cancel
Save