Package org.eclipse.jgit.pgm
Class TextBuiltin
- java.lang.Object
-
- org.eclipse.jgit.pgm.TextBuiltin
-
public abstract class TextBuiltin extends Object
Abstract command which can be invoked from the command line.Commands are configured with a single "current" repository and then the
execute(String[])
method is invoked with the arguments that appear on the command line after the command name.Command constructors should perform as little work as possible as they may be invoked very early during process loading, and the command may not execute even though it was constructed.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TextBuiltin.TerminatedByHelpException
Exception thrown byTextBuiltin
if it proceeds 'help' option
-
Field Summary
Fields Modifier and Type Field Description protected RevWalk
argWalk
RevWalk used during command line parsing, if it was required.protected Repository
db
Git repository the command was invoked within.protected OutputStream
errs
Error output stream, typically this is standard error.protected ThrowingPrintWriter
errw
Error writer, typically this is standard error.protected String
gitdir
Directory supplied via --git-dir command line option.protected InputStream
ins
Input stream, typically this is standard input.protected OutputStream
outs
Stream to output to, typically this is standard output.protected ThrowingPrintWriter
outw
Writer to output to, typically this is standard output.
-
Constructor Summary
Constructors Constructor Description TextBuiltin()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static boolean
containsHelp(String[] args)
Check if the arguments contain a help optionprotected static Die
die(boolean aborted)
Exit the commandprotected static Die
die(boolean aborted, Throwable cause)
Exit the commandprotected static Die
die(String why)
Exit the command with an error messageprotected static Die
die(String why, Throwable cause)
Exit the command with an error message and an exceptionvoid
execute(String[] args)
Parse arguments and run this command.ThrowingPrintWriter
getErrorWriter()
Get error writerThrowingPrintWriter
getOutputWriter()
Get output writerRepository
getRepository()
Get the repositoryprotected ResourceBundle
getResourceBundle()
Get resource bundle with localized textsprotected void
init(Repository repository, String gitDir)
Initialize the command to work with a repository.void
initRaw(Repository repository, String gitDir, InputStream input, OutputStream output, OutputStream error)
Initializes the command to work with a repository, including setting the output and error streams.protected void
parseArguments(String[] args)
Parses the command line arguments prior to running.protected void
printUsage(String message, CmdLineParser clp)
Print usage help text.void
printUsageAndExit(String message, CmdLineParser clp)
Print an error message and the usage linevoid
printUsageAndExit(CmdLineParser clp)
Print the usage lineprotected boolean
requiresRepository()
If this command requires a repository.protected abstract void
run()
Perform the actions of this command.
-
-
-
Field Detail
-
ins
protected InputStream ins
Input stream, typically this is standard input.- Since:
- 3.4
-
outw
protected ThrowingPrintWriter outw
Writer to output to, typically this is standard output.- Since:
- 2.2
-
outs
protected OutputStream outs
Stream to output to, typically this is standard output.- Since:
- 2.2
-
errw
protected ThrowingPrintWriter errw
Error writer, typically this is standard error.- Since:
- 3.4
-
errs
protected OutputStream errs
Error output stream, typically this is standard error.- Since:
- 3.4
-
db
protected Repository db
Git repository the command was invoked within.
-
gitdir
protected String gitdir
Directory supplied via --git-dir command line option.
-
argWalk
protected RevWalk argWalk
RevWalk used during command line parsing, if it was required.
-
-
Method Detail
-
requiresRepository
protected boolean requiresRepository()
If this command requires a repository.- Returns:
- true if
db
/getRepository()
is required
-
initRaw
public void initRaw(Repository repository, String gitDir, InputStream input, OutputStream output, OutputStream error)
Initializes the command to work with a repository, including setting the output and error streams.- Parameters:
repository
- the opened repository that the command should work on.gitDir
- value of the--git-dir
command line option, ifrepository
is null.input
- input stream from which input will be readoutput
- output stream to which output will be writtenerror
- error stream to which errors will be written- Since:
- 4.9
-
init
protected void init(Repository repository, String gitDir)
Initialize the command to work with a repository.- Parameters:
repository
- the opened repository that the command should work on.gitDir
- value of the--git-dir
command line option, ifrepository
is null.
-
execute
public final void execute(String[] args) throws Exception
Parse arguments and run this command.- Parameters:
args
- command line arguments passed after the command name.- Throws:
Exception
- an error occurred while processing the command. The main framework will catch the exception and print a message on standard error.
-
parseArguments
protected void parseArguments(String[] args) throws IOException
Parses the command line arguments prior to running.This method should only be invoked by
execute(String[])
, prior to callingrun()
. The default implementation parses all arguments into this object's instance fields.- Parameters:
args
- the arguments supplied on the command line, if any.- Throws:
IOException
-
printUsageAndExit
public void printUsageAndExit(CmdLineParser clp) throws IOException
Print the usage line- Parameters:
clp
- aCmdLineParser
object.- Throws:
IOException
-
printUsageAndExit
public void printUsageAndExit(String message, CmdLineParser clp) throws IOException
Print an error message and the usage line- Parameters:
message
- aString
object.clp
- aCmdLineParser
object.- Throws:
IOException
-
printUsage
protected void printUsage(String message, CmdLineParser clp) throws IOException
Print usage help text.- Parameters:
message
- non nullclp
- parser used to print options- Throws:
IOException
- Since:
- 4.2
-
getErrorWriter
public ThrowingPrintWriter getErrorWriter()
Get error writer- Returns:
- error writer, typically this is standard error.
- Since:
- 4.2
-
getOutputWriter
public ThrowingPrintWriter getOutputWriter()
Get output writer- Returns:
- output writer, typically this is standard output.
- Since:
- 4.9
-
getResourceBundle
protected ResourceBundle getResourceBundle()
Get resource bundle with localized texts- Returns:
- the resource bundle that will be passed to args4j for purpose of string localization
-
run
protected abstract void run() throws Exception
Perform the actions of this command.This method should only be invoked by
execute(String[])
.- Throws:
Exception
- an error occurred while processing the command. The main framework will catch the exception and print a message on standard error.
-
getRepository
public Repository getRepository()
Get the repository- Returns:
- the repository this command accesses.
-
die
protected static Die die(String why)
Exit the command with an error message- Parameters:
why
- textual explanation- Returns:
- a runtime exception the caller is expected to throw
-
die
protected static Die die(String why, Throwable cause)
Exit the command with an error message and an exception- Parameters:
why
- textual explanationcause
- why the command has failed.- Returns:
- a runtime exception the caller is expected to throw
-
die
protected static Die die(boolean aborted)
Exit the command- Parameters:
aborted
- boolean indicating that the execution has been aborted before running- Returns:
- a runtime exception the caller is expected to throw
- Since:
- 3.4
-
die
protected static Die die(boolean aborted, Throwable cause)
Exit the command- Parameters:
aborted
- boolean indicating that the execution has been aborted before runningcause
- why the command has failed.- Returns:
- a runtime exception the caller is expected to throw
- Since:
- 4.2
-
containsHelp
public static boolean containsHelp(String[] args)
Check if the arguments contain a help option- Parameters:
args
- non null- Returns:
- true if the given array contains help option
- Since:
- 4.2
-
-