@@ -79,6 +79,10 @@ public class MastodonGitRepository
79
79
80
80
private static final PersistentCredentials credentials = new PersistentCredentials ();
81
81
82
+ private static final String INITIAL_STATE_FOLDER = "mastodon.initial_state" ;
83
+
84
+ private static final String MASTODON_PROJECT_FOLDER = "mastodon.project" ;
85
+
82
86
private final ProjectModel projectModel ;
83
87
84
88
private final File projectRoot ;
@@ -109,30 +113,44 @@ public static MastodonGitRepository shareProject(
109
113
throw new IllegalArgumentException ( "Not a directory: " + directory );
110
114
if ( !isDirectoryEmpty ( directory ) )
111
115
throw new IllegalArgumentException ( "Directory not empty: " + directory );
112
- final Git git = Git .cloneRepository ()
116
+
117
+ try (final Git git = Git .cloneRepository ()
113
118
.setURI ( repositoryURL )
114
119
.setCredentialsProvider ( credentials .getSingleUseCredentialsProvider () )
115
120
.setDirectory ( directory )
116
- .call ();
117
- final Path mastodonProjectPath = directory .toPath ().resolve ( "mastodon.project" );
118
- if ( Files .exists ( mastodonProjectPath ) )
119
- throw new MastodonGitException ( "The repository already contains a shared mastodon project: " + repositoryURL );
120
- Files .createDirectory ( mastodonProjectPath );
121
- ProjectSaver .saveProject ( mastodonProjectPath .toFile (), projectModel );
122
- Files .copy ( mastodonProjectPath .resolve ( "gui.xml" ), mastodonProjectPath .resolve ( "gui.xml_remote" ) );
123
- Files .copy ( mastodonProjectPath .resolve ( "project.xml" ), mastodonProjectPath .resolve ( "project.xml_remote" ) );
124
- Files .copy ( mastodonProjectPath .resolve ( "dataset.xml.backup" ), mastodonProjectPath .resolve ( "dataset.xml.backup_remote" ) );
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
+ + "\n Please specify an empty repository." );
130
+
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
+ {
125
147
final Path gitignore = directory .toPath ().resolve ( ".gitignore" );
126
- Files .write ( gitignore , "/mastodon.project/gui.xml\n " .getBytes (), StandardOpenOption .CREATE , StandardOpenOption .APPEND );
127
- Files .write ( gitignore , "/mastodon.project/project.xml\n " .getBytes (), StandardOpenOption .CREATE , StandardOpenOption .APPEND );
128
- Files .write ( gitignore , "/mastodon.project/dataset.xml.backup\n " .getBytes (), StandardOpenOption .CREATE , StandardOpenOption .APPEND );
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 );
129
152
git .add ().addFilepattern ( ".gitignore" ).call ();
130
153
git .commit ().setMessage ( "Add .gitignore file" ).call ();
131
- git .add ().addFilepattern ( "mastodon.project" ).call ();
132
- git .commit ().setMessage ( "Share mastodon project" ).call ();
133
- git .push ().setCredentialsProvider ( credentials .getSingleUseCredentialsProvider () ).setRemote ( "origin" ).call ();
134
- git .close ();
135
- return new MastodonGitRepository ( projectModel );
136
154
}
137
155
138
156
public File getProjectRoot ()
@@ -161,19 +179,25 @@ public static void cloneRepository( final String repositoryURL, final File direc
161
179
.setDirectory ( directory )
162
180
.call ())
163
181
{
164
- final Path mastodonProjectPath = directory .toPath ().resolve ( "mastodon.project" );
165
- Files .copy ( mastodonProjectPath .resolve ( "gui.xml_remote" ), mastodonProjectPath .resolve ( "gui.xml" ) );
166
- Files .copy ( mastodonProjectPath .resolve ( "project.xml_remote" ), mastodonProjectPath .resolve ( "project.xml" ) );
167
- Files .copy ( mastodonProjectPath .resolve ( "dataset.xml.backup_remote" ), mastodonProjectPath .resolve ( "dataset.xml.backup" ) );
182
+ final Path mastodonProjectPath = directory .toPath ().resolve ( MASTODON_PROJECT_FOLDER );
183
+ final Path remoteFolder = directory .toPath ().resolve ( INITIAL_STATE_FOLDER );
184
+ copyXmlsFromTo ( remoteFolder , mastodonProjectPath );
168
185
}
169
186
}
170
187
188
+ private static void copyXmlsFromTo ( final Path from , final Path to ) throws IOException
189
+ {
190
+ Files .copy ( from .resolve ( "gui.xml" ), to .resolve ( "gui.xml" ) );
191
+ Files .copy ( from .resolve ( "project.xml" ), to .resolve ( "project.xml" ) );
192
+ Files .copy ( from .resolve ( "dataset.xml.backup" ), to .resolve ( "dataset.xml.backup" ) );
193
+ }
194
+
171
195
/**
172
196
* Simply starts a new Mastodon window with the project in the given repository.
173
197
*/
174
198
public static void openProjectInRepository ( final Context context , final File directory ) throws Exception
175
199
{
176
- final String mastodonFile = directory .toPath ().resolve ( "mastodon.project" ).toString ();
200
+ final String mastodonFile = directory .toPath ().resolve ( MASTODON_PROJECT_FOLDER ).toString ();
177
201
final boolean restoreGUIState = true ;
178
202
final boolean authorizeSubstituteDummyData = true ;
179
203
final ProjectModel newProject = ProjectLoader .open ( mastodonFile , context , restoreGUIState , authorizeSubstituteDummyData );
@@ -187,7 +211,7 @@ public synchronized void commitWithoutSave( final String message ) throws Except
187
211
{
188
212
try (final Git git = initGit ())
189
213
{
190
- git .add ().addFilepattern ( "mastodon.project" ).call ();
214
+ git .add ().addFilepattern ( MASTODON_PROJECT_FOLDER ).call ();
191
215
final CommitCommand commit = git .commit ();
192
216
commit .setMessage ( message );
193
217
commit .setAuthor ( settingsService .getPersonIdent () );
@@ -480,7 +504,7 @@ public synchronized void reset() throws Exception
480
504
481
505
private Git initGit () throws IOException
482
506
{
483
- final boolean correctFolder = projectRoot .getName ().equals ( "mastodon.project" );
507
+ final boolean correctFolder = projectRoot .getName ().equals ( MASTODON_PROJECT_FOLDER );
484
508
if ( !correctFolder )
485
509
throw new MastodonGitException ( "The current project does not appear to be in a git repo." );
486
510
final File gitRoot = projectRoot .getParentFile ();
0 commit comments