mirror of
https://github.com/gitbucket/gitbucket.git
synced 2026-05-07 21:17:22 +02:00
implement JGitResource/JGitResourcePath class with scala
This commit is contained in:
@@ -1,134 +0,0 @@
|
||||
package editorconfig;
|
||||
|
||||
import org.ec4j.core.Resource;
|
||||
import org.ec4j.core.ResourcePath;
|
||||
import org.ec4j.core.model.Ec4jPath;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.ObjectReader;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevTree;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class JGitResource implements Resource {
|
||||
private final Repository repo;
|
||||
private final String revStr;
|
||||
|
||||
Ec4jPath path;
|
||||
|
||||
private static String removeInitialSlash(Ec4jPath path) {
|
||||
return Ec4jPath.Ec4jPaths.root().relativize(path).toString();
|
||||
}
|
||||
|
||||
public JGitResource(Git git, String revStr, String path){
|
||||
if (!path.startsWith("/")){
|
||||
path = "/" + path;
|
||||
}
|
||||
this.repo= git.getRepository();
|
||||
this.path = Ec4jPath.Ec4jPaths.of(path);
|
||||
this.revStr = revStr;
|
||||
}
|
||||
|
||||
public JGitResource(Repository repo, String revStr, String path){
|
||||
if (!path.startsWith("/")){
|
||||
path = "/" + path;
|
||||
}
|
||||
this.repo = repo;
|
||||
this.path = Ec4jPath.Ec4jPaths.of(path);
|
||||
this.revStr = revStr;
|
||||
}
|
||||
|
||||
|
||||
public JGitResource(Repository repo, String revStr, Ec4jPath path){
|
||||
this.repo = repo;
|
||||
this.path = path;
|
||||
this.revStr = revStr;
|
||||
}
|
||||
|
||||
private RevTree getRevTree() throws IOException {
|
||||
ObjectReader reader = repo.newObjectReader();
|
||||
try {
|
||||
RevWalk revWalk = new RevWalk(reader);
|
||||
ObjectId id = repo.resolve(revStr);
|
||||
RevCommit commit = revWalk.parseCommit(id);
|
||||
return commit.getTree();
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
ObjectReader reader = repo.newObjectReader();
|
||||
try {
|
||||
TreeWalk treeWalk = TreeWalk.forPath(reader, removeInitialSlash(path), getRevTree());
|
||||
if (treeWalk != null){
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourcePath getParent() {
|
||||
Ec4jPath parent = path.getParentPath();
|
||||
return parent == null ? null : new JGitResourcePath(repo, revStr, path.getParentPath());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ec4jPath getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomReader openRandomReader() throws IOException {
|
||||
return Resources.StringRandomReader.ofReader(openReader());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader openReader() throws IOException {
|
||||
ObjectReader reader = repo.newObjectReader();
|
||||
try {
|
||||
TreeWalk treeWalk = TreeWalk.forPath(reader, removeInitialSlash(path), getRevTree());
|
||||
return new InputStreamReader(reader.open(treeWalk.getObjectId(0)).openStream(), StandardCharsets.UTF_8);
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
JGitResource other = (JGitResource) obj;
|
||||
if (!repo.equals(other.repo) || !revStr.equals(other.revStr) || !path.equals(other.path)){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "JGitResouce(Repo:" + repo.getDirectory() + ", revStr:" + revStr + ", path:" + path.toString() + ")";
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package editorconfig;
|
||||
|
||||
import org.ec4j.core.Resource;
|
||||
import org.ec4j.core.ResourcePath;
|
||||
import org.ec4j.core.model.Ec4jPath;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
public class JGitResourcePath implements ResourcePath {
|
||||
private final Repository repo;
|
||||
private final String revStr;
|
||||
private final Ec4jPath path;
|
||||
|
||||
public JGitResourcePath(Repository repo, String revStr, Ec4jPath path){
|
||||
this.repo= repo;
|
||||
this.revStr = revStr;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public static JGitResourcePath RootDirectory(Git git, String revStr){
|
||||
return new JGitResourcePath(git.getRepository(), revStr, Ec4jPath.Ec4jPaths.of("/"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourcePath getParent() {
|
||||
Ec4jPath parent = path.getParentPath();
|
||||
return parent == null ? null : new JGitResourcePath(repo, revStr, parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ec4jPath getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasParent() {
|
||||
return path.getParentPath() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource relativize(Resource resource) {
|
||||
if (resource instanceof JGitResource) {
|
||||
JGitResource jgitResource = (JGitResource) resource;
|
||||
return new JGitResource(repo, revStr, path.relativize(jgitResource.path).toString());
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
this.getClass().getName() + ".relativize(Resource resource) can handle only instances of "
|
||||
+ JGitResource.class.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource resolve(String name) {
|
||||
if(path == null){
|
||||
return new JGitResource(repo, revStr, name);
|
||||
}
|
||||
else {
|
||||
return new JGitResource(repo, revStr, path.resolve(name));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
JGitResourcePath other = (JGitResourcePath) obj;
|
||||
if (!repo.equals(other.repo) || !revStr.equals(other.revStr) || !path.equals(other.path)){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "JGitResoucePath(Repo:" + repo.getDirectory() + ", revStr:" + revStr + ", path:" + path.toString() + ")";
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,111 @@
|
||||
package gitbucket.core.util
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.{IOException, InputStreamReader, Reader}
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
import editorconfig.{JGitResource, JGitResourcePath}
|
||||
import org.ec4j.core.Resource.Resources.StringRandomReader
|
||||
import org.ec4j.core.model.PropertyType.{EndOfLineValue, IndentStyleValue}
|
||||
import org.ec4j.core.model.{PropertyType, Version}
|
||||
import org.ec4j.core.model.{Ec4jPath, PropertyType, Version}
|
||||
import org.ec4j.core.parser.ParseException
|
||||
import org.ec4j.core.{EditorConfigConstants, EditorConfigLoader, ResourceProperties, ResourcePropertiesService}
|
||||
import org.ec4j.core._
|
||||
import org.eclipse.jgit.api.Git
|
||||
import org.eclipse.jgit.lib.{ObjectReader, Repository}
|
||||
import org.eclipse.jgit.revwalk.{RevTree, RevWalk}
|
||||
import org.eclipse.jgit.treewalk.TreeWalk
|
||||
import gitbucket.core.util.SyntaxSugars._
|
||||
|
||||
object EditorConfigUtil {
|
||||
val TabSizeDefault: Int = 8
|
||||
val NewLineModeDefault: String = "auto"
|
||||
val UseSoftTabsDefault = false
|
||||
private class JGitResource(repo: Repository, revStr: String, path: Ec4jPath) extends Resource {
|
||||
private def removeInitialSlash(path: Ec4jPath) = Ec4jPath.Ec4jPaths.root.relativize(path).toString
|
||||
|
||||
def this(git: Git, revStr: String, path: String) = {
|
||||
this(git.getRepository, revStr, Ec4jPath.Ec4jPaths.of(if (path.startsWith("/")) path else "/" + path))
|
||||
}
|
||||
|
||||
def this(repo: Repository, revStr: String, path: String) = {
|
||||
this(repo, revStr, Ec4jPath.Ec4jPaths.of(if (path.startsWith("/")) path else "/" + path))
|
||||
}
|
||||
|
||||
private def getRevTree: RevTree = {
|
||||
using(repo.newObjectReader()) { reader: ObjectReader =>
|
||||
val revWalk = new RevWalk(reader)
|
||||
val id = repo.resolve(revStr)
|
||||
val commit = revWalk.parseCommit(id)
|
||||
commit.getTree
|
||||
}
|
||||
}
|
||||
|
||||
override def exists(): Boolean = {
|
||||
using(repo.newObjectReader()) { reader: ObjectReader =>
|
||||
try {
|
||||
val treeWalk = Option(TreeWalk.forPath(reader, removeInitialSlash(path), getRevTree))
|
||||
treeWalk.isDefined
|
||||
} catch {
|
||||
case e: IOException => false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override def getPath: Ec4jPath = {
|
||||
path
|
||||
}
|
||||
|
||||
override def getParent: ResourcePath = {
|
||||
Option(path.getParentPath).map { new JGitResourcePath(repo, revStr, _) }.getOrElse(null)
|
||||
}
|
||||
|
||||
override def openRandomReader(): Resource.RandomReader = {
|
||||
StringRandomReader.ofReader(openReader())
|
||||
}
|
||||
|
||||
override def openReader(): Reader = {
|
||||
using(repo.newObjectReader) { reader: ObjectReader =>
|
||||
val treeWalk = TreeWalk.forPath(reader, removeInitialSlash(path), getRevTree)
|
||||
new InputStreamReader(reader.open(treeWalk.getObjectId(0)).openStream, StandardCharsets.UTF_8)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class JGitResourcePath(repo: Repository, revStr: String, path: Ec4jPath) extends ResourcePath {
|
||||
|
||||
override def getParent: ResourcePath = {
|
||||
Option(path.getParentPath).map { new JGitResourcePath(repo, revStr, _) }.getOrElse(null)
|
||||
}
|
||||
|
||||
override def getPath: Ec4jPath = {
|
||||
path
|
||||
}
|
||||
|
||||
override def hasParent: Boolean = {
|
||||
Option(path.getParentPath).isDefined
|
||||
}
|
||||
|
||||
override def relativize(resource: Resource): Resource = {
|
||||
resource match {
|
||||
case r: JGitResource =>
|
||||
new JGitResource(repo, revStr, path.relativize(r.getPath).toString)
|
||||
}
|
||||
}
|
||||
|
||||
override def resolve(name: String): Resource = {
|
||||
Option(path)
|
||||
.map { p =>
|
||||
new JGitResource(repo, revStr, p.resolve(name))
|
||||
}
|
||||
.getOrElse {
|
||||
new JGitResource(repo, revStr, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private object JGitResourcePath {
|
||||
def RootDirectory(git: Git, revStr: String) =
|
||||
new JGitResourcePath(git.getRepository, revStr, Ec4jPath.Ec4jPaths.of("/"))
|
||||
}
|
||||
|
||||
private val TabSizeDefault: Int = 8
|
||||
private val NewLineModeDefault: String = "auto"
|
||||
private val UseSoftTabsDefault = false
|
||||
|
||||
case class EditorConfigInfo(
|
||||
tabSize: Int,
|
||||
|
||||
Reference in New Issue
Block a user