|
42 | 42 | import org.eclipse.jgit.api.Git;
|
43 | 43 | import org.eclipse.jgit.api.ListBranchCommand;
|
44 | 44 | import org.eclipse.jgit.api.ResetCommand;
|
| 45 | +import org.eclipse.jgit.api.errors.GitAPIException; |
45 | 46 | import org.eclipse.jgit.lib.BranchConfig;
|
46 | 47 | import org.eclipse.jgit.lib.ConfigConstants;
|
47 | 48 | import org.eclipse.jgit.lib.Ref;
|
@@ -112,35 +113,44 @@ public static MastodonGitRepository shareProject(
|
112 | 113 | throw new IllegalArgumentException( "Not a directory: " + directory );
|
113 | 114 | if ( !isDirectoryEmpty( directory ) )
|
114 | 115 | throw new IllegalArgumentException( "Directory not empty: " + directory );
|
115 |
| - final Git git = Git.cloneRepository() |
| 116 | + |
| 117 | + try (final Git git = Git.cloneRepository() |
116 | 118 | .setURI( repositoryURL )
|
117 | 119 | .setCredentialsProvider( credentials.getSingleUseCredentialsProvider() )
|
118 | 120 | .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." ); |
133 | 130 |
|
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 ); |
137 | 152 | git.add().addFilepattern( ".gitignore" ).call();
|
138 | 153 | 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 ); |
144 | 154 | }
|
145 | 155 |
|
146 | 156 | public File getProjectRoot()
|
|
0 commit comments