| 1 |
package org.iwakura.larcjb |
| 2 |
|
| 3 |
import com.intellij.openapi.project.ProjectManager |
| 4 |
import com.intellij.openapi.vcs.ProjectLevelVcsManager |
| 5 |
import com.intellij.openapi.vcs.VcsKey |
| 6 |
import com.intellij.openapi.vcs.VcsRootChecker |
| 7 |
import com.intellij.openapi.vfs.VirtualFile |
| 8 |
|
| 9 |
/** |
| 10 |
* Determines if a directory is a larc repository by checking for .larc directory. |
| 11 |
*/ |
| 12 |
class LarcRootChecker : VcsRootChecker() { |
| 13 |
|
| 14 |
override fun getSupportedVcs(): VcsKey { |
| 15 |
/* |
| 16 |
* Get VcsKey from any open project that has Larc VCS registered. |
| 17 |
* This is needed because VcsKey constructor is package-private. |
| 18 |
*/ |
| 19 |
val projects = ProjectManager.getInstance().openProjects |
| 20 |
for (project in projects) { |
| 21 |
val vcsManager = ProjectLevelVcsManager.getInstance(project) |
| 22 |
val vcs = vcsManager.findVcsByName(LarcVcs.NAME) |
| 23 |
if (vcs != null) { |
| 24 |
return vcs.keyInstanceMethod |
| 25 |
} |
| 26 |
} |
| 27 |
/* Fallback: return key from default project */ |
| 28 |
val defaultProject = ProjectManager.getInstance().defaultProject |
| 29 |
val vcs = ProjectLevelVcsManager.getInstance(defaultProject).findVcsByName(LarcVcs.NAME) |
| 30 |
return vcs?.keyInstanceMethod ?: throw IllegalStateException("Larc VCS not registered") |
| 31 |
} |
| 32 |
|
| 33 |
override fun isRoot(path: VirtualFile): Boolean { |
| 34 |
val larcDir = path.findChild(LarcVcs.LARC_DIR) |
| 35 |
return larcDir != null && larcDir.isDirectory |
| 36 |
} |
| 37 |
|
| 38 |
override fun isVcsDir(dirName: String): Boolean { |
| 39 |
return dirName.equals(LarcVcs.LARC_DIR, ignoreCase = false) |
| 40 |
} |
| 41 |
|
| 42 |
override fun validateRoot(path: VirtualFile): Boolean { |
| 43 |
/* |
| 44 |
* Additional validation: check for larc.db inside .larc |
| 45 |
* This distinguishes larc repos from other tools that might use .larc |
| 46 |
*/ |
| 47 |
val larcDir = path.findChild(LarcVcs.LARC_DIR) ?: return false |
| 48 |
return larcDir.findChild("larc.db") != null |
| 49 |
} |
| 50 |
} |
| 51 |
|