Handle .editorconfig parse error

This commit is contained in:
KOUNOIKE Yuusuke
2018-04-22 16:46:34 +09:00
parent e2d5382787
commit 58381c3d30
3 changed files with 66 additions and 49 deletions

View File

@@ -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()

View File

@@ -1,17 +1,27 @@
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 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 val resourcePropertiesService = ResourcePropertiesService
.builder() .builder()
.configFileName(EditorConfigConstants.EDITORCONFIG) .configFileName(EditorConfigConstants.EDITORCONFIG)
@@ -20,27 +30,24 @@ object EditorConfigUtil {
.keepUnset(true) .keepUnset(true)
.build() .build()
resourcePropertiesService.queryProperties(new JGitResource(git, rev, path)) val props = resourcePropertiesService.queryProperties(new JGitResource(git, rev, path))
} EditorConfigInfo(
tabSize = props.getValue[Integer](PropertyType.tab_width, TabSizeDefault, false),
def getTabWidth(props: ResourceProperties): Int = { newLineMode = props.getValue[EndOfLineValue](PropertyType.end_of_line, null, false) match {
props.getValue[Integer](PropertyType.tab_width, 8, false)
}
def getNewLineMode(props: ResourceProperties): String = {
props.getValue[EndOfLineValue](PropertyType.end_of_line, null, false) match {
case EndOfLineValue.cr => "cr" case EndOfLineValue.cr => "cr"
case EndOfLineValue.lf => "lf" case EndOfLineValue.lf => "lf"
case EndOfLineValue.crlf => "crlf" case EndOfLineValue.crlf => "crlf"
case _ => "auto" case _ => "auto"
} },
} props.getValue[IndentStyleValue](PropertyType.indent_style, null, false) match {
def getUseSoftTabs(props: ResourceProperties): Boolean = {
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)
}
} }
} }

View File

@@ -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")
} }
} }
} }