@ -50,8 +50,10 @@ import static org.eclipse.jgit.lib.Constants.R_TAGS;
import java.io.BufferedWriter ;
import java.io.FileDescriptor ;
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.OutputStream ;
import java.io.OutputStreamWriter ;
import java.io.PrintWriter ;
@ -84,6 +86,13 @@ public abstract class TextBuiltin {
@Option ( name = "--help" , usage = "usage_displayThisHelpText" , aliases = { "-h" } )
private boolean help ;
/ * *
* Input stream , typically this is standard input .
*
* @since 3 . 4
* /
protected InputStream ins ;
/ * *
* Writer to output to , typically this is standard output .
*
@ -106,6 +115,20 @@ public abstract class TextBuiltin {
@Deprecated
protected PrintWriter out ;
/ * *
* Error writer , typically this is standard error .
*
* @since 3 . 4
* /
protected ThrowingPrintWriter errw ;
/ * *
* Error output stream , typically this is standard error .
*
* @since 3 . 4
* /
protected OutputStream errs ;
/** Git repository the command was invoked within. */
protected Repository db ;
@ -137,16 +160,27 @@ public abstract class TextBuiltin {
try {
final String outputEncoding = repository ! = null ? repository
. getConfig ( ) . getString ( "i18n" , null , "logOutputEncoding" ) : null ; //$NON-NLS-1$ //$NON-NLS-2$
if ( ins = = null )
ins = new FileInputStream ( FileDescriptor . in ) ;
if ( outs = = null )
outs = new FileOutputStream ( FileDescriptor . out ) ;
BufferedWriter bufw ;
if ( errs = = null )
errs = new FileOutputStream ( FileDescriptor . err ) ;
BufferedWriter outbufw ;
if ( outputEncoding ! = null )
outbufw = new BufferedWriter ( new OutputStreamWriter ( outs ,
outputEncoding ) ) ;
else
outbufw = new BufferedWriter ( new OutputStreamWriter ( outs ) ) ;
out = new PrintWriter ( outbufw ) ;
outw = new ThrowingPrintWriter ( outbufw ) ;
BufferedWriter errbufw ;
if ( outputEncoding ! = null )
bufw = new BufferedWriter ( new OutputStreamWriter ( outs ,
err bufw = new BufferedWriter ( new OutputStreamWriter ( err s,
outputEncoding ) ) ;
else
bufw = new BufferedWriter ( new OutputStreamWriter ( outs ) ) ;
out = new PrintWriter ( bufw ) ;
outw = new ThrowingPrintWriter ( bufw ) ;
errbufw = new BufferedWriter ( new OutputStreamWriter ( errs ) ) ;
errw = new ThrowingPrintWriter ( errbufw ) ;
} catch ( IOException e ) {
throw die ( CLIText . get ( ) . cannotCreateOutputStream ) ;
}
@ -184,14 +218,15 @@ public abstract class TextBuiltin {
*
* @param args
* the arguments supplied on the command line , if any .
* @throws IOException
* /
protected void parseArguments ( final String [ ] args ) {
protected void parseArguments ( final String [ ] args ) throws IOException {
final CmdLineParser clp = new CmdLineParser ( this ) ;
try {
clp . parseArgument ( args ) ;
} catch ( CmdLineException err ) {
if ( ! help ) {
System . err . println ( MessageFormat . format ( CLIText . get ( ) . fatalError , err . getMessage ( ) ) ) ;
this . errw . println ( MessageFormat . format ( CLIText . get ( ) . fatalError , err . getMessage ( ) ) ) ;
System . exit ( 1 ) ;
}
}
@ -207,8 +242,9 @@ public abstract class TextBuiltin {
* Print the usage line
*
* @param clp
* @throws IOException
* /
public void printUsageAndExit ( final CmdLineParser clp ) {
public void printUsageAndExit ( final CmdLineParser clp ) throws IOException {
printUsageAndExit ( "" , clp ) ; //$NON-NLS-1$
}
@ -217,20 +253,20 @@ public abstract class TextBuiltin {
*
* @param message
* @param clp
* @throws IOException
* /
public void printUsageAndExit ( final String message , final CmdLineParser clp ) {
PrintWriter writer = new PrintWriter ( System . err ) ;
writer . println ( message ) ;
writer . print ( "jgit " ) ; //$NON-NLS-1$
writer . print ( commandName ) ;
clp . printSingleLineUsage ( writer , getResourceBundle ( ) ) ;
writer . println ( ) ;
writer . println ( ) ;
clp . printUsage ( writer , getResourceBundle ( ) ) ;
writer . println ( ) ;
writer . flush ( ) ;
public void printUsageAndExit ( final String message , final CmdLineParser clp ) throws IOException {
errw . println ( message ) ;
errw . print ( "jgit " ) ; //$NON-NLS-1$
errw . print ( commandName ) ;
clp . printSingleLineUsage ( errw , getResourceBundle ( ) ) ;
errw . println ( ) ;
errw . println ( ) ;
clp . printUsage ( errw , getResourceBundle ( ) ) ;
errw . println ( ) ;
errw . flush ( ) ;
System . exit ( 1 ) ;
}