This is an automated email from the ASF dual-hosted git repository. pdallig pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/zeppelin.git
The following commit(s) were added to refs/heads/master by this push: new ce577ad757 [ZEPPELIN-5921] Fix sync with remote repository (#4610) ce577ad757 is described below commit ce577ad757fccf26552b1d1282aea43c6a3c3f21 Author: matthias-koch <koch-matth...@web.de> AuthorDate: Fri Jun 2 08:59:30 2023 +0200 [ZEPPELIN-5921] Fix sync with remote repository (#4610) * [ZEPPELIN-5921] Fix sync with remote repository * [ZEPPELIN-5921] Add missing imports * [ZEPPELIN-5921] Add missing import --------- Co-authored-by: Matthias Koch <matthias-a.k...@rohde-schwarz.com> --- .../zeppelin/notebook/repo/GitNotebookRepo.java | 33 ++++++-- .../notebook/repo/GitNotebookRepoTest.java | 96 ++++++++++++++++++++++ 2 files changed, 124 insertions(+), 5 deletions(-) diff --git a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/GitNotebookRepo.java b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/GitNotebookRepo.java index 8ad7cfed37..9fb9c7b1cb 100644 --- a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/GitNotebookRepo.java +++ b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/GitNotebookRepo.java @@ -90,9 +90,9 @@ public class GitNotebookRepo extends VFSNotebookRepo implements NotebookRepoWith 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) { @@ -104,15 +104,39 @@ public class GitNotebookRepo extends VFSNotebookRepo implements NotebookRepoWith 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 @@ -238,4 +262,3 @@ public class GitNotebookRepo extends VFSNotebookRepo implements NotebookRepoWith } } - diff --git a/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/repo/GitNotebookRepoTest.java b/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/repo/GitNotebookRepoTest.java index 54595bc69d..054b90c21c 100644 --- a/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/repo/GitNotebookRepoTest.java +++ b/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/repo/GitNotebookRepoTest.java @@ -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; @@ -42,7 +43,10 @@ import org.apache.zeppelin.user.AuthenticationInfo; 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; @@ -399,4 +403,96 @@ public class GitNotebookRepoTest { 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()); + } + } }