Skip to content

Commit 4905225

Browse files
committed
MastodonGitRepository: refactoring
Minor refactoring use try with resource. Extract a private method. Reduce code duplication.
1 parent 785ff77 commit 4905225

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

src/main/java/org/mastodon/mamut/collaboration/MastodonGitRepository.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.eclipse.jgit.api.Git;
4343
import org.eclipse.jgit.api.ListBranchCommand;
4444
import org.eclipse.jgit.api.ResetCommand;
45+
import org.eclipse.jgit.api.errors.GitAPIException;
4546
import org.eclipse.jgit.lib.BranchConfig;
4647
import org.eclipse.jgit.lib.ConfigConstants;
4748
import org.eclipse.jgit.lib.Ref;
@@ -112,35 +113,44 @@ public static MastodonGitRepository shareProject(
112113
throw new IllegalArgumentException( "Not a directory: " + directory );
113114
if ( !isDirectoryEmpty( directory ) )
114115
throw new IllegalArgumentException( "Directory not empty: " + directory );
115-
final Git git = Git.cloneRepository()
116+
117+
try (final Git git = Git.cloneRepository()
116118
.setURI( repositoryURL )
117119
.setCredentialsProvider( credentials.getSingleUseCredentialsProvider() )
118120
.setDirectory( directory )
119-
.call();
120-
final Path mastodonProjectPath = directory.toPath().resolve( MASTODON_PROJECT_FOLDER );
121-
if ( Files.exists( mastodonProjectPath ) )
122-
throw new MastodonGitException( "The repository already contains a shared mastodon project: " + repositoryURL );
123-
Files.createDirectory( mastodonProjectPath );
124-
125-
final Path initialStateFolder = directory.toPath().resolve( INITIAL_STATE_FOLDER );
126-
if ( Files.exists( initialStateFolder ) )
127-
throw new MastodonGitException( "The repository already contains a shared mastodon project: " + repositoryURL );
128-
Files.createDirectory( initialStateFolder );
129-
130-
ProjectSaver.saveProject( mastodonProjectPath.toFile(), projectModel );
131-
copyXmlsFromTo( mastodonProjectPath, initialStateFolder );
132-
final Path gitignore = directory.toPath().resolve( ".gitignore" );
121+
.call())
122+
{
123+
124+
final Path mastodonProjectPath = directory.toPath().resolve( MASTODON_PROJECT_FOLDER );
125+
final Path initialStateFolder = directory.toPath().resolve( INITIAL_STATE_FOLDER );
126+
127+
if ( Files.exists( mastodonProjectPath ) || Files.exists( initialStateFolder ) )
128+
throw new MastodonGitException( "The repository already contains a shared mastodon project: " + repositoryURL
129+
+ "\nPlease specify an empty repository." );
133130

134-
Files.write( gitignore, "/mastodon.project/gui.xml\n".getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND );
135-
Files.write( gitignore, "/mastodon.project/project.xml\n".getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND );
136-
Files.write( gitignore, "/mastodon.project/dataset.xml.backup\n".getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND );
131+
Files.createDirectory( mastodonProjectPath );
132+
Files.createDirectory( initialStateFolder );
133+
134+
addGitIgnoreFile( git, directory );
135+
136+
ProjectSaver.saveProject( mastodonProjectPath.toFile(), projectModel );
137+
copyXmlsFromTo( mastodonProjectPath, initialStateFolder );
138+
git.add().addFilepattern( INITIAL_STATE_FOLDER ).addFilepattern( MASTODON_PROJECT_FOLDER ).call();
139+
git.commit().setMessage( "Share mastodon project" ).call();
140+
git.push().setCredentialsProvider( credentials.getSingleUseCredentialsProvider() ).setRemote( "origin" ).call();
141+
return new MastodonGitRepository( projectModel );
142+
}
143+
}
144+
145+
private static void addGitIgnoreFile( final Git git, final File directory ) throws IOException, GitAPIException
146+
{
147+
final Path gitignore = directory.toPath().resolve( ".gitignore" );
148+
final String gitignoreContent = "/mastodon.project/gui.xml\n"
149+
+ "/mastodon.project/project.xml\n"
150+
+ "/mastodon.project/dataset.xml.backup\n";
151+
Files.write( gitignore, gitignoreContent.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND );
137152
git.add().addFilepattern( ".gitignore" ).call();
138153
git.commit().setMessage( "Add .gitignore file" ).call();
139-
git.add().addFilepattern( INITIAL_STATE_FOLDER ).addFilepattern( MASTODON_PROJECT_FOLDER ).call();
140-
git.commit().setMessage( "Share mastodon project" ).call();
141-
git.push().setCredentialsProvider( credentials.getSingleUseCredentialsProvider() ).setRemote( "origin" ).call();
142-
git.close();
143-
return new MastodonGitRepository( projectModel );
144154
}
145155

146156
public File getProjectRoot()

0 commit comments

Comments
 (0)