feat: 细化 Git 状态诊断并升级 0.8.1
插件区分本机未安装 Git、目录非 Git 仓库与所有权被拒并显示具体原因;服务端提示改为“本机未提供 Git 状态”。同步文档当前版本,0.8.1 已部署 192.168.5.222 与本机,插件 38 项测试通过。
This commit is contained in:
1 parent
b46ebd1070
commit
95cb8b4e83
14 files changed
+87
-16
No files matched your search
@@ -3,7 +3,7 @@ plugins {
|
||||
id("org.jetbrains.intellij.platform") version "2.5.0"
|
||||
}
|
||||
group = "studio.tongtong"
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
repositories {
|
||||
mavenCentral()
|
||||
intellijPlatform { defaultRepositories() }
|
||||
|
||||
@@ -335,7 +335,7 @@ class RemotePanel(private val project: Project) : JPanel(BorderLayout()), Dispos
|
||||
val checkBody = mapOf("workspacePath" to cfg.remoteFolder, "projectId" to cfg.projectId, "git" to localGit, "javaHome" to cfg.remoteJavaHome, "dedicated" to dedicated)
|
||||
val inspection = json(base, secret, "POST", "/projects/check", checkBody)
|
||||
append("远程位置:${inspection.get("workspacePath").asString}")
|
||||
append("Git 检查:${inspection.get("message").asString};本机=$localGit;编译工作区基线=${inspection.get("git")}")
|
||||
append("Git 检查:${inspection.get("message").asString};本机=${SourceFiles.describe(source, localGit)};编译工作区基线=${inspection.get("git")}")
|
||||
check(inspection.get("canSync").asBoolean) { inspection.get("message").asString }
|
||||
inspection.getAsJsonObject("preflight")?.let { report ->
|
||||
append(PreflightChecks.describe(report))
|
||||
|
||||
@@ -325,7 +325,7 @@ private class RemoteSettingsPanel(private val project: Project) : JPanel(BorderL
|
||||
val response = ServerTls.client(cert).send(req, java.net.http.HttpResponse.BodyHandlers.ofString())
|
||||
check(response.statusCode() == 200) { ServerErrors.describe(response.statusCode(), "/projects/check", response.body()) }
|
||||
val remote = com.google.gson.Gson().fromJson(response.body(), com.google.gson.JsonObject::class.java)
|
||||
"${remote.get("message").asString}\n服务端位置:${remote.get("workspacePath").asString}\n本机 Git:$local\n编译工作区基线:${remote.get("git")}\n${PreflightChecks.describe(remote.getAsJsonObject("preflight"))}\n(上游差异使用已有本地引用,检查不会 fetch/pull,也不会修改文件)"
|
||||
"${remote.get("message").asString}\n服务端位置:${remote.get("workspacePath").asString}\n本机 Git:${SourceFiles.describe(source, local)}\n编译工作区基线:${remote.get("git")}\n${PreflightChecks.describe(remote.getAsJsonObject("preflight"))}\n(上游差异使用已有本地引用,检查不会 fetch/pull,也不会修改文件)"
|
||||
}.getOrElse { (it.message ?: it.javaClass.simpleName).let { message -> if (secret.isEmpty()) message else message.replace(secret, "***") } }
|
||||
ApplicationManager.getApplication().invokeLater { if (!project.isDisposed) { checkRemote.isEnabled = true; checkResult.text = result } }
|
||||
}
|
||||
|
||||
@@ -15,7 +15,9 @@ object SourceFiles {
|
||||
return parts.any { it in excluded } || leaf == "local.properties" || leaf.endsWith(".jks") || leaf.endsWith(".keystore") ||
|
||||
leaf.startsWith(".env") || leaf in setOf("key.properties", "keystore.properties")
|
||||
}
|
||||
internal fun git(root: Path, args: List<String>, input: ByteArray = byteArrayOf()): Pair<Int, String> {
|
||||
internal data class GitResult(val code: Int, val output: String, val error: String)
|
||||
|
||||
internal fun gitResult(root: Path, args: List<String>, input: ByteArray = byteArrayOf()): GitResult {
|
||||
val output = Files.createTempFile("remotecompile-git-", ".out")
|
||||
val stderr = Files.createTempFile("remotecompile-git-", ".err")
|
||||
val stdin = Files.createTempFile("remotecompile-git-", ".in")
|
||||
@@ -24,18 +26,40 @@ object SourceFiles {
|
||||
val process = ProcessBuilder(listOf("git", "-C", root.toString()) + args)
|
||||
.redirectInput(stdin.toFile()).redirectOutput(output.toFile()).redirectError(stderr.toFile()).start()
|
||||
if (!process.waitFor(60, TimeUnit.SECONDS)) { process.destroyForcibly(); error("Git 检查超时") }
|
||||
return process.exitValue() to Files.readString(output)
|
||||
return GitResult(process.exitValue(), Files.readString(output), Files.readString(stderr))
|
||||
} finally { Files.deleteIfExists(output); Files.deleteIfExists(stderr); Files.deleteIfExists(stdin) }
|
||||
}
|
||||
internal fun git(root: Path, args: List<String>, input: ByteArray = byteArrayOf()): Pair<Int, String> =
|
||||
gitResult(root, args, input).let { it.code to it.output }
|
||||
private fun gitError(result: GitResult): String =
|
||||
result.error.lineSequence().map { it.trim() }.firstOrNull { it.isNotEmpty() } ?: "git 退出码 ${result.code}"
|
||||
fun snapshot(root: Path): GitSnapshot? {
|
||||
val repo = try { git(root, listOf("rev-parse", "--is-inside-work-tree")) } catch (_: java.io.IOException) { return null }
|
||||
if (repo.first != 0 || repo.second.trim() != "true") return null
|
||||
val repo = try { gitResult(root, listOf("rev-parse", "--is-inside-work-tree")) } catch (_: java.io.IOException) { return null }
|
||||
if (repo.code != 0 || repo.output.trim() != "true") return null
|
||||
fun read(vararg args: String) = git(root, args.toList()).let { if (it.first == 0) it.second.trim() else "" }
|
||||
check(git(root, listOf("status", "--porcelain", "--untracked-files=normal")).first == 0) { "无法读取本机 Git 状态" }
|
||||
val status = gitResult(root, listOf("status", "--porcelain", "--untracked-files=normal"))
|
||||
check(status.code == 0) { "无法读取本机 Git 状态:${gitError(status)}" }
|
||||
return GitSnapshot(read("rev-parse", "--verify", "HEAD"), read("symbolic-ref", "--short", "-q", "HEAD"),
|
||||
read("status", "--porcelain", "--untracked-files=normal").isNotEmpty(),
|
||||
status.output.isNotEmpty(),
|
||||
read("rev-parse", "--abbrev-ref", "@{upstream}"), read("rev-list", "--left-right", "--count", "HEAD...@{upstream}"))
|
||||
}
|
||||
/** null when [snapshot] can read the work tree; otherwise a user-facing reason for the check result. */
|
||||
fun unavailableReason(root: Path): String? {
|
||||
val repo = try { gitResult(root, listOf("rev-parse", "--is-inside-work-tree")) }
|
||||
catch (_: java.io.IOException) { return "IDE 进程找不到 git 命令;请安装 Git 后重启 IDE" }
|
||||
if (repo.code == 0 && repo.output.trim() == "true") {
|
||||
val status = gitResult(root, listOf("status", "--porcelain", "--untracked-files=normal"))
|
||||
return if (status.code == 0) null else "仓库可识别,但读取状态失败:${gitError(status)}"
|
||||
}
|
||||
val message = repo.error.lowercase()
|
||||
return when {
|
||||
"dubious ownership" in message -> "Git 因仓库所有权(dubious ownership)拒绝访问;请确认目录所有者,或为 Git 配置 safe.directory"
|
||||
"not a git repository" in message -> "本机该目录及其上级目录都不是 Git 仓库;复制工程时可能丢失了 .git"
|
||||
else -> "本机无法识别 Git 工作树:${gitError(repo)}"
|
||||
}
|
||||
}
|
||||
fun describe(root: Path, snapshot: GitSnapshot?): String =
|
||||
snapshot?.toString() ?: "未提供(${unavailableReason(root) ?: "原因未知"})"
|
||||
fun collect(root: Path, gitSnapshot: GitSnapshot?): List<Path> {
|
||||
val candidates = mutableListOf<Path>()
|
||||
var hasIgnore = false
|
||||
@@ -55,7 +79,9 @@ object SourceFiles {
|
||||
return FileVisitResult.CONTINUE
|
||||
}
|
||||
})
|
||||
check(gitSnapshot != null || !hasIgnore) { "存在 .gitignore,但无法读取 Git 工作树;请安装 Git 并确认该工程属于 Git 仓库,避免误传忽略文件" }
|
||||
check(gitSnapshot != null || !hasIgnore) {
|
||||
"存在 .gitignore,但无法读取 Git 工作树:${unavailableReason(root) ?: "原因未知"};为避免忽略规则失效误传密钥,已停止"
|
||||
}
|
||||
val names = candidates.map { root.relativize(it).joinToString("/") }
|
||||
val ignored = if (gitSnapshot == null || names.isEmpty()) emptySet() else {
|
||||
val result = git(root, listOf("check-ignore", "--no-index", "-z", "--stdin"), (names.joinToString("\u0000") + "\u0000").toByteArray(Charsets.UTF_8))
|
||||
|
||||
@@ -29,4 +29,21 @@ class SourceFilesTest {
|
||||
try { Files.writeString(root.resolve(".gitignore"), "private/\n"); assertThrows(IllegalStateException::class.java) { SourceFiles.collect(root, null) } }
|
||||
finally { root.toFile().deleteRecursively() }
|
||||
}
|
||||
@Test fun describesWhyLocalGitStateIsUnavailable() {
|
||||
val repo = Files.createTempDirectory("remote-git-ok")
|
||||
val plain = Files.createTempDirectory("remote-git-absent")
|
||||
try {
|
||||
assertEquals(0, SourceFiles.git(repo, listOf("init", "-q")).first)
|
||||
assertNull(SourceFiles.unavailableReason(repo))
|
||||
assertFalse(SourceFiles.describe(repo, SourceFiles.snapshot(repo)).startsWith("未提供"))
|
||||
Files.writeString(plain.resolve(".gitignore"), "private/\n")
|
||||
val reason = SourceFiles.unavailableReason(plain)
|
||||
assertNotNull(reason)
|
||||
assertTrue(reason!!.contains("不是 Git 仓库"))
|
||||
assertTrue(SourceFiles.describe(plain, null).contains("未提供"))
|
||||
} finally {
|
||||
repo.toFile().deleteRecursively()
|
||||
plain.toFile().deleteRecursively()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user