mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 14:05:52 +01:00
Handle .editorconfig parse error
This commit is contained in:
@@ -318,7 +318,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
getPathObjectId(git, path, revCommit).map {
|
getPathObjectId(git, path, revCommit).map {
|
||||||
objectId =>
|
objectId =>
|
||||||
val paths = path.split("/")
|
val paths = path.split("/")
|
||||||
val props = EditorConfigUtil.readProperties(git, branch, path)
|
val info = EditorConfigUtil.getEditorConfigInfo(git, branch, path)
|
||||||
|
|
||||||
html.editor(
|
html.editor(
|
||||||
branch = branch,
|
branch = branch,
|
||||||
@@ -328,9 +328,9 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
content = JGitUtil.getContentInfo(git, path, objectId),
|
content = JGitUtil.getContentInfo(git, path, objectId),
|
||||||
protectedBranch = protectedBranch,
|
protectedBranch = protectedBranch,
|
||||||
commit = revCommit.getName,
|
commit = revCommit.getName,
|
||||||
newLineMode = EditorConfigUtil.getNewLineMode(props),
|
newLineMode = info.newLineMode,
|
||||||
useSoftTabs = EditorConfigUtil.getUseSoftTabs(props),
|
useSoftTabs = info.useSoftTabs,
|
||||||
tabSize = EditorConfigUtil.getTabWidth(props)
|
tabSize = info.tabSize
|
||||||
)
|
)
|
||||||
} getOrElse NotFound()
|
} getOrElse NotFound()
|
||||||
}
|
}
|
||||||
@@ -442,8 +442,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
// Download (This route is left for backword compatibility)
|
// Download (This route is left for backword compatibility)
|
||||||
responseRawFile(git, objectId, path, repository)
|
responseRawFile(git, objectId, path, repository)
|
||||||
} else {
|
} else {
|
||||||
val props = EditorConfigUtil.readProperties(git, id, path)
|
val info = EditorConfigUtil.getEditorConfigInfo(git, id, path)
|
||||||
val tabSize = EditorConfigUtil.getTabWidth(props)
|
|
||||||
html.blob(
|
html.blob(
|
||||||
branch = id,
|
branch = id,
|
||||||
repository = repository,
|
repository = repository,
|
||||||
@@ -453,7 +452,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
hasWritePermission = hasDeveloperRole(repository.owner, repository.name, context.loginAccount),
|
hasWritePermission = hasDeveloperRole(repository.owner, repository.name, context.loginAccount),
|
||||||
isBlame = request.paths(2) == "blame",
|
isBlame = request.paths(2) == "blame",
|
||||||
isLfsFile = isLfsFile(git, objectId),
|
isLfsFile = isLfsFile(git, objectId),
|
||||||
tabSize = tabSize
|
tabSize = info.tabSize
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} getOrElse NotFound()
|
} getOrElse NotFound()
|
||||||
|
|||||||
@@ -1,46 +1,53 @@
|
|||||||
package gitbucket.core.util
|
package gitbucket.core.util
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets
|
import java.io.IOException
|
||||||
|
|
||||||
import editorconfig.{JGitResource, JGitResourcePath}
|
import editorconfig.{JGitResource, JGitResourcePath}
|
||||||
import org.ec4j.core.model.PropertyType.{EndOfLineValue, IndentStyleValue}
|
import org.ec4j.core.model.PropertyType.{EndOfLineValue, IndentStyleValue}
|
||||||
import org.ec4j.core.model.{PropertyType, Version}
|
import org.ec4j.core.model.{PropertyType, Version}
|
||||||
|
import org.ec4j.core.parser.ParseException
|
||||||
import org.ec4j.core.{EditorConfigConstants, EditorConfigLoader, ResourceProperties, ResourcePropertiesService}
|
import org.ec4j.core.{EditorConfigConstants, EditorConfigLoader, ResourceProperties, ResourcePropertiesService}
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
|
|
||||||
import collection.JavaConverters._
|
|
||||||
|
|
||||||
object EditorConfigUtil {
|
object EditorConfigUtil {
|
||||||
def readProperties(git: Git, rev: String, path: String): ResourceProperties = {
|
val TabSizeDefault: Int = 8
|
||||||
val resourcePropertiesService = ResourcePropertiesService
|
val NewLineModeDefault: String = "auto"
|
||||||
.builder()
|
val UseSoftTabsDefault = false
|
||||||
.configFileName(EditorConfigConstants.EDITORCONFIG)
|
|
||||||
.rootDirectory(JGitResourcePath.RootDirectory(git, rev))
|
|
||||||
.loader(EditorConfigLoader.of(Version.CURRENT))
|
|
||||||
.keepUnset(true)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
resourcePropertiesService.queryProperties(new JGitResource(git, rev, path))
|
case class EditorConfigInfo(
|
||||||
}
|
tabSize: Int,
|
||||||
|
newLineMode: String,
|
||||||
|
useSoftTabs: Boolean
|
||||||
|
)
|
||||||
|
|
||||||
def getTabWidth(props: ResourceProperties): Int = {
|
def getEditorConfigInfo(git: Git, rev: String, path: String): EditorConfigInfo = {
|
||||||
props.getValue[Integer](PropertyType.tab_width, 8, false)
|
try {
|
||||||
}
|
val resourcePropertiesService = ResourcePropertiesService
|
||||||
|
.builder()
|
||||||
|
.configFileName(EditorConfigConstants.EDITORCONFIG)
|
||||||
|
.rootDirectory(JGitResourcePath.RootDirectory(git, rev))
|
||||||
|
.loader(EditorConfigLoader.of(Version.CURRENT))
|
||||||
|
.keepUnset(true)
|
||||||
|
.build()
|
||||||
|
|
||||||
def getNewLineMode(props: ResourceProperties): String = {
|
val props = resourcePropertiesService.queryProperties(new JGitResource(git, rev, path))
|
||||||
props.getValue[EndOfLineValue](PropertyType.end_of_line, null, false) match {
|
EditorConfigInfo(
|
||||||
case EndOfLineValue.cr => "cr"
|
tabSize = props.getValue[Integer](PropertyType.tab_width, TabSizeDefault, false),
|
||||||
case EndOfLineValue.lf => "lf"
|
newLineMode = props.getValue[EndOfLineValue](PropertyType.end_of_line, null, false) match {
|
||||||
case EndOfLineValue.crlf => "crlf"
|
case EndOfLineValue.cr => "cr"
|
||||||
case _ => "auto"
|
case EndOfLineValue.lf => "lf"
|
||||||
}
|
case EndOfLineValue.crlf => "crlf"
|
||||||
}
|
case _ => "auto"
|
||||||
|
},
|
||||||
def getUseSoftTabs(props: ResourceProperties): Boolean = {
|
props.getValue[IndentStyleValue](PropertyType.indent_style, null, false) match {
|
||||||
props.getValue[IndentStyleValue](PropertyType.indent_style, IndentStyleValue.tab, false) match {
|
case IndentStyleValue.space => true
|
||||||
case IndentStyleValue.space => true
|
case IndentStyleValue.tab => false
|
||||||
case IndentStyleValue.tab => false
|
case _ => false
|
||||||
case _ => false
|
}
|
||||||
|
)
|
||||||
|
} catch {
|
||||||
|
case e: ParseException => EditorConfigInfo(TabSizeDefault, NewLineModeDefault, UseSoftTabsDefault)
|
||||||
|
case e: IOException => EditorConfigInfo(TabSizeDefault, NewLineModeDefault, UseSoftTabsDefault)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,15 +12,15 @@ class EditorConfigUtilSpec extends FunSuite {
|
|||||||
test("no EditorConfig file") {
|
test("no EditorConfig file") {
|
||||||
withTestRepository { git =>
|
withTestRepository { git =>
|
||||||
createFile(git, "master", "README.md", "body", message = "commit1")
|
createFile(git, "master", "README.md", "body", message = "commit1")
|
||||||
val props = EditorConfigUtil.readProperties(git, "master", "test.txt")
|
val info = EditorConfigUtil.getEditorConfigInfo(git, "master", "test.txt")
|
||||||
assert(EditorConfigUtil.getTabWidth(props) == 8)
|
assert(info.tabSize == 8)
|
||||||
assert(EditorConfigUtil.getUseSoftTabs(props) == false)
|
assert(info.useSoftTabs == false)
|
||||||
assert(EditorConfigUtil.getNewLineMode(props) == "auto")
|
assert(info.newLineMode == "auto")
|
||||||
|
|
||||||
val subdirProps = EditorConfigUtil.readProperties(git, "master", "dir1/dir2/dir3/dir4/test.txt")
|
val subdirInfo = EditorConfigUtil.getEditorConfigInfo(git, "master", "dir1/dir2/dir3/dir4/test.txt")
|
||||||
assert(EditorConfigUtil.getTabWidth(subdirProps) == 8)
|
assert(subdirInfo.tabSize == 8)
|
||||||
assert(EditorConfigUtil.getUseSoftTabs(subdirProps) == false)
|
assert(subdirInfo.useSoftTabs == false)
|
||||||
assert(EditorConfigUtil.getNewLineMode(subdirProps) == "auto")
|
assert(subdirInfo.newLineMode == "auto")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,11 +28,22 @@ class EditorConfigUtilSpec extends FunSuite {
|
|||||||
withTestRepository { git =>
|
withTestRepository { git =>
|
||||||
createFile(git, "master", ".editorconfig", simpleConfig, message = "commit1")
|
createFile(git, "master", ".editorconfig", simpleConfig, message = "commit1")
|
||||||
|
|
||||||
val props = EditorConfigUtil.readProperties(git, "master", "test.txt")
|
val info = EditorConfigUtil.getEditorConfigInfo(git, "master", "test.txt")
|
||||||
assert(EditorConfigUtil.getTabWidth(props) == 4)
|
assert(info.tabSize == 4)
|
||||||
|
|
||||||
val subdirProps = EditorConfigUtil.readProperties(git, "master", "dir1/dir2/dir3/dir4/test.txt")
|
val subdirInfo = EditorConfigUtil.getEditorConfigInfo(git, "master", "dir1/dir2/dir3/dir4/test.txt")
|
||||||
assert(EditorConfigUtil.getTabWidth(subdirProps) == 4)
|
assert(subdirInfo.tabSize == 4)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test(".editorconfig parse error") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
createFile(git, "master", ".editorconfig", "equal_missing", message = "commit1")
|
||||||
|
|
||||||
|
val info = EditorConfigUtil.getEditorConfigInfo(git, "master", "test.txt")
|
||||||
|
assert(info.tabSize == 8)
|
||||||
|
assert(info.useSoftTabs == false)
|
||||||
|
assert(info.newLineMode == "auto")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user