public static File getRelativePath ( File in , File repositoryPath ) throws JavaGitException { String path = in . getPath ( ) ; String absolutePath = in . getAbsolutePath ( ) ; //check if the path is relative or absolute if ( path . equals ( absolutePath ) ) { //if absolute, make sure it belongs to working tree String workingTreePath = repositoryPath . getAbsolutePath ( ) ; if ( ! path . startsWith ( workingTreePath ) ) { throw new JavaGitException ( 999 , "Invalid path :" + path + ". Does not belong to the git working tree/ repository: " + workingTreePath ) ; } //make path relative if ( ! path . equals ( workingTreePath ) ) { path = path . substring ( workingTreePath . length ( ) + 1 ) ; } } return new File ( path ) ; }
Returns a file with path relative to git working tree