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