Class FS

  • Direct Known Subclasses:
    FS_POSIX, FS_Win32

    public abstract class FS
    extends java.lang.Object
    Abstraction to support various file system operations not in Java.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected FS()
      Constructs a file system abstraction.
      protected FS​(FS src)
      Initialize this FS using another's current settings.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      abstract boolean canExecute​(java.io.File f)
      Determine if the file is executable (or not).
      boolean createNewFile​(java.io.File path)
      Deprecated.
      FS.LockToken createNewFileAtomic​(java.io.File path)
      Create a new file.
      void createSymLink​(java.io.File path, java.lang.String target)
      Create a symbolic link
      private java.io.File defaultUserHomeImpl()  
      void delete​(java.io.File f)
      Delete a file.
      static FS detect()
      Auto-detect the appropriate file system abstraction.
      static FS detect​(java.lang.Boolean cygwinUsed)
      Auto-detect the appropriate file system abstraction, taking into account the presence of a Cygwin installation on the system.
      private void detectSymlinkSupport()  
      protected abstract java.io.File discoverGitExe()
      Discover the path to the Git executable.
      protected java.io.File discoverGitSystemConfig()
      Discover the path to the system-wide Git configuration file
      FS.ExecutionResult execute​(java.lang.ProcessBuilder pb, java.io.InputStream in)
      Execute a command defined by a ProcessBuilder.
      boolean exists​(java.io.File path)
      Tests if the path exists, in case of a symbolic link, true even if the target does not exist
      java.nio.file.attribute.BasicFileAttributes fileAttributes​(java.io.File file)
      Return all the attributes of a file, without following symbolic links.
      java.io.File findHook​(Repository repository, java.lang.String hookName)
      Tries to find a hook matching the given one in the given repository.
      FS.Attributes getAttributes​(java.io.File path)
      Get the file attributes we care for.
      static FS.FileStoreAttributes getFileStoreAttributes​(java.nio.file.Path dir)
      Get cached FileStore attributes, if not yet available measure them using a probe file under the given directory.
      java.io.File getGitSystemConfig()
      Get the currently used path to the system-wide Git configuration file.
      private java.io.File getHooksDirectory​(Repository repository)  
      private java.io.File getRunDirectory​(Repository repository, java.lang.String hookName)  
      protected ProcessResult internalRunHookIfPresent​(Repository repository, java.lang.String hookName, java.lang.String[] args, java.io.PrintStream outRedirect, java.io.PrintStream errRedirect, java.lang.String stdinArgs)
      abstract boolean isCaseSensitive()
      Is this file system case sensitive
      boolean isDirectory​(java.io.File path)
      Check if path is a directory.
      boolean isFile​(java.io.File path)
      Examine if path represents a regular file.
      boolean isHidden​(java.io.File path)
      Whether path is hidden, either starts with .
      boolean isSymLink​(java.io.File path)
      Whether the path is a symbolic link (and we support these).
      long lastModified​(java.io.File f)
      Deprecated.
      java.time.Instant lastModifiedInstant​(java.io.File f)
      Get the last modified time of a file system object.
      java.time.Instant lastModifiedInstant​(java.nio.file.Path p)
      Get the last modified time of a file system object.
      long length​(java.io.File path)
      Get the length of a file or link, If the OS/JRE supports symbolic links it's the length of the link, else the length of the target.
      WorkingTreeIterator.Entry[] list​(java.io.File directory, FileTreeIterator.FileModeStrategy fileModeStrategy)
      Enumerates children of a directory.
      abstract FS newInstance()
      Create a new instance of the same type of FS.
      java.io.File normalize​(java.io.File file)
      Normalize the unicode path to composed form.
      java.lang.String normalize​(java.lang.String name)
      Normalize the unicode path to composed form.
      protected static java.lang.String readPipe​(java.io.File dir, java.lang.String[] command, java.lang.String encoding)
      Execute a command and return a single line of output as a String
      protected static java.lang.String readPipe​(java.io.File dir, java.lang.String[] command, java.lang.String encoding, java.util.Map<java.lang.String,​java.lang.String> env)
      Execute a command and return a single line of output as a String
      java.lang.String readSymLink​(java.io.File path)
      Check if a file is a symbolic link and read it
      java.lang.String relativize​(java.lang.String base, java.lang.String other)
      java.io.File resolve​(java.io.File dir, java.lang.String name)
      Resolve this file to its actual path name that the JRE can use.
      protected static java.io.File resolveGrandparentFile​(java.io.File grandchild)
      Get the parent directory of this file's parent directory
      abstract boolean retryFailedLockFileCommit()
      Does this file system have problems with atomic renames?
      ProcessResult runHookIfPresent​(Repository repository, java.lang.String hookName, java.lang.String[] args)
      Checks whether the given hook is defined for the given repository, then runs it with the given arguments.
      ProcessResult runHookIfPresent​(Repository repository, java.lang.String hookName, java.lang.String[] args, java.io.PrintStream outRedirect, java.io.PrintStream errRedirect, java.lang.String stdinArgs)
      Checks whether the given hook is defined for the given repository, then runs it with the given arguments.
      abstract java.lang.ProcessBuilder runInShell​(java.lang.String cmd, java.lang.String[] args)
      Initialize a ProcessBuilder to run a command using the system shell.
      int runProcess​(java.lang.ProcessBuilder processBuilder, java.io.OutputStream outRedirect, java.io.OutputStream errRedirect, java.io.InputStream inRedirect)
      Runs the given process until termination, clearing its stdout and stderr streams on-the-fly.
      int runProcess​(java.lang.ProcessBuilder processBuilder, java.io.OutputStream outRedirect, java.io.OutputStream errRedirect, java.lang.String stdinArgs)
      Runs the given process until termination, clearing its stdout and stderr streams on-the-fly.
      private java.io.File safeUserHomeImpl()  
      protected static java.io.File searchPath​(java.lang.String path, java.lang.String... lookFor)
      Searches the given path to see if it contains one of the given files.
      static void setAsyncFileStoreAttributes​(boolean asynch)
      abstract boolean setExecute​(java.io.File f, boolean canExec)
      Set a file to be executable by the user.
      FS setGitSystemConfig​(java.io.File configFile)
      Set the path to the system-wide Git configuration file to use.
      void setHidden​(java.io.File path, boolean hidden)
      Set the hidden attribute for file whose name starts with a period.
      void setLastModified​(java.io.File f, long time)
      Deprecated.
      void setLastModified​(java.nio.file.Path p, java.time.Instant time)
      Set the last modified time of a file system object.
      FS setUserHome​(java.io.File path)
      Set the user's home directory location.
      (package private) java.lang.String shellQuote​(java.lang.String cmd)
      Quote a string (such as a file system path obtained from a Java File or Path object) such that it can be passed as first argument to runInShell(String, String[]).
      private static boolean shutdownAndAwaitTermination​(java.util.concurrent.ExecutorService pool)
      Shuts down an ExecutorService in two phases, first by calling shutdown to reject incoming tasks, and then calling shutdownNow, if necessary, to cancel any lingering tasks.
      boolean supportsAtomicCreateNewFile()
      Does this file system support atomic file creation via java.io.File#createNewFile()? In certain environments (e.g.
      abstract boolean supportsExecute()
      Does this operating system and JRE support the execute flag on files?
      boolean supportsSymlinks()
      Does this operating system and JRE supports symbolic links.
      java.io.File userHome()
      Determine the user's home directory (location where preferences are).
      protected java.io.File userHomeImpl()
      Determine the user's home directory (location where preferences are).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
      • supportSymlinks

        private volatile java.lang.Boolean supportSymlinks
      • DETECTED

        public static final FS DETECTED
        The auto-detected implementation selected for this operating system and JRE.
      • userHome

        private volatile FS.Holder<java.io.File> userHome
      • gitSystemConfig

        private volatile FS.Holder<java.io.File> gitSystemConfig
    • Constructor Detail

      • FS

        protected FS()
        Constructs a file system abstraction.
      • FS

        protected FS​(FS src)
        Initialize this FS using another's current settings.
        Parameters:
        src - the source FS to copy from.
    • Method Detail

      • detect

        public static FS detect()
        Auto-detect the appropriate file system abstraction.
        Returns:
        detected file system abstraction
      • setAsyncFileStoreAttributes

        @Deprecated
        public static void setAsyncFileStoreAttributes​(boolean asynch)
        Whether FileStore attributes should be determined asynchronously
        Parameters:
        asynch - whether FileStore attributes should be determined asynchronously. If false access to cached attributes may block for some seconds for the first call per FileStore
        Since:
        5.1.9
      • detect

        public static FS detect​(java.lang.Boolean cygwinUsed)
        Auto-detect the appropriate file system abstraction, taking into account the presence of a Cygwin installation on the system. Using jgit in combination with Cygwin requires a more elaborate (and possibly slower) resolution of file system paths.
        Parameters:
        cygwinUsed -
        • Boolean.TRUE to assume that Cygwin is used in combination with jgit
        • Boolean.FALSE to assume that Cygwin is not used with jgit
        • null to auto-detect whether a Cygwin installation is present on the system and in this case assume that Cygwin is used
        Note: this parameter is only relevant on Windows.
        Returns:
        detected file system abstraction
      • getFileStoreAttributes

        public static FS.FileStoreAttributes getFileStoreAttributes​(@NonNull
                                                                    java.nio.file.Path dir)
        Get cached FileStore attributes, if not yet available measure them using a probe file under the given directory.
        Parameters:
        dir - the directory under which the probe file will be created to measure the timer resolution.
        Returns:
        measured filesystem timestamp resolution
        Since:
        5.1.9
      • newInstance

        public abstract FS newInstance()
        Create a new instance of the same type of FS.
        Returns:
        a new instance of the same type of FS.
      • supportsExecute

        public abstract boolean supportsExecute()
        Does this operating system and JRE support the execute flag on files?
        Returns:
        true if this implementation can provide reasonably accurate executable bit information; false otherwise.
      • supportsAtomicCreateNewFile

        public boolean supportsAtomicCreateNewFile()
        Does this file system support atomic file creation via java.io.File#createNewFile()? In certain environments (e.g. on NFS) it is not guaranteed that when two file system clients run createNewFile() in parallel only one will succeed. In such cases both clients may think they created a new file.
        Returns:
        true if this implementation support atomic creation of new Files by File.createNewFile()
        Since:
        4.5
      • supportsSymlinks

        public boolean supportsSymlinks()
        Does this operating system and JRE supports symbolic links. The capability to handle symbolic links is detected at runtime.
        Returns:
        true if symbolic links may be used
        Since:
        3.0
      • detectSymlinkSupport

        private void detectSymlinkSupport()
      • isCaseSensitive

        public abstract boolean isCaseSensitive()
        Is this file system case sensitive
        Returns:
        true if this implementation is case sensitive
      • canExecute

        public abstract boolean canExecute​(java.io.File f)
        Determine if the file is executable (or not).

        Not all platforms and JREs support executable flags on files. If the feature is unsupported this method will always return false.

        If the platform supports symbolic links and f is a symbolic link this method returns false, rather than the state of the executable flags on the target file.

        Parameters:
        f - abstract path to test.
        Returns:
        true if the file is believed to be executable by the user.
      • setExecute

        public abstract boolean setExecute​(java.io.File f,
                                           boolean canExec)
        Set a file to be executable by the user.

        Not all platforms and JREs support executable flags on files. If the feature is unsupported this method will always return false and no changes will be made to the file specified.

        Parameters:
        f - path to modify the executable status of.
        canExec - true to enable execution; false to disable it.
        Returns:
        true if the change succeeded; false otherwise.
      • lastModified

        @Deprecated
        public long lastModified​(java.io.File f)
                          throws java.io.IOException
        Deprecated.
        Get the last modified time of a file system object. If the OS/JRE support symbolic links, the modification time of the link is returned, rather than that of the link target.
        Parameters:
        f - a File object.
        Returns:
        last modified time of f
        Throws:
        java.io.IOException
        Since:
        3.0
      • lastModifiedInstant

        public java.time.Instant lastModifiedInstant​(java.nio.file.Path p)
        Get the last modified time of a file system object. If the OS/JRE support symbolic links, the modification time of the link is returned, rather than that of the link target.
        Parameters:
        p - a Path object.
        Returns:
        last modified time of p
        Since:
        5.1.9
      • lastModifiedInstant

        public java.time.Instant lastModifiedInstant​(java.io.File f)
        Get the last modified time of a file system object. If the OS/JRE support symbolic links, the modification time of the link is returned, rather than that of the link target.
        Parameters:
        f - a File object.
        Returns:
        last modified time of p
        Since:
        5.1.9
      • setLastModified

        @Deprecated
        public void setLastModified​(java.io.File f,
                                    long time)
                             throws java.io.IOException
        Deprecated.
        Set the last modified time of a file system object.

        For symlinks it sets the modified time of the link target.

        Parameters:
        f - a File object.
        time - last modified time
        Throws:
        java.io.IOException
        Since:
        3.0
      • setLastModified

        public void setLastModified​(java.nio.file.Path p,
                                    java.time.Instant time)
                             throws java.io.IOException
        Set the last modified time of a file system object.

        For symlinks it sets the modified time of the link target.

        Parameters:
        p - a Path object.
        time - last modified time
        Throws:
        java.io.IOException
        Since:
        5.1.9
      • length

        public long length​(java.io.File path)
                    throws java.io.IOException
        Get the length of a file or link, If the OS/JRE supports symbolic links it's the length of the link, else the length of the target.
        Parameters:
        path - a File object.
        Returns:
        length of a file
        Throws:
        java.io.IOException
        Since:
        3.0
      • delete

        public void delete​(java.io.File f)
                    throws java.io.IOException
        Delete a file. Throws an exception if delete fails.
        Parameters:
        f - a File object.
        Throws:
        java.io.IOException - this may be a Java7 subclass with detailed information
        Since:
        3.3
      • resolve

        public java.io.File resolve​(java.io.File dir,
                                    java.lang.String name)
        Resolve this file to its actual path name that the JRE can use.

        This method can be relatively expensive. Computing a translation may require forking an external process per path name translated. Callers should try to minimize the number of translations necessary by caching the results.

        Not all platforms and JREs require path name translation. Currently only Cygwin on Win32 require translation for Cygwin based paths.

        Parameters:
        dir - directory relative to which the path name is.
        name - path name to translate.
        Returns:
        the translated path. new File(dir,name) if this platform does not require path name translation.
      • userHome

        public java.io.File userHome()
        Determine the user's home directory (location where preferences are).

        This method can be expensive on the first invocation if path name translation is required. Subsequent invocations return a cached result.

        Not all platforms and JREs require path name translation. Currently only Cygwin on Win32 requires translation of the Cygwin HOME directory.

        Returns:
        the user's home directory; null if the user does not have one.
      • safeUserHomeImpl

        private java.io.File safeUserHomeImpl()
      • setUserHome

        public FS setUserHome​(java.io.File path)
        Set the user's home directory location.
        Parameters:
        path - the location of the user's preferences; null if there is no home directory for the current user.
        Returns:
        this.
      • retryFailedLockFileCommit

        public abstract boolean retryFailedLockFileCommit()
        Does this file system have problems with atomic renames?
        Returns:
        true if the caller should retry a failed rename of a lock file.
      • fileAttributes

        public java.nio.file.attribute.BasicFileAttributes fileAttributes​(java.io.File file)
                                                                   throws java.io.IOException
        Return all the attributes of a file, without following symbolic links.
        Parameters:
        file -
        Returns:
        BasicFileAttributes of the file
        Throws:
        java.io.IOException - in case of any I/O errors accessing the file
        Since:
        4.5.6
      • userHomeImpl

        protected java.io.File userHomeImpl()
        Determine the user's home directory (location where preferences are).
        Returns:
        the user's home directory; null if the user does not have one.
      • defaultUserHomeImpl

        private java.io.File defaultUserHomeImpl()
      • searchPath

        protected static java.io.File searchPath​(java.lang.String path,
                                                 java.lang.String... lookFor)
        Searches the given path to see if it contains one of the given files. Returns the first it finds. Returns null if not found or if path is null.
        Parameters:
        path - List of paths to search separated by File.pathSeparator
        lookFor - Files to search for in the given path
        Returns:
        the first match found, or null
        Since:
        3.0
      • readPipe

        @Nullable
        protected static java.lang.String readPipe​(java.io.File dir,
                                                   java.lang.String[] command,
                                                   java.lang.String encoding)
                                            throws CommandFailedException
        Execute a command and return a single line of output as a String
        Parameters:
        dir - Working directory for the command
        command - as component array
        encoding - to be used to parse the command's output
        Returns:
        the one-line output of the command or null if there is none
        Throws:
        CommandFailedException - thrown when the command failed (return code was non-zero)
      • readPipe

        @Nullable
        protected static java.lang.String readPipe​(java.io.File dir,
                                                   java.lang.String[] command,
                                                   java.lang.String encoding,
                                                   java.util.Map<java.lang.String,​java.lang.String> env)
                                            throws CommandFailedException
        Execute a command and return a single line of output as a String
        Parameters:
        dir - Working directory for the command
        command - as component array
        encoding - to be used to parse the command's output
        env - Map of environment variables to be merged with those of the current process
        Returns:
        the one-line output of the command or null if there is none
        Throws:
        CommandFailedException - thrown when the command failed (return code was non-zero)
        Since:
        4.0
      • discoverGitExe

        protected abstract java.io.File discoverGitExe()
        Discover the path to the Git executable.
        Returns:
        the path to the Git executable or null if it cannot be determined.
        Since:
        4.0
      • discoverGitSystemConfig

        protected java.io.File discoverGitSystemConfig()
        Discover the path to the system-wide Git configuration file
        Returns:
        the path to the system-wide Git configuration file or null if it cannot be determined.
        Since:
        4.0
      • getGitSystemConfig

        public java.io.File getGitSystemConfig()
        Get the currently used path to the system-wide Git configuration file.
        Returns:
        the currently used path to the system-wide Git configuration file or null if none has been set.
        Since:
        4.0
      • setGitSystemConfig

        public FS setGitSystemConfig​(java.io.File configFile)
        Set the path to the system-wide Git configuration file to use.
        Parameters:
        configFile - the path to the config file.
        Returns:
        this
        Since:
        4.0
      • resolveGrandparentFile

        protected static java.io.File resolveGrandparentFile​(java.io.File grandchild)
        Get the parent directory of this file's parent directory
        Parameters:
        grandchild - a File object.
        Returns:
        the parent directory of this file's parent directory or null in case there's no grandparent directory
        Since:
        4.0
      • readSymLink

        public java.lang.String readSymLink​(java.io.File path)
                                     throws java.io.IOException
        Check if a file is a symbolic link and read it
        Parameters:
        path - a File object.
        Returns:
        target of link or null
        Throws:
        java.io.IOException
        Since:
        3.0
      • isSymLink

        public boolean isSymLink​(java.io.File path)
                          throws java.io.IOException
        Whether the path is a symbolic link (and we support these).
        Parameters:
        path - a File object.
        Returns:
        true if the path is a symbolic link (and we support these)
        Throws:
        java.io.IOException
        Since:
        3.0
      • exists

        public boolean exists​(java.io.File path)
        Tests if the path exists, in case of a symbolic link, true even if the target does not exist
        Parameters:
        path - a File object.
        Returns:
        true if path exists
        Since:
        3.0
      • isDirectory

        public boolean isDirectory​(java.io.File path)
        Check if path is a directory. If the OS/JRE supports symbolic links and path is a symbolic link to a directory, this method returns false.
        Parameters:
        path - a File object.
        Returns:
        true if file is a directory,
        Since:
        3.0
      • isFile

        public boolean isFile​(java.io.File path)
        Examine if path represents a regular file. If the OS/JRE supports symbolic links the test returns false if path represents a symbolic link.
        Parameters:
        path - a File object.
        Returns:
        true if path represents a regular file
        Since:
        3.0
      • isHidden

        public boolean isHidden​(java.io.File path)
                         throws java.io.IOException
        Whether path is hidden, either starts with . on unix or has the hidden attribute in windows
        Parameters:
        path - a File object.
        Returns:
        true if path is hidden, either starts with . on unix or has the hidden attribute in windows
        Throws:
        java.io.IOException
        Since:
        3.0
      • setHidden

        public void setHidden​(java.io.File path,
                              boolean hidden)
                       throws java.io.IOException
        Set the hidden attribute for file whose name starts with a period.
        Parameters:
        path - a File object.
        hidden - whether to set the file hidden
        Throws:
        java.io.IOException
        Since:
        3.0
      • createSymLink

        public void createSymLink​(java.io.File path,
                                  java.lang.String target)
                           throws java.io.IOException
        Create a symbolic link
        Parameters:
        path - a File object.
        target - target path of the symlink
        Throws:
        java.io.IOException
        Since:
        3.0
      • createNewFile

        @Deprecated
        public boolean createNewFile​(java.io.File path)
                              throws java.io.IOException
        Deprecated.
        Create a new file. See File.createNewFile(). Subclasses of this class may take care to provide a safe implementation for this even if supportsAtomicCreateNewFile() is false
        Parameters:
        path - the file to be created
        Returns:
        true if the file was created, false if the file already existed
        Throws:
        java.io.IOException
        Since:
        4.5
      • createNewFileAtomic

        public FS.LockToken createNewFileAtomic​(java.io.File path)
                                         throws java.io.IOException
        Create a new file. See File.createNewFile(). Subclasses of this class may take care to provide a safe implementation for this even if supportsAtomicCreateNewFile() is false
        Parameters:
        path - the file to be created
        Returns:
        LockToken this token must be closed after the created file was deleted
        Throws:
        java.io.IOException
        Since:
        4.7
      • list

        public WorkingTreeIterator.Entry[] list​(java.io.File directory,
                                                FileTreeIterator.FileModeStrategy fileModeStrategy)
        Enumerates children of a directory.
        Parameters:
        directory - to get the children of
        fileModeStrategy - to use to calculate the git mode of a child
        Returns:
        an array of entries for the children
        Since:
        5.0
      • runHookIfPresent

        public ProcessResult runHookIfPresent​(Repository repository,
                                              java.lang.String hookName,
                                              java.lang.String[] args)
                                       throws JGitInternalException
        Checks whether the given hook is defined for the given repository, then runs it with the given arguments.

        The hook's standard output and error streams will be redirected to System.out and System.err respectively. The hook will have no stdin.

        Parameters:
        repository - The repository for which a hook should be run.
        hookName - The name of the hook to be executed.
        args - Arguments to pass to this hook. Cannot be null, but can be an empty array.
        Returns:
        The ProcessResult describing this hook's execution.
        Throws:
        JGitInternalException - if we fail to run the hook somehow. Causes may include an interrupted process or I/O errors.
        Since:
        4.0
      • runHookIfPresent

        public ProcessResult runHookIfPresent​(Repository repository,
                                              java.lang.String hookName,
                                              java.lang.String[] args,
                                              java.io.PrintStream outRedirect,
                                              java.io.PrintStream errRedirect,
                                              java.lang.String stdinArgs)
                                       throws JGitInternalException
        Checks whether the given hook is defined for the given repository, then runs it with the given arguments.
        Parameters:
        repository - The repository for which a hook should be run.
        hookName - The name of the hook to be executed.
        args - Arguments to pass to this hook. Cannot be null, but can be an empty array.
        outRedirect - A print stream on which to redirect the hook's stdout. Can be null, in which case the hook's standard output will be lost.
        errRedirect - A print stream on which to redirect the hook's stderr. Can be null, in which case the hook's standard error will be lost.
        stdinArgs - A string to pass on to the standard input of the hook. May be null.
        Returns:
        The ProcessResult describing this hook's execution.
        Throws:
        JGitInternalException - if we fail to run the hook somehow. Causes may include an interrupted process or I/O errors.
        Since:
        4.0
      • internalRunHookIfPresent

        protected ProcessResult internalRunHookIfPresent​(Repository repository,
                                                         java.lang.String hookName,
                                                         java.lang.String[] args,
                                                         java.io.PrintStream outRedirect,
                                                         java.io.PrintStream errRedirect,
                                                         java.lang.String stdinArgs)
                                                  throws JGitInternalException
        See runHookIfPresent(Repository, String, String[], PrintStream, PrintStream, String) . Should only be called by FS supporting shell scripts execution.
        Parameters:
        repository - The repository for which a hook should be run.
        hookName - The name of the hook to be executed.
        args - Arguments to pass to this hook. Cannot be null, but can be an empty array.
        outRedirect - A print stream on which to redirect the hook's stdout. Can be null, in which case the hook's standard output will be lost.
        errRedirect - A print stream on which to redirect the hook's stderr. Can be null, in which case the hook's standard error will be lost.
        stdinArgs - A string to pass on to the standard input of the hook. May be null.
        Returns:
        The ProcessResult describing this hook's execution.
        Throws:
        JGitInternalException - if we fail to run the hook somehow. Causes may include an interrupted process or I/O errors.
        Since:
        4.0
      • shellQuote

        java.lang.String shellQuote​(java.lang.String cmd)
        Quote a string (such as a file system path obtained from a Java File or Path object) such that it can be passed as first argument to runInShell(String, String[]).

        This default implementation returns the string unchanged.

        Parameters:
        cmd - the String to quote
        Returns:
        the quoted string
      • findHook

        public java.io.File findHook​(Repository repository,
                                     java.lang.String hookName)
        Tries to find a hook matching the given one in the given repository.
        Parameters:
        repository - The repository within which to find a hook.
        hookName - The name of the hook we're trying to find.
        Returns:
        The File containing this particular hook if it exists in the given repository, null otherwise.
        Since:
        4.0
      • getRunDirectory

        private java.io.File getRunDirectory​(Repository repository,
                                             @NonNull
                                             java.lang.String hookName)
      • getHooksDirectory

        private java.io.File getHooksDirectory​(Repository repository)
      • runProcess

        public int runProcess​(java.lang.ProcessBuilder processBuilder,
                              java.io.OutputStream outRedirect,
                              java.io.OutputStream errRedirect,
                              java.lang.String stdinArgs)
                       throws java.io.IOException,
                              java.lang.InterruptedException
        Runs the given process until termination, clearing its stdout and stderr streams on-the-fly.
        Parameters:
        processBuilder - The process builder configured for this process.
        outRedirect - A OutputStream on which to redirect the processes stdout. Can be null, in which case the processes standard output will be lost.
        errRedirect - A OutputStream on which to redirect the processes stderr. Can be null, in which case the processes standard error will be lost.
        stdinArgs - A string to pass on to the standard input of the hook. Can be null.
        Returns:
        the exit value of this process.
        Throws:
        java.io.IOException - if an I/O error occurs while executing this process.
        java.lang.InterruptedException - if the current thread is interrupted while waiting for the process to end.
        Since:
        4.2
      • runProcess

        public int runProcess​(java.lang.ProcessBuilder processBuilder,
                              java.io.OutputStream outRedirect,
                              java.io.OutputStream errRedirect,
                              java.io.InputStream inRedirect)
                       throws java.io.IOException,
                              java.lang.InterruptedException
        Runs the given process until termination, clearing its stdout and stderr streams on-the-fly.
        Parameters:
        processBuilder - The process builder configured for this process.
        outRedirect - An OutputStream on which to redirect the processes stdout. Can be null, in which case the processes standard output will be lost.
        errRedirect - An OutputStream on which to redirect the processes stderr. Can be null, in which case the processes standard error will be lost.
        inRedirect - An InputStream from which to redirect the processes stdin. Can be null, in which case the process doesn't get any data over stdin. It is assumed that the whole InputStream will be consumed by the process. The method will close the inputstream after all bytes are read.
        Returns:
        the return code of this process.
        Throws:
        java.io.IOException - if an I/O error occurs while executing this process.
        java.lang.InterruptedException - if the current thread is interrupted while waiting for the process to end.
        Since:
        4.2
      • shutdownAndAwaitTermination

        private static boolean shutdownAndAwaitTermination​(java.util.concurrent.ExecutorService pool)
        Shuts down an ExecutorService in two phases, first by calling shutdown to reject incoming tasks, and then calling shutdownNow, if necessary, to cancel any lingering tasks. Returns true if the pool has been properly shutdown, false otherwise.

        Parameters:
        pool - the pool to shutdown
        Returns:
        true if the pool has been properly shutdown, false otherwise.
      • runInShell

        public abstract java.lang.ProcessBuilder runInShell​(java.lang.String cmd,
                                                            java.lang.String[] args)
        Initialize a ProcessBuilder to run a command using the system shell.
        Parameters:
        cmd - command to execute. This string should originate from the end-user, and thus is platform specific.
        args - arguments to pass to command. These should be protected from shell evaluation.
        Returns:
        a partially completed process builder. Caller should finish populating directory, environment, and then start the process.
      • execute

        public FS.ExecutionResult execute​(java.lang.ProcessBuilder pb,
                                          java.io.InputStream in)
                                   throws java.io.IOException,
                                          java.lang.InterruptedException
        Execute a command defined by a ProcessBuilder.
        Parameters:
        pb - The command to be executed
        in - The standard input stream passed to the process
        Returns:
        The result of the executed command
        Throws:
        java.lang.InterruptedException
        java.io.IOException
        Since:
        4.2
      • getAttributes

        public FS.Attributes getAttributes​(java.io.File path)
        Get the file attributes we care for.
        Parameters:
        path - a File object.
        Returns:
        the file attributes we care for.
        Since:
        3.3
      • normalize

        public java.io.File normalize​(java.io.File file)
        Normalize the unicode path to composed form.
        Parameters:
        file - a File object.
        Returns:
        NFC-format File
        Since:
        3.3
      • normalize

        public java.lang.String normalize​(java.lang.String name)
        Normalize the unicode path to composed form.
        Parameters:
        name - path name
        Returns:
        NFC-format string
        Since:
        3.3