Skip to content

Commit

Permalink
[ZEPPELIN-5921] Fix sync with remote repository (#4610)
Browse files Browse the repository at this point in the history
* [ZEPPELIN-5921] Fix sync with remote repository

* [ZEPPELIN-5921] Add missing imports

* [ZEPPELIN-5921] Add missing import

---------

Co-authored-by: Matthias Koch <matthias-a.koch@rohde-schwarz.com>
  • Loading branch information
matthias-koch and Matthias Koch committed Jun 2, 2023
1 parent df5d827 commit ce577ad
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 5 deletions.
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 @@ -18,6 +18,7 @@
package org.apache.zeppelin.notebook.repo;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -42,7 +43,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 +403,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());
}
}
}

0 comments on commit ce577ad

Please sign in to comment.