Skip to content

Commit

Permalink
IJPL-173307 Cannot initialize Backup & Sync: java.nio.file.NoSuchFile…
Browse files Browse the repository at this point in the history
…Exception: /settingsSync/.git/refs/heads

GitOrigin-RevId: 5e889dc1ac5dc460fa9a51ed6e2b0d69f609d9f6
  • Loading branch information
paksv authored and intellij-monorepo-bot committed Dec 10, 2024
1 parent e15d831 commit f8ee39a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ class GitSettingsLog(private val settingsSyncStorage: Path,

private fun checkLocks(dotGit: Path) {
val rootLockEntries = dotGit.listDirectoryEntries("*.lock")
val headsLockEntries = (dotGit / "refs" / "heads").listDirectoryEntries("*.lock")
val refsHeads = dotGit / "refs" / "heads"
if (!refsHeads.exists()) {
LOG.warn("refs/heads is absent under ${dotGit}. Will reinitialize the repo from scratch")
return
}
val headsLockEntries = refsHeads.listDirectoryEntries("*.lock")
for (lock in rootLockEntries + headsLockEntries) {
if (System.currentTimeMillis() - lock.getLastModifiedTime().toMillis() > FIVE_SECONDS) {
FileUtil.delete(lock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.intellij.settingsSync
import com.intellij.idea.TestFor
import com.intellij.openapi.components.SettingsCategory
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.io.FileUtil
import com.intellij.settingsSync.SettingsSnapshot.AppInfo
import com.intellij.settingsSync.communicator.SettingsSyncUserData
import com.intellij.testFramework.ApplicationRule
Expand Down Expand Up @@ -567,6 +568,34 @@ internal class GitSettingsLogTest {
@Test
@TestFor(issues = ["IJPL-13080"])
fun `drop and reinit settings sync if cannot init`() {
val size = 16384
drop_and_reinit_base( {
val indexFile = getRepository().indexFile
val zeroBytesArray = ByteArray(size)
indexFile.writeBytes(zeroBytesArray)
assertEquals(size.toLong(), indexFile.length())
}, {
val indexFile = getRepository().indexFile
assertNotEquals(size, indexFile.length())
})
}

@Test
@TestFor(issues = ["IJPL-173307"])
fun `drop and reinit settings sync if cannot init 2`() {
drop_and_reinit_base( {
val dir = getRepository().directory
val refs = dir.resolve("refs")
FileUtil.deleteRecursively(refs.toPath())
assertFalse(refs.exists())
}, {
val dir = getRepository().directory
val refs = dir.resolve("refs")
assertTrue(refs.exists())
})
}

private fun drop_and_reinit_base(damageAction: ()->Unit, verifyAction: ()->Unit) {
val editorXml = (configDir / "options" / "editor.xml").createParentDirectories().createFile()
val editorContent = "editorContent"
val state1 = "State 1"
Expand All @@ -577,15 +606,16 @@ internal class GitSettingsLogTest {
settingsLog.applyIdeState(settingsSnapshot {
fileState("options/editor.xml", state1)
}, "Local changes")
val indexFile = getRepository().indexFile

val size = 16384
val zeroBytesArray = ByteArray(size)
indexFile.writeBytes(zeroBytesArray)
assertEquals(size.toLong(), indexFile.length())

val editorXmlSync = settingsSyncStorage / "options" / "editor.xml"
assertEquals(state1, editorXmlSync.readText())


settingsLog.applyIdeState(settingsSnapshot {
fileState("options/editor.xml", state1)
}, "Local changes")

damageAction()

try {
DirCache.read(getRepository())
}
Expand All @@ -594,15 +624,17 @@ internal class GitSettingsLogTest {

initializeGitSettingsLog(editorXml)
getRepository().indexFile.length()
assertNotEquals(size, indexFile.length())
assertTrue(editorXmlSync.exists() && editorXmlSync.readText() == editorContent)

verifyAction()

try {
DirCache.read(getRepository())
}
catch (ex: Exception) {
fail("Shouldn't fail: ${ex.message}")
}

}

@Test
Expand Down

0 comments on commit f8ee39a

Please sign in to comment.