Fixed bug with git not adding deleted files.

This commit is contained in:
Winston Li 2015-01-11 17:23:49 +00:00
parent d4613864c0
commit e124a74c2f
12 changed files with 193 additions and 8 deletions

View file

@ -49,6 +49,9 @@ public class WLBridgedProject {
for (WritableRepositoryContents contents : writableRepositories) {
contents.write();
Git git = new Git(repository);
for (String missing : git.status().call().getMissing()) {
git.rm().setCached(true).addFilepattern(missing).call();
}
git.add().addFilepattern(".").call();
git.commit().setAuthor(new PersonIdent(contents.getUserName(), contents.getUserEmail(), contents.getWhen(), TimeZone.getDefault()))
.setMessage(contents.getCommitMessage())

View file

@ -34,8 +34,8 @@ public class FileUtil {
}
public static boolean gitDirectoriesAreEqual(Path dir1, Path dir2) {
Set<String> dir1Contents = getAllFilesRecursivelyInDirectoryApartFrom(dir1, dir1.resolve(".git"));
Set<String> dir2Contents = getAllFilesRecursivelyInDirectoryApartFrom(dir2, dir2.resolve(".git"));
Set<String> dir1Contents = getAllRecursivelyInDirectoryApartFrom(dir1, dir1.resolve(".git"));
Set<String> dir2Contents = getAllRecursivelyInDirectoryApartFrom(dir2, dir2.resolve(".git"));
boolean filesEqual = dir1Contents.equals(dir2Contents);
if (!filesEqual) {
System.out.println("Not equal: (" + dir1Contents + ", " + dir2Contents + ")");
@ -72,20 +72,31 @@ public class FileUtil {
}
}
public static Set<String> getAllFilesRecursivelyInDirectoryApartFrom(Path dir, Path excluded) {
public static Set<String> getAllRecursivelyInDirectoryApartFrom(Path dir, Path excluded) {
return getAllRecursivelyInDirectoryApartFrom(dir, excluded, true);
}
public static Set<String> getOnlyFilesRecursivelyInDirectoryApartFrom(Path dir, Path excluded) {
return getAllRecursivelyInDirectoryApartFrom(dir, excluded, false);
}
private static Set<String> getAllRecursivelyInDirectoryApartFrom(Path dir, Path excluded, boolean directories) {
if (!dir.toFile().isDirectory()) {
throw new IllegalArgumentException("need a directory");
}
return getAllFilesRecursively(dir, dir, excluded);
return getAllFilesRecursively(dir, dir, excluded, directories);
}
static Set<String> getAllFilesRecursively(Path baseDir, Path dir, Path excluded) {
static Set<String> getAllFilesRecursively(Path baseDir, Path dir, Path excluded, boolean directories) {
Set<String> files = new HashSet<String>();
for (File file : dir.toFile().listFiles()) {
if (!file.equals(excluded.toFile())) {
boolean isDirectory = file.isDirectory();
if (directories || !isDirectory) {
files.add(baseDir.relativize(file.toPath()).toString());
if (file.isDirectory()) {
files.addAll(getAllFilesRecursively(baseDir, file.toPath(), excluded));
}
if (isDirectory) {
files.addAll(getAllFilesRecursively(baseDir, file.toPath(), excluded, directories));
}
}
}

View file

@ -36,6 +36,7 @@ public class GitDirectoryContents implements WritableRepositoryContents {
public void write() throws IOException, FailedConnectionException {
WLFileStore.deleteInDirectoryApartFrom(gitDirectory, ".git");
for (FileNode fileNode : fileNodes) {
System.out.println("Writing: " + fileNode.getFilePath());
fileNode.writeToDisk(gitDirectory);
}
}

View file

@ -58,6 +58,7 @@ public class WLFileStore implements PersistentStoreSource {
if (file.isDirectory()) {
deleteInDirectory(file);
}
System.out.println("deleting: " + file.getAbsolutePath());
file.delete();
}
}

View file

@ -36,6 +36,10 @@ public class WLGitBridgeIntegrationTest {
put("base", new SnapshotAPIStateBuilder(getResourceAsStream("/canPullAModifiedTexFile/base/state.json")).build());
put("withModifiedTexFile", new SnapshotAPIStateBuilder(getResourceAsStream("/canPullAModifiedTexFile/withModifiedTexFile/state.json")).build());
}});
put("canPullADeletedTexFile", new HashMap<String, SnapshotAPIState>() {{
put("base", new SnapshotAPIStateBuilder(getResourceAsStream("/canPullADeletedTexFile/base/state.json")).build());
put("withDeletedTexFile", new SnapshotAPIStateBuilder(getResourceAsStream("/canPullADeletedTexFile/withDeletedTexFile/state.json")).build());
}});
}};
@Rule
@ -111,6 +115,29 @@ public class WLGitBridgeIntegrationTest {
assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/canPullAModifiedTexFile/withModifiedTexFile/testproj"), git.toPath()));
}
@Test
public void canPullADeletedTexFile() throws IOException, GitAPIException {
MockSnapshotServer server = new MockSnapshotServer(3860, getResource("/canPullADeletedTexFile").toFile());
server.start();
server.setState(states.get("canPullADeletedTexFile").get("base"));
WLGitBridgeApplication wlgb = new WLGitBridgeApplication(new String[] {
makeConfigFile(33860, 3860)
});
wlgb.run();
folder.create();
File git = folder.newFolder();
Git base = Git.cloneRepository()
.setURI("http://127.0.0.1:33860/testproj.git")
.setDirectory(git)
.call();
assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/canPullADeletedTexFile/base/testproj"), git.toPath()));
server.setState(states.get("canPullADeletedTexFile").get("withDeletedTexFile"));
base.pull().call();
base.close();
wlgb.stop();
assertTrue(FileUtil.gitDirectoriesAreEqual(getResource("/canPullADeletedTexFile/withDeletedTexFile/testproj"), git.toPath()));
}
private String makeConfigFile(int port, int apiPort) throws IOException {
File wlgb = folder.newFolder();
File config = folder.newFile();

View file

@ -0,0 +1,46 @@
[
{
"project": "testproj",
"getDoc": {
"versionID": 1,
"createdAt": "2014-11-30T18:40:58Z",
"email": "jdleesmiller+1@gmail.com",
"name": "John+1"
},
"getSavedVers": [
{
"versionID": 1,
"comment": "added more info on doc GET and error details",
"email": "jdleesmiller+1@gmail.com",
"name": "John+1",
"createdAt": "2014-11-30T18:47:01Z"
}
],
"getForVers": [
{
"versionID": 1,
"srcs": [
{
"content": "content\n",
"path": "main.tex"
},
{
"content": "This text is from another file.",
"path": "test.tex"
}
],
"atts": [
{
"url": "http://127.0.0.1:3860/base/testproj/overleaf-white-410.png",
"path": "overleaf-white-410.png"
}
]
}
],
"push": "success",
"postback": {
"type": "success",
"versionID": 2
}
}
]

View file

@ -0,0 +1,12 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 404 </title>
</head>
<body>
<h2>HTTP ERROR: 404</h2>
<p>Problem accessing /state/testproj1/overleaf-white-410.png. Reason:
<pre> Not Found</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
</body>
</html>

View file

@ -0,0 +1,68 @@
[
{
"project": "testproj",
"getDoc": {
"versionID": 2,
"createdAt": "2014-11-30T18:40:58Z",
"email": "jdleesmiller+1@gmail.com",
"name": "John+1"
},
"getSavedVers": [
{
"versionID": 2,
"comment": "i deleted test.tex",
"email": "jdleesmiller+1@gmail.com",
"name": "John+1",
"createdAt": "2014-11-30T18:48:01Z"
},
{
"versionID": 1,
"comment": "added more info on doc GET and error details",
"email": "jdleesmiller+1@gmail.com",
"name": "John+1",
"createdAt": "2014-11-30T18:47:01Z"
}
],
"getForVers": [
{
"versionID": 2,
"srcs": [
{
"content": "content\nadded more stuff\n",
"path": "main.tex"
}
],
"atts": [
{
"url": "http://127.0.0.1:3860/withDeletedTexFile/testproj/overleaf-white-410.png",
"path": "overleaf-white-410.png"
}
]
},
{
"versionID": 1,
"srcs": [
{
"content": "content\n",
"path": "main.tex"
},
{
"content": "This text is from another file.",
"path": "test.tex"
}
],
"atts": [
{
"url": "http://127.0.0.1:3860/base/testproj/overleaf-white-410.png",
"path": "overleaf-white-410.png"
}
]
}
],
"push": "success",
"postback": {
"type": "success",
"versionID": 2
}
}
]

View file

@ -0,0 +1,12 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 404 </title>
</head>
<body>
<h2>HTTP ERROR: 404</h2>
<p>Problem accessing /state/testproj1/overleaf-white-410.png. Reason:
<pre> Not Found</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
</body>
</html>