@Test public void testNewRepo() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git git = JGitUtil.newRepository( gitFolder, true ); assertThat( git ).isNotNull(); assertThat( branchList( git ).size() ).isEqualTo( 0 ); commit( git, "master", "name", "name@example.com", "commit", null, null, false, new HashMap<String, File>() {{ put( "file.txt", tempFile( "temp" ) ); }} ); assertThat( branchList( git ).size() ).isEqualTo( 1 ); }
@Test public void testAmend() throws IOException { final File parentFolder = createTempDirectory(); System.out.println("COOL!:" + parentFolder.toString()); final File gitFolder = new File( parentFolder, "myxxxtest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "name", "name@example.com", "commit!", null, null, false, new HashMap<String, File>() {{ put( "path/to/file2.txt", tempFile( "tempwdf sdf asdf asd2222" ) ); }} ); commit( origin, "master", "name", "name@example.com", "commit!", null, null, true, new HashMap<String, File>() {{ put( "path/to/file3.txt", tempFile( "temp2x d dasdf asdf 222" ) ); }} ); final File gitClonedFolder = new File( parentFolder, "myclone.git" ); final Git git = cloneRepository( gitClonedFolder, origin.getRepository().getDirectory().toString(), true, CredentialsProvider.getDefault() ); assertThat( JGitUtil.checkPath( git, "master", "pathx/" ).getK1() ).isEqualTo( NOT_FOUND ); assertThat( JGitUtil.checkPath( git, "master", "path/to/file2.txt" ).getK1() ).isEqualTo( FILE ); assertThat( JGitUtil.checkPath( git, "master", "path/to" ).getK1() ).isEqualTo( DIRECTORY ); }
@Override public VersionAttributes readAttributes() throws IOException { if ( attrs == null ) { attrs = JGitUtil.buildVersionAttributes( path.getFileSystem(), path.getRefTree(), path.getPath() ); } return attrs; }
final String originURI = env.get( GIT_DEFAULT_REMOTE_NAME ).toString(); credential = buildCredential( env ); git = cloneRepository( repoDest, originURI, bare, credential ); } else { credential = buildCredential( null ); git = newRepository( repoDest, bare );
@Test(expected = NoSuchFileException.class) public void testInputStream3() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "path/to/file/myfile.txt", tempFile( "temp\n.origin\n.content" ) ); }} ); final URI newRepo = URI.create( "git://xxinputstream-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@xxinputstream-test-repo/path/to" ) ); PROVIDER.newInputStream( path ); }
@Test public void testClone() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "user_branch", "name", "name@example.com", "commit!", null, null, false, new HashMap<String, File>() {{ put( "file2.txt", tempFile( "temp2222" ) ); }} ); commit( origin, "master", "name", "name@example.com", "commit", null, null, false, new HashMap<String, File>() {{ put( "file.txt", tempFile( "temp" ) ); }} ); commit( origin, "master", "name", "name@example.com", "commit", null, null, false, new HashMap<String, File>() {{ put( "file3.txt", tempFile( "temp3" ) ); }} ); final File gitClonedFolder = new File( parentFolder, "myclone.git" ); final Git git = cloneRepository( gitClonedFolder, origin.getRepository().getDirectory().toString(), true, CredentialsProvider.getDefault() ); assertThat( git ).isNotNull(); assertThat( branchList( git, ALL ) ).hasSize( 4 ); assertThat( branchList( git, ALL ).get( 0 ).getName() ).isEqualTo( "refs/heads/master" ); assertThat( branchList( git, ALL ).get( 1 ).getName() ).isEqualTo( "refs/heads/user_branch" ); assertThat( branchList( git, ALL ).get( 2 ).getName() ).isEqualTo( "refs/remotes/origin/master" ); assertThat( branchList( git, ALL ).get( 3 ).getName() ).isEqualTo( "refs/remotes/origin/user_branch" ); }
final ObjectId oldHead = JGitUtil.getTreeRefObjectId( path.getFileSystem().gitRepo().getRepository(), branchName ); commit( git, branchName, name, email, message, timeZone, when, amend, content ); final ObjectId newHead = JGitUtil.getTreeRefObjectId( path.getFileSystem().gitRepo().getRepository(), branchName ); notifyDiffs( path.getFileSystem(), branchName, sessionId, name, oldHead, newHead ); final Map<String, File> content ) { final PersonIdent author = buildPersonIdent( git, name, email, timeZone, when ); // Create the in-memory index of the new/updated issue. final ObjectId headId = git.getRepository().resolve( branchName + "^{commit}" ); final DirCache index = createTemporaryIndex( git, headId, content ); final ObjectId indexTreeId = index.writeTree( odi );
@Override public void createDirectory( final Path path, final FileAttribute<?>... attrs ) throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException { checkNotNull( "path", path ); final JGitPathImpl gPath = toPathImpl( path ); final Pair<PathType, ObjectId> result = checkPath( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() ); if ( !result.getK1().equals( NOT_FOUND ) ) { throw new FileAlreadyExistsException( path.toString() ); } try { final OutputStream outputStream = newOutputStream( path.resolve( ".gitignore" ) ); outputStream.write( "# empty\n".getBytes() ); outputStream.close(); } catch ( final Exception e ) { throw new IOException( e ); } }
public boolean deleteAssetIfExists( final JGitPathImpl path, final DeleteOption... options ) { final Pair<PathType, ObjectId> result = checkPath( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); if ( result.getK1().equals( PathType.DIRECTORY ) ) { if ( deleteNonEmptyDirectory( options ) ) { deleteResource( path, options ); return true; } final List<JGitPathInfo> content = listPathContent( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); if ( content.size() == 1 && content.get( 0 ).getPath().equals( path.getPath().substring( 1 ) + "/.gitignore" ) ) { delete( path.resolve( ".gitignore" ) ); return true; } throw new DirectoryNotEmptyException( path.toString() ); } if ( result.getK1().equals( NOT_FOUND ) ) { return false; } deleteResource( path, options ); return true; }
public static void delete( final JGitPathImpl path, final String sessionId, final String name, final String email, final String message, final TimeZone timeZone, final Date when, final boolean amend ) { commit( path, sessionId, name, email, message, timeZone, when, amend, new HashMap<String, File>() {{ put( path.getPath(), null ); }} ); }
public static List<Ref> branchList( final Git git ) { checkNotNull( "git", git ); return branchList( git, null ); }
public JGitFileSystemProvider() { fullHostName = DAEMON_ENABLED ? DAEMON_HOST + ":" + DAEMON_PORT : null; final String[] repos = FILE_REPOSITORIES_ROOT.list( new FilenameFilter() { @Override public boolean accept( final File dir, String name ) { return name.endsWith( DOT_GIT_EXT ); } } ); if ( repos != null ) { for ( final String repo : repos ) { final File repoDir = new File( FILE_REPOSITORIES_ROOT, repo ); if ( repoDir.isDirectory() ) { final String name = repoDir.getName().substring( 0, repoDir.getName().indexOf( DOT_GIT_EXT ) ); final JGitFileSystem fs = new JGitFileSystem( this, fullHostName, newRepository( repoDir, true ), name, ALL, buildCredential( null ) ); fileSystems.put( name, fs ); repoIndex.put( fs.gitRepo().getRepository(), fs ); } } } if ( DAEMON_ENABLED ) { buildAndStartDaemon(); } else { daemonService = null; } }
private void createBranch( final JGitPathImpl source, final JGitPathImpl target ) { JGitUtil.createBranch( source.getFileSystem().gitRepo(), source.getRefTree(), target.getRefTree() ); }
void deleteResource( final JGitPathImpl path, final DeleteOption... options ) { String sessionId = null; String name = null; String email = null; String message = "delete {" + path.getPath() + "}"; TimeZone timeZone = null; Date when = null; if ( options != null && options.length > 0 ) { for ( final DeleteOption option : options ) { if ( option instanceof CommentedOption ) { final CommentedOption op = (CommentedOption) option; sessionId = op.getSessionId(); name = op.getName(); email = op.getEmail(); message = op.getMessage(); timeZone = op.getTimeZone(); when = op.getWhen(); break; } } } JGitUtil.delete( path, sessionId, name, email, message, timeZone, when, amend() ); checkAmend(); }
@Test(expected = NoSuchFileException.class) public void testInputStreamNoSuchFile() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user1", "user1@example.com", "commitx", null, null, false, new HashMap<String, File>() {{ put( "file.txt", tempFile( "temp.origin.content.2" ) ); }} ); final URI newRepo = URI.create( "git://inputstream-not-exists-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@inputstream-not-exists-test-repo/temp.txt" ) ); PROVIDER.newInputStream( path ); }
@Override public void checkAccess( final Path path, final AccessMode... modes ) throws UnsupportedOperationException, NoSuchFileException, AccessDeniedException, IOException, SecurityException { checkNotNull( "path", path ); final JGitPathImpl gPath = toPathImpl( path ); final Pair<PathType, ObjectId> result = checkPath( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() ); if ( result.getK1().equals( NOT_FOUND ) ) { throw new NoSuchFileException( path.toString() ); } }
public void deleteAsset( final JGitPathImpl path, final DeleteOption... options ) { final Pair<PathType, ObjectId> result = checkPath( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); if ( result.getK1().equals( PathType.DIRECTORY ) ) { if ( deleteNonEmptyDirectory( options ) ) { deleteResource( path, options ); return; } final List<JGitPathInfo> content = listPathContent( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); if ( content.size() == 1 && content.get( 0 ).getPath().equals( path.getPath().substring( 1 ) + "/.gitignore" ) ) { delete( path.resolve( ".gitignore" ) ); deleteResource( path, options ); return; } throw new DirectoryNotEmptyException( path.toString() ); } if ( result.getK1().equals( NOT_FOUND ) ) { throw new NoSuchFileException( path.toString() ); } deleteResource( path, options ); }
public void close() throws java.io.IOException { super.close(); String sessionId = null; String name = null; String email = null; String message = null; TimeZone timeZone = null; Date when = null; if ( options != null && options.length > 0 ) { for ( final OpenOption option : options ) { if ( option instanceof CommentedOption ) { final CommentedOption op = (CommentedOption) option; sessionId = op.getSessionId(); name = op.getName(); email = op.getEmail(); message = op.getMessage(); timeZone = op.getTimeZone(); when = op.getWhen(); break; } } } commit( gPath, sessionId, name, email, message, timeZone, when, amend(), new HashMap<String, File>() {{ put( gPath.getPath(), file ); }} ); checkAmend(); } };
@Test public void testPathResolve() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "user_branch", "name", "name@example.com", "commit!", null, null, false, new HashMap<String, File>() {{ put( "path/to/file2.txt", tempFile( "temp2222" ) ); }} ); commit( origin, "user_branch", "name", "name@example.com", "commit!", null, null, false, new HashMap<String, File>() {{ put( "path/to/file3.txt", tempFile( "temp2222" ) ); }} ); final File gitClonedFolder = new File( parentFolder, "myclone.git" ); final Git git = cloneRepository( gitClonedFolder, origin.getRepository().getDirectory().toString(), true, CredentialsProvider.getDefault() ); assertThat( JGitUtil.checkPath( git, "user_branch", "pathx/" ).getK1() ).isEqualTo( NOT_FOUND ); assertThat( JGitUtil.checkPath( git, "user_branch", "path/to/file2.txt" ).getK1() ).isEqualTo( FILE ); assertThat( JGitUtil.checkPath( git, "user_branch", "path/to" ).getK1() ).isEqualTo( DIRECTORY ); }
@Test public void testNewOutputStreamWithJGitOp() throws Exception { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "myfile.txt", tempFile( "temp\n.origin\n.content" ) ); }} ); commit( origin, "user_branch", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "path/to/some/file/myfile.txt", tempFile( "some\n.content\nhere" ) ); }} ); final URI newRepo = URI.create( "git://outstreamwithop-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final SimpleDateFormat formatter = new SimpleDateFormat( "dd/MM/yyyy" ); final CommentedOption op = new CommentedOption( "User Tester", "user.tester@example.com", "omg, is it the end?", formatter.parse( "31/12/2012" ) ); final Path path = PROVIDER.getPath( URI.create( "git://user_branch@outstreamwithop-test-repo/some/path/myfile.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path, op ); assertThat( outStream ).isNotNull(); outStream.write( "my cool content".getBytes() ); outStream.close(); final InputStream inStream = PROVIDER.newInputStream( path ); final String content = new Scanner( inStream ).useDelimiter( "\\A" ).next(); inStream.close(); assertThat( content ).isNotNull().isEqualTo( "my cool content" ); }