Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ public String getNotebookDir() {
return getRelativeDir(ConfVars.ZEPPELIN_NOTEBOOK_DIR);
}

public String getGitRepoDir() {
return getString(ConfVars.ZEPPELIN_GIT_REPO_DIR);
}

public String getNotebookRunId() {
return getString(ConfVars.ZEPPELIN_NOTEBOOK_RUN_ID);
}
Expand Down Expand Up @@ -909,6 +913,7 @@ public enum ConfVars {
ZEPPELIN_INTERPRETER_OUTPUT_LIMIT("zeppelin.interpreter.output.limit", 1024 * 100),
ZEPPELIN_ENCODING("zeppelin.encoding", "UTF-8"),
ZEPPELIN_NOTEBOOK_DIR("zeppelin.notebook.dir", "notebook"),
ZEPPELIN_GIT_REPO_DIR("zeppelin.git.repo.dir", ""),

ZEPPELIN_NOTEBOOK_RUN_ID("zeppelin.notebook.run.id", null), // run particular note id on zeppelin start
ZEPPELIN_NOTEBOOK_RUN_REV("zeppelin.notebook.run.rev", null), // revision id for ZEPPELIN_NOTEBOOK_RUN_ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.zeppelin.conf.ZeppelinConfiguration;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.user.AuthenticationInfo;
Expand All @@ -39,6 +40,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;

Expand All @@ -56,6 +59,7 @@ public class GitNotebookRepo extends VFSNotebookRepo implements NotebookRepoWith
private static final Logger LOGGER = LoggerFactory.getLogger(GitNotebookRepo.class);

private Git git;
protected String gitRepoDir;

public GitNotebookRepo() {
super();
Expand All @@ -73,10 +77,11 @@ public void init(ZeppelinConfiguration conf) throws IOException {
//AbstractMethodError
this.conf = conf;
setNotebookDirectory(conf.getNotebookDir());
setGitRepoDir(conf.getGitRepoDir());

LOGGER.info("Opening a git repo at '{}'", this.rootNotebookFolder);
LOGGER.info("Opening a git repo at '{}'", this.gitRepoDir);
Repository localRepo = new FileRepository(Joiner.on(File.separator)
.join(this.rootNotebookFolder, ".git"));
.join(this.gitRepoDir, ".git"));
if (!localRepo.getDirectory().exists()) {
LOGGER.info("Git repo {} does not exist, creating a new one", localRepo.getDirectory());
localRepo.create();
Expand All @@ -90,8 +95,8 @@ public void move(String noteId,
String newNotePath,
AuthenticationInfo subject) throws IOException {
super.move(noteId, notePath, newNotePath, subject);
String noteFileName = buildNoteFileName(noteId, notePath);
String newNoteFileName = buildNoteFileName(noteId, newNotePath);
String noteFileName = buildNoteFileNameForGitRepo(noteId, notePath);
String newNoteFileName = buildNoteFileNameForGitRepo(noteId, newNotePath);
git.rm().addFilepattern(noteFileName);
git.add().addFilepattern(newNoteFileName);
try {
Expand Down Expand Up @@ -127,7 +132,7 @@ public Revision checkpoint(String noteId,
String notePath,
String commitMessage,
AuthenticationInfo subject) throws IOException {
String noteFileName = buildNoteFileName(noteId, notePath);
String noteFileName = buildNoteFileNameForGitRepo(noteId, notePath);
Revision revision = Revision.EMPTY;
try {
List<DiffEntry> gitDiff = git.diff().call();
Expand Down Expand Up @@ -161,7 +166,7 @@ public synchronized Note get(String noteId,
AuthenticationInfo subject) throws IOException {
Note note = null;
RevCommit stash = null;
String noteFileName = buildNoteFileName(noteId, notePath);
String noteFileName = buildNoteFileNameForGitRepo(noteId, notePath);
try {
List<DiffEntry> gitDiff = git.diff().setPathFilter(PathFilter.create(noteFileName)).call();
boolean modified = !gitDiff.isEmpty();
Expand Down Expand Up @@ -197,7 +202,7 @@ public List<Revision> revisionHistory(String noteId,
String notePath,
AuthenticationInfo subject) throws IOException {
List<Revision> history = Lists.newArrayList();
String noteFileName = buildNoteFileName(noteId, notePath);
String noteFileName = buildNoteFileNameForGitRepo(noteId, notePath);
LOGGER.debug("Listing history for {}:", noteFileName);
try {
Iterable<RevCommit> logs = git.log().addPath(noteFileName).call();
Expand Down Expand Up @@ -239,5 +244,30 @@ void setGit(Git git) {
this.git = git;
}

protected void setGitRepoDir(String gitRepoDir) {
if (StringUtils.isEmpty(gitRepoDir)) {
this.gitRepoDir = rootNotebookFolder;
} else {
this.gitRepoDir = gitRepoDir;
}
if (!isChild(rootNotebookFolder, this.gitRepoDir)) {
throw new IllegalArgumentException("Notebook directory (" + rootNotebookFolder + ")" +
" must be nested within Git Repo (" + gitRepoDir + ")");
}
}

private Boolean isChild(String childText, String parentText) {
Path child = Paths.get(childText.replaceAll("^/*", "")).toAbsolutePath();
Path parent = Paths.get(parentText.replaceAll("^/*", "")).toAbsolutePath();
return child.startsWith(parent);
}

protected String buildNoteFileNameForGitRepo(String noteId, String notePath) throws IOException {
Path child = Paths.get(rootNotebookFolder.replaceAll("^/*", "")).toAbsolutePath();
Path parent = Paths.get(gitRepoDir.replaceAll("^/*", "")).toAbsolutePath();
String notepathPrefix = "/" + parent.relativize(child).toString().replace("\\","/");
return buildNoteFileName(noteId, notepathPrefix + notePath);
}

}