public abstract class TextBuiltin extends Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
TextBuiltin.TerminatedByHelpException
Exception thrown by
TextBuiltin if it proceeds 'help' option |
Modifier and Type | Field and 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 and Description |
---|
TextBuiltin() |
Modifier and Type | Method and Description |
---|---|
static boolean |
containsHelp(String[] args)
Check if the arguments contain a help option
|
protected static Die |
die(boolean aborted)
Exit the command
|
protected static Die |
die(boolean aborted,
Throwable cause)
Exit the command
|
protected static Die |
die(String why)
Exit the command with an error message
|
protected static Die |
die(String why,
Throwable cause)
Exit the command with an error message and an exception
|
void |
execute(String[] args)
Parse arguments and run this command.
|
ThrowingPrintWriter |
getErrorWriter()
Get error writer
|
ThrowingPrintWriter |
getOutputWriter()
Get output writer
|
Repository |
getRepository()
Get the repository
|
protected ResourceBundle |
getResourceBundle()
Get resource bundle with localized texts
|
protected 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(CmdLineParser clp)
Print the usage line
|
void |
printUsageAndExit(String message,
CmdLineParser clp)
Print an error message and the usage line
|
protected boolean |
requiresRepository()
If this command requires a repository.
|
protected abstract void |
run()
Perform the actions of this command.
|
protected InputStream ins
protected ThrowingPrintWriter outw
protected OutputStream outs
protected ThrowingPrintWriter errw
protected OutputStream errs
protected Repository db
protected String gitdir
protected RevWalk argWalk
protected boolean requiresRepository()
db
/getRepository()
is requiredpublic void initRaw(Repository repository, String gitDir, InputStream input, OutputStream output, OutputStream error)
repository
- the opened repository that the command should work on.gitDir
- value of the --git-dir
command line option, if
repository
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 writtenprotected void init(Repository repository, String gitDir)
repository
- the opened repository that the command should work on.gitDir
- value of the --git-dir
command line option, if
repository
is null.public final void execute(String[] args) throws Exception
args
- command line arguments passed after the command name.Exception
- an error occurred while processing the command. The main
framework will catch the exception and print a message on
standard error.protected void parseArguments(String[] args) throws IOException
This method should only be invoked by execute(String[])
, prior
to calling run()
. The default implementation parses all
arguments into this object's instance fields.
args
- the arguments supplied on the command line, if any.IOException
public void printUsageAndExit(CmdLineParser clp) throws IOException
clp
- a CmdLineParser
object.IOException
public void printUsageAndExit(String message, CmdLineParser clp) throws IOException
message
- a String
object.clp
- a CmdLineParser
object.IOException
protected void printUsage(String message, CmdLineParser clp) throws IOException
message
- non nullclp
- parser used to print optionsIOException
public ThrowingPrintWriter getErrorWriter()
public ThrowingPrintWriter getOutputWriter()
protected ResourceBundle getResourceBundle()
protected abstract void run() throws Exception
This method should only be invoked by execute(String[])
.
Exception
- an error occurred while processing the command. The main
framework will catch the exception and print a message on
standard error.public Repository getRepository()
protected static Die die(String why)
why
- textual explanationprotected static Die die(String why, Throwable cause)
why
- textual explanationcause
- why the command has failed.protected static Die die(boolean aborted)
aborted
- boolean indicating that the execution has been aborted before
runningprotected static Die die(boolean aborted, Throwable cause)
aborted
- boolean indicating that the execution has been aborted before
runningcause
- why the command has failed.public static boolean containsHelp(String[] args)
args
- non nullCopyright © 2019 Eclipse JGit Project. All rights reserved.