public abstract class TextBuiltin
extends java.lang.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.
|
private java.lang.String |
commandName |
protected Repository |
db
Git repository the command was invoked within.
|
protected java.io.OutputStream |
errs
Error output stream, typically this is standard error.
|
protected ThrowingPrintWriter |
errw
Error writer, typically this is standard error.
|
protected java.lang.String |
gitdir
Directory supplied via --git-dir command line option.
|
private boolean |
help |
protected java.io.InputStream |
ins
Input stream, typically this is standard input.
|
protected java.io.OutputStream |
outs
Stream to output to, typically this is standard output.
|
protected ThrowingPrintWriter |
outw
Writer to output to, typically this is standard output.
|
private SshDriver |
sshDriver |
Constructor and Description |
---|
TextBuiltin() |
Modifier and Type | Method and Description |
---|---|
(package private) java.lang.String |
abbreviateRef(java.lang.String dst,
boolean abbreviateRemote) |
static boolean |
containsHelp(java.lang.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,
java.lang.Throwable cause)
Exit the command
|
protected static Die |
die(java.lang.String why)
Exit the command with an error message
|
protected static Die |
die(java.lang.String why,
java.lang.Throwable cause)
Exit the command with an error message and an exception
|
void |
execute(java.lang.String[] args)
Parse arguments and run this command.
|
ThrowingPrintWriter |
getErrorWriter()
Get error writer
|
private java.nio.charset.Charset |
getLogOutputEncodingCharset(Repository repository)
Get the log output encoding specified in the repository's
i18n.logOutputEncoding configuration. |
ThrowingPrintWriter |
getOutputWriter()
Get output writer
|
Repository |
getRepository()
Get the repository
|
protected java.util.ResourceBundle |
getResourceBundle()
Get resource bundle with localized texts
|
protected void |
init(Repository repository,
java.lang.String gitDir)
Initialize the command to work with a repository.
|
void |
initRaw(Repository repository,
java.lang.String gitDir,
java.io.InputStream input,
java.io.OutputStream output,
java.io.OutputStream error)
Initializes the command to work with a repository, including setting the
output and error streams.
|
protected void |
parseArguments(java.lang.String[] args)
Parses the command line arguments prior to running.
|
protected void |
printUsage(java.lang.String message,
CmdLineParser clp)
Print usage help text.
|
void |
printUsageAndExit(CmdLineParser clp)
Print the usage line
|
void |
printUsageAndExit(java.lang.String message,
CmdLineParser clp)
Print an error message and the usage line
|
protected boolean |
requiresRepository()
If this command requires a repository.
|
(package private) ObjectId |
resolve(java.lang.String s) |
protected abstract void |
run()
Perform the actions of this command.
|
(package private) void |
setCommandName(java.lang.String name) |
private java.lang.String commandName
private boolean help
private SshDriver sshDriver
protected java.io.InputStream ins
protected ThrowingPrintWriter outw
protected java.io.OutputStream outs
protected ThrowingPrintWriter errw
protected java.io.OutputStream errs
protected Repository db
protected java.lang.String gitdir
protected RevWalk argWalk
final void setCommandName(java.lang.String name)
protected boolean requiresRepository()
db
/getRepository()
is requiredpublic void initRaw(Repository repository, java.lang.String gitDir, java.io.InputStream input, java.io.OutputStream output, java.io.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 writtenprivate java.nio.charset.Charset getLogOutputEncodingCharset(Repository repository)
i18n.logOutputEncoding
configuration.repository
- the repository.i18n.logOutputEncoding
, or
UTF_8
.protected void init(Repository repository, java.lang.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(java.lang.String[] args) throws java.lang.Exception
args
- command line arguments passed after the command name.java.lang.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(java.lang.String[] args) throws java.io.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.java.io.IOException
public void printUsageAndExit(CmdLineParser clp) throws java.io.IOException
clp
- a CmdLineParser
object.java.io.IOException
public void printUsageAndExit(java.lang.String message, CmdLineParser clp) throws java.io.IOException
message
- a String
object.clp
- a CmdLineParser
object.java.io.IOException
protected void printUsage(java.lang.String message, CmdLineParser clp) throws java.io.IOException
message
- non nullclp
- parser used to print optionsjava.io.IOException
public ThrowingPrintWriter getErrorWriter()
public ThrowingPrintWriter getOutputWriter()
protected java.util.ResourceBundle getResourceBundle()
protected abstract void run() throws java.lang.Exception
This method should only be invoked by execute(String[])
.
java.lang.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()
ObjectId resolve(java.lang.String s) throws java.io.IOException
java.io.IOException
protected static Die die(java.lang.String why)
why
- textual explanationprotected static Die die(java.lang.String why, java.lang.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, java.lang.Throwable cause)
aborted
- boolean indicating that the execution has been aborted before
runningcause
- why the command has failed.java.lang.String abbreviateRef(java.lang.String dst, boolean abbreviateRemote)
public static boolean containsHelp(java.lang.String[] args)
args
- non null