Skip to content

Commit

Permalink
feat: history prewview
Browse files Browse the repository at this point in the history
  • Loading branch information
Hansanshi committed Dec 26, 2021
1 parent ae57ce7 commit 2cb6448
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>ink.markidea</groupId>
<artifactId>note</artifactId>
<version>0.4.6</version>
<version>0.4.7</version>
<name>note</name>
<description>MarkIdea, a note-taking software</description>

Expand Down
25 changes: 25 additions & 0 deletions src/main/java/ink/markidea/note/controller/HistoryController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ink.markidea.note.controller;

import ink.markidea.note.entity.req.QueryHistoryContentReq;
import ink.markidea.note.entity.resp.ServerResponse;
import ink.markidea.note.service.INoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/history")
public class HistoryController {


@Autowired
private INoteService noteService;

@PostMapping("queryHistoryContent")
public ServerResponse<String> queryHistoryContent(@RequestBody QueryHistoryContentReq req) {
return noteService.getNoteHistoryContent(req.getNotebookName(), req.getNoteTitle(), req.getVersionRef());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ink.markidea.note.entity.req;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class QueryHistoryContentReq {

private String notebookName;
private String noteTitle;
private String versionRef;
}
5 changes: 5 additions & 0 deletions src/main/java/ink/markidea/note/service/INoteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public interface INoteService {
*/
ServerResponse<List<NoteVersionVo>> getNoteHistory(String notebookName, String noteTitle);

/**
* get note content at a history version
*/
ServerResponse<String> getNoteHistoryContent(String notebookName, String noteTitle, String versionRef);

/**
* recover note to a certain version
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ public ServerResponse<List<NoteVersionVo>> getNoteHistory(String notebookName, S
return ServerResponse.buildSuccessResponse(noteVersionVoList);
}

@Override
public ServerResponse<String> getNoteHistoryContent(String notebookName, String noteTitle, String versionRef) {
String relativeFileName = getRelativeFileName(notebookName,noteTitle);
String historyContent = GitUtil.getFileHistoryContent(getOrCreateUserGit(), relativeFileName, versionRef);
return ServerResponse.buildSuccessResponse(historyContent);
}

@Override
public ServerResponse<String> resetAndGet(String notebookName, String noteTitle, String versionRef){
String relativeFileName = getRelativeFileName(notebookName,noteTitle);
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/ink/markidea/note/util/GitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import ink.markidea.note.entity.exception.PromptException;
import ink.markidea.note.entity.vo.NoteVersionVo;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -16,19 +17,28 @@
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.patch.FileHeader;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.*;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.io.DisabledOutputStream;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;

import static org.eclipse.jgit.diff.DiffEntry.Side.OLD;
import static org.eclipse.jgit.lib.ConfigConstants.*;

/**
Expand Down Expand Up @@ -268,6 +278,46 @@ public static String getFileCurRef(Git git, String fileName) {
return null;
}

/**
* 获取文件某个历史版本的内容
* 实现不优雅 由于不清楚git原理
*/
public static String getFileHistoryContent(Git git, String filePath, String ref) {
List<RevCommit> revCommitList = getVersionHistory(git, filePath);
String curRef = revCommitList.get(0).getName();
if (ref.equals(curRef)) {
throw new PromptException("当前版本无需预览");
}
Repository repository = git.getRepository();
try {
AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, ref);
AbstractTreeIterator newTreeParser = prepareTreeParser(repository, curRef);
List<DiffEntry> diff = git.diff().
setOldTree(oldTreeParser).
setNewTree(newTreeParser).
setPathFilter(PathFilter.create(filePath)).
call();

for (DiffEntry entry : diff) {
try (DiffFormatter formatter = new DiffFormatter(System.out)) {
formatter.setRepository(repository);
Method method = DiffFormatter.class.getDeclaredMethod("open", DiffEntry.Side.class, DiffEntry.class);
method.setAccessible(true);
RawText rawText = (RawText) method.invoke(formatter, OLD, entry);
return new String(rawText.getRawContent());
}
}
throw new RuntimeException();
} catch (Exception e) {
log.error("getHistory content error", e);
throw new IllegalArgumentException();
}
}

public static void main(String[] args) {
Git git = GitUtil.getOrInitGit("/Users/arthurlhan/Documents/GitHub/mark-idea");
System.out.println(GitUtil.getFileHistoryContent(git, "README.md", "07cc4db71ae5218267e221de351999fc5d5b1bc6"));
}

/**
* temporarily quit to complete the method
Expand Down Expand Up @@ -451,4 +501,22 @@ public static void discardChange(Git git, String path){
}
}

private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId) throws IOException {
// from the commit we can build the tree which allows us to construct the TreeParser
//noinspection Duplicates
try (RevWalk walk = new RevWalk(repository)) {
RevCommit commit = walk.parseCommit(ObjectId.fromString(objectId));
RevTree tree = walk.parseTree(commit.getTree().getId());

CanonicalTreeParser treeParser = new CanonicalTreeParser();
try (ObjectReader reader = repository.newObjectReader()) {
treeParser.reset(reader, tree.getId());
}

walk.dispose();

return treeParser;
}
}

}

0 comments on commit 2cb6448

Please sign in to comment.