Class 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.

    • 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.
    • Constructor Detail

      • TextBuiltin

        public TextBuiltin()
    • 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, if repository is null.
        input - input stream from which input will be read
        output - output stream to which output will be written
        error - 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, if repository 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 calling run(). 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
      • printUsage

        protected void printUsage​(String message,
                                  CmdLineParser clp)
                           throws IOException
        Print usage help text.
        Parameters:
        message - non null
        clp - 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 explanation
        cause - 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 running
        cause - 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