Browse Source

Sort "eager" path-like options to the end of the help

The "--" path option (and all other similar options consuming all
remaining arguments) should be placed at the end of the command line
help.

Currently jgit reset -h shows this:

jgit reset [commit-ish] [path ... ...] [-- path ... ...] [--hard]
[--help (-h)] [--mixed] [--soft]

After the patch the help shows this:

jgit reset [commit-ish] [path ... ...] [--hard] [--help (-h)] [--mixed]
[--soft] [-- path ... ...]

Bug: 484951
Change-Id: I3db332bf293ca8d6bfaab0d546cd35af689bd46e
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
tags/v4.2.0.201601211800-r
Andrey Loskutov 8 years ago
parent
commit
aabbc58341

+ 7
- 0
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ResetTest.java View File

@@ -62,6 +62,13 @@ public class ResetTest extends CLIRepositoryTestCase {
git = new Git(db);
}

@Test
public void testPathOptionHelp() throws Exception {
String[] result = execute("git reset -h");
assertTrue("Unexpected argument: " + result[1],
result[1].endsWith("[-- path ... ...]"));
}

@Test
public void testZombieArgument_Bug484951() throws Exception {
String[] result = execute("git reset -h");

+ 8
- 0
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java View File

@@ -44,6 +44,7 @@ package org.eclipse.jgit.pgm;

import static org.eclipse.jgit.lib.Constants.MASTER;
import static org.eclipse.jgit.lib.Constants.R_HEADS;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

@@ -55,6 +56,13 @@ import org.junit.Test;

public class StatusTest extends CLIRepositoryTestCase {

@Test
public void testPathOptionHelp() throws Exception {
String[] result = execute("git status -h");
assertTrue("Unexpected argument: " + result[1],
result[1].endsWith("[-- path ... ...]"));
}

@Test
public void testStatusDefault() throws Exception {
executeTest("git status", false, true);

+ 36
- 0
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/opt/CmdLineParser.java View File

@@ -43,11 +43,13 @@

package org.eclipse.jgit.pgm.opt;

import java.io.Writer;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
@@ -65,6 +67,7 @@ import org.kohsuke.args4j.NamedOptionDef;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.RestOfArgumentsHandler;
import org.kohsuke.args4j.spi.Setter;

/**
@@ -288,4 +291,37 @@ public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
}
return options;
}

@Override
public void printSingleLineUsage(Writer w, ResourceBundle rb) {
List<OptionHandler> options = getOptions();
if (options.isEmpty()) {
super.printSingleLineUsage(w, rb);
return;
}
List<OptionHandler> backup = new ArrayList<>(options);
boolean changed = sortRestOfArgumentsHandlerToTheEnd(options);
try {
super.printSingleLineUsage(w, rb);
} finally {
if (changed) {
options.clear();
options.addAll(backup);
}
}
}

private boolean sortRestOfArgumentsHandlerToTheEnd(
List<OptionHandler> options) {
for (int i = 0; i < options.size(); i++) {
OptionHandler handler = options.get(i);
if (handler instanceof RestOfArgumentsHandler
|| handler instanceof PathTreeFilterHandler) {
options.remove(i);
options.add(handler);
return true;
}
}
return false;
}
}

Loading…
Cancel
Save