Skip to content

Commit 89443a9

Browse files
authored
Merge pull request #23 from mastodon-sc/fix-repository-not-clean-bug
Fix "There are uncommitted changes" error messages when using mastodon-git with maston (core) 1.0.0-SNAPSHOT-31
2 parents c5e9f6d + 8c4998c commit 89443a9

File tree

2 files changed

+56
-25
lines changed

2 files changed

+56
-25
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@
156156
<configFile>mastodon-coding-style.xml</configFile>
157157
</configuration>
158158
</plugin>
159+
<plugin>
160+
<groupId>org.apache.maven.plugins</groupId>
161+
<artifactId>maven-surefire-plugin</artifactId>
162+
<configuration>
163+
<argLine>-Xmx2g</argLine>
164+
</configuration>
165+
</plugin>
159166
</plugins>
160167
</pluginManagement>
161168
</build>

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

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public class MastodonGitRepository
7979

8080
private static final PersistentCredentials credentials = new PersistentCredentials();
8181

82+
private static final String INITIAL_STATE_FOLDER = "mastodon.initial_state";
83+
84+
private static final String MASTODON_PROJECT_FOLDER = "mastodon.project";
85+
8286
private final ProjectModel projectModel;
8387

8488
private final File projectRoot;
@@ -109,30 +113,44 @@ public static MastodonGitRepository shareProject(
109113
throw new IllegalArgumentException( "Not a directory: " + directory );
110114
if ( !isDirectoryEmpty( directory ) )
111115
throw new IllegalArgumentException( "Directory not empty: " + directory );
112-
final Git git = Git.cloneRepository()
116+
117+
try (final Git git = Git.cloneRepository()
113118
.setURI( repositoryURL )
114119
.setCredentialsProvider( credentials.getSingleUseCredentialsProvider() )
115120
.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+
+ "\nPlease 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+
{
125147
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 );
129152
git.add().addFilepattern( ".gitignore" ).call();
130153
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 );
136154
}
137155

138156
public File getProjectRoot()
@@ -161,19 +179,25 @@ public static void cloneRepository( final String repositoryURL, final File direc
161179
.setDirectory( directory )
162180
.call())
163181
{
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 );
168185
}
169186
}
170187

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+
171195
/**
172196
* Simply starts a new Mastodon window with the project in the given repository.
173197
*/
174198
public static void openProjectInRepository( final Context context, final File directory ) throws Exception
175199
{
176-
final String mastodonFile = directory.toPath().resolve( "mastodon.project" ).toString();
200+
final String mastodonFile = directory.toPath().resolve( MASTODON_PROJECT_FOLDER ).toString();
177201
final boolean restoreGUIState = true;
178202
final boolean authorizeSubstituteDummyData = true;
179203
final ProjectModel newProject = ProjectLoader.open( mastodonFile, context, restoreGUIState, authorizeSubstituteDummyData );
@@ -187,7 +211,7 @@ public synchronized void commitWithoutSave( final String message ) throws Except
187211
{
188212
try (final Git git = initGit())
189213
{
190-
git.add().addFilepattern( "mastodon.project" ).call();
214+
git.add().addFilepattern( MASTODON_PROJECT_FOLDER ).call();
191215
final CommitCommand commit = git.commit();
192216
commit.setMessage( message );
193217
commit.setAuthor( settingsService.getPersonIdent() );
@@ -480,7 +504,7 @@ public synchronized void reset() throws Exception
480504

481505
private Git initGit() throws IOException
482506
{
483-
final boolean correctFolder = projectRoot.getName().equals( "mastodon.project" );
507+
final boolean correctFolder = projectRoot.getName().equals( MASTODON_PROJECT_FOLDER );
484508
if ( !correctFolder )
485509
throw new MastodonGitException( "The current project does not appear to be in a git repo." );
486510
final File gitRoot = projectRoot.getParentFile();

0 commit comments

Comments
 (0)