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 {
|
||||
objectId =>
|
||||
val paths = path.split("/")
|
||||
val props = EditorConfigUtil.readProperties(git, branch, path)
|
||||
val info = EditorConfigUtil.getEditorConfigInfo(git, branch, path)
|
||||
|
||||
html.editor(
|
||||
branch = branch,
|
||||
@@ -328,9 +328,9 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
||||
content = JGitUtil.getContentInfo(git, path, objectId),
|
||||
protectedBranch = protectedBranch,
|
||||
commit = revCommit.getName,
|
||||
newLineMode = EditorConfigUtil.getNewLineMode(props),
|
||||
useSoftTabs = EditorConfigUtil.getUseSoftTabs(props),
|
||||
tabSize = EditorConfigUtil.getTabWidth(props)
|
||||
newLineMode = info.newLineMode,
|
||||
useSoftTabs = info.useSoftTabs,
|
||||
tabSize = info.tabSize
|
||||
)
|
||||
} getOrElse NotFound()
|
||||
}
|
||||
@@ -442,8 +442,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
||||
// Download (This route is left for backword compatibility)
|
||||
responseRawFile(git, objectId, path, repository)
|
||||
} else {
|
||||
val props = EditorConfigUtil.readProperties(git, id, path)
|
||||
val tabSize = EditorConfigUtil.getTabWidth(props)
|
||||
val info = EditorConfigUtil.getEditorConfigInfo(git, id, path)
|
||||
html.blob(
|
||||
branch = id,
|
||||
repository = repository,
|
||||
@@ -453,7 +452,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
||||
hasWritePermission = hasDeveloperRole(repository.owner, repository.name, context.loginAccount),
|
||||
isBlame = request.paths(2) == "blame",
|
||||
isLfsFile = isLfsFile(git, objectId),
|
||||
tabSize = tabSize
|
||||
tabSize = info.tabSize
|
||||
)
|
||||
}
|
||||
} getOrElse NotFound()
|
||||
|
||||
@@ -1,17 +1,27 @@
|
||||
package gitbucket.core.util
|
||||
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.io.IOException
|
||||
|
||||
import editorconfig.{JGitResource, JGitResourcePath}
|
||||
import org.ec4j.core.model.PropertyType.{EndOfLineValue, IndentStyleValue}
|
||||
import org.ec4j.core.model.{PropertyType, Version}
|
||||
import org.ec4j.core.parser.ParseException
|
||||
import org.ec4j.core.{EditorConfigConstants, EditorConfigLoader, ResourceProperties, ResourcePropertiesService}
|
||||
import org.eclipse.jgit.api.Git
|
||||
|
||||
import collection.JavaConverters._
|
||||
|
||||
object EditorConfigUtil {
|
||||
def readProperties(git: Git, rev: String, path: String): ResourceProperties = {
|
||||
val TabSizeDefault: Int = 8
|
||||
val NewLineModeDefault: String = "auto"
|
||||
val UseSoftTabsDefault = false
|
||||
|
||||
case class EditorConfigInfo(
|
||||
tabSize: Int,
|
||||
newLineMode: String,
|
||||
useSoftTabs: Boolean
|
||||
)
|
||||
|
||||
def getEditorConfigInfo(git: Git, rev: String, path: String): EditorConfigInfo = {
|
||||
try {
|
||||
val resourcePropertiesService = ResourcePropertiesService
|
||||
.builder()
|
||||
.configFileName(EditorConfigConstants.EDITORCONFIG)
|
||||
@@ -20,27 +30,24 @@ object EditorConfigUtil {
|
||||
.keepUnset(true)
|
||||
.build()
|
||||
|
||||
resourcePropertiesService.queryProperties(new JGitResource(git, rev, path))
|
||||
}
|
||||
|
||||
def getTabWidth(props: ResourceProperties): Int = {
|
||||
props.getValue[Integer](PropertyType.tab_width, 8, false)
|
||||
}
|
||||
|
||||
def getNewLineMode(props: ResourceProperties): String = {
|
||||
props.getValue[EndOfLineValue](PropertyType.end_of_line, null, false) match {
|
||||
val props = resourcePropertiesService.queryProperties(new JGitResource(git, rev, path))
|
||||
EditorConfigInfo(
|
||||
tabSize = props.getValue[Integer](PropertyType.tab_width, TabSizeDefault, false),
|
||||
newLineMode = props.getValue[EndOfLineValue](PropertyType.end_of_line, null, false) match {
|
||||
case EndOfLineValue.cr => "cr"
|
||||
case EndOfLineValue.lf => "lf"
|
||||
case EndOfLineValue.crlf => "crlf"
|
||||
case _ => "auto"
|
||||
}
|
||||
}
|
||||
|
||||
def getUseSoftTabs(props: ResourceProperties): Boolean = {
|
||||
props.getValue[IndentStyleValue](PropertyType.indent_style, IndentStyleValue.tab, false) match {
|
||||
},
|
||||
props.getValue[IndentStyleValue](PropertyType.indent_style, null, false) match {
|
||||
case IndentStyleValue.space => true
|
||||
case IndentStyleValue.tab => 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") {
|
||||
withTestRepository { git =>
|
||||
createFile(git, "master", "README.md", "body", message = "commit1")
|
||||
val props = EditorConfigUtil.readProperties(git, "master", "test.txt")
|
||||
assert(EditorConfigUtil.getTabWidth(props) == 8)
|
||||
assert(EditorConfigUtil.getUseSoftTabs(props) == false)
|
||||
assert(EditorConfigUtil.getNewLineMode(props) == "auto")
|
||||
val info = EditorConfigUtil.getEditorConfigInfo(git, "master", "test.txt")
|
||||
assert(info.tabSize == 8)
|
||||
assert(info.useSoftTabs == false)
|
||||
assert(info.newLineMode == "auto")
|
||||
|
||||
val subdirProps = EditorConfigUtil.readProperties(git, "master", "dir1/dir2/dir3/dir4/test.txt")
|
||||
assert(EditorConfigUtil.getTabWidth(subdirProps) == 8)
|
||||
assert(EditorConfigUtil.getUseSoftTabs(subdirProps) == false)
|
||||
assert(EditorConfigUtil.getNewLineMode(subdirProps) == "auto")
|
||||
val subdirInfo = EditorConfigUtil.getEditorConfigInfo(git, "master", "dir1/dir2/dir3/dir4/test.txt")
|
||||
assert(subdirInfo.tabSize == 8)
|
||||
assert(subdirInfo.useSoftTabs == false)
|
||||
assert(subdirInfo.newLineMode == "auto")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,11 +28,22 @@ class EditorConfigUtilSpec extends FunSuite {
|
||||
withTestRepository { git =>
|
||||
createFile(git, "master", ".editorconfig", simpleConfig, message = "commit1")
|
||||
|
||||
val props = EditorConfigUtil.readProperties(git, "master", "test.txt")
|
||||
assert(EditorConfigUtil.getTabWidth(props) == 4)
|
||||
val info = EditorConfigUtil.getEditorConfigInfo(git, "master", "test.txt")
|
||||
assert(info.tabSize == 4)
|
||||
|
||||
val subdirProps = EditorConfigUtil.readProperties(git, "master", "dir1/dir2/dir3/dir4/test.txt")
|
||||
assert(EditorConfigUtil.getTabWidth(subdirProps) == 4)
|
||||
val subdirInfo = EditorConfigUtil.getEditorConfigInfo(git, "master", "dir1/dir2/dir3/dir4/test.txt")
|
||||
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