Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ZEPPELIN-5921] Fix sync with remote repository #4610

Merged
merged 3 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public void move(String noteId,
super.move(noteId, notePath, newNotePath, subject);
String noteFileName = buildNoteFileName(noteId, notePath);
String newNoteFileName = buildNoteFileName(noteId, newNotePath);
git.rm().addFilepattern(noteFileName);
git.add().addFilepattern(newNoteFileName);
try {
git.rm().addFilepattern(noteFileName).call();
git.add().addFilepattern(newNoteFileName).call();
git.commit().setMessage("Move note " + noteId + " from " + noteFileName + " to " +
newNoteFileName).call();
} catch (GitAPIException e) {
Expand All @@ -104,15 +104,39 @@ public void move(String noteId,
public void move(String folderPath, String newFolderPath,
AuthenticationInfo subject) throws IOException {
super.move(folderPath, newFolderPath, subject);
git.rm().addFilepattern(folderPath.substring(1));
git.add().addFilepattern(newFolderPath.substring(1));
try {
git.rm().addFilepattern(folderPath.substring(1)).call();
git.add().addFilepattern(newFolderPath.substring(1)).call();
git.commit().setMessage("Move folder " + folderPath + " to " + newFolderPath).call();
} catch (GitAPIException e) {
throw new IOException(e);
}
}

@Override
public void remove(String noteId, String notePath, AuthenticationInfo subject)
throws IOException {
super.remove(noteId, notePath, subject);
String noteFileName = buildNoteFileName(noteId, notePath);
try {
git.rm().addFilepattern(noteFileName).call();
git.commit().setMessage("Remove note: " + noteId + ", notePath: " + notePath).call();
} catch (GitAPIException e) {
throw new IOException(e);
}
}

@Override
public void remove(String folderPath, AuthenticationInfo subject) throws IOException {
super.remove(folderPath, subject);
try {
git.rm().addFilepattern(folderPath.substring(1)).call();
git.commit().setMessage("Remove folder: " + folderPath).call();
} catch (GitAPIException e) {
throw new IOException(e);
}
}

/* implemented as git add+commit
* @param noteId is the noteId
* @param noteName name of the note
Expand Down Expand Up @@ -238,4 +262,3 @@ void setGit(Git git) {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -399,4 +402,96 @@ public void setRevisionTest() throws IOException {
returnedNote = notebookRepo.setNoteRevision(note.getId(), note.getPath(), "nonexistent_id", null);
assertNull(returnedNote);
}

@Test
public void moveNoteTest() throws IOException, GitAPIException {
//given
notebookRepo = new GitNotebookRepo(conf);
notebookRepo.checkpoint(TEST_NOTE_ID, TEST_NOTE_PATH, "first commit, note1", null);

//when
final String NOTE_FILENAME = TEST_NOTE_PATH.substring(TEST_NOTE_PATH.lastIndexOf("/") + 1);
final String MOVE_DIR = "/move";
final String TEST_MOVE_PATH = MOVE_DIR + "/" + NOTE_FILENAME;
new File(notebooksDir + MOVE_DIR).mkdirs();
notebookRepo.move(TEST_NOTE_ID, TEST_NOTE_PATH, TEST_MOVE_PATH, null);

//then
assertFileIsMoved();
}

@Test
public void moveFolderTest() throws IOException, GitAPIException {
//given
notebookRepo = new GitNotebookRepo(conf);
notebookRepo.checkpoint(TEST_NOTE_ID, TEST_NOTE_PATH, "first commit, note1", null);
notebookRepo.checkpoint(TEST_NOTE_ID2, TEST_NOTE_PATH2, "second commit, note2", null);

//when
final String NOTE_DIR = TEST_NOTE_PATH.substring(0, TEST_NOTE_PATH.lastIndexOf("/"));
final String MOVE_DIR = "/move";
new File(notebooksDir + MOVE_DIR).mkdirs();
notebookRepo.move(NOTE_DIR, MOVE_DIR, null);

//then
assertFileIsMoved();
}

@Test
public void removeNoteTest() throws IOException, GitAPIException {
//given
notebookRepo = new GitNotebookRepo(conf);
notebookRepo.checkpoint(TEST_NOTE_ID, TEST_NOTE_PATH, "first commit, note1", null);

//when
notebookRepo.remove(TEST_NOTE_ID, TEST_NOTE_PATH, null);

//then
assertFileIsDeleted();
}

@Test
public void removeFolderTest() throws IOException, GitAPIException {
//given
notebookRepo = new GitNotebookRepo(conf);
notebookRepo.checkpoint(TEST_NOTE_ID, TEST_NOTE_PATH, "first commit, note1", null);

//when
final String NOTE_DIR = TEST_NOTE_PATH.substring(0, TEST_NOTE_PATH.lastIndexOf("/"));
notebookRepo.remove(NOTE_DIR, null);

//then
assertFileIsDeleted();
}

private void assertFileIsMoved() throws IOException, GitAPIException {
Git git = notebookRepo.getGit();
RevCommit latestCommit = git.log().call().iterator().next();
ObjectId treeId = latestCommit.getTree().getId();
Repository repository = git.getRepository();

try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.reset(treeId);
treeWalk.next();
RevCommit previousCommit = latestCommit.getParent(0);
try (TreeWalk previousTreeWalk = new TreeWalk(repository)) {
previousTreeWalk.reset(previousCommit.getTree());
previousTreeWalk.next();
assertNotEquals(treeWalk.getPathString(), previousTreeWalk.getPathString());
assertEquals(treeWalk.getObjectId(0), previousTreeWalk.getObjectId(0));
}
}
}

private void assertFileIsDeleted() throws IOException, GitAPIException {
Git git = notebookRepo.getGit();
RevCommit latestCommit = git.log().call().iterator().next();
ObjectId treeId = latestCommit.getTree().getId();
Repository repository = git.getRepository();

try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.reset(treeId);
assertFalse(treeWalk.next());
}
}
}