|
| 1 | +package cfig.lazybox.staging |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import java.io.ByteArrayOutputStream |
| 5 | +import org.apache.commons.exec.CommandLine |
| 6 | +import org.apache.commons.exec.DefaultExecutor |
| 7 | +import org.apache.commons.exec.ExecuteWatchdog |
| 8 | +import org.apache.commons.exec.PumpStreamHandler |
| 9 | +import org.slf4j.LoggerFactory |
| 10 | + |
| 11 | +class RepoWorker { |
| 12 | + private val log = LoggerFactory.getLogger(RepoWorker::class.simpleName) |
| 13 | + fun lfsPullRepo(args: Array<String>) { |
| 14 | + val startPath = args.firstOrNull() ?: "." |
| 15 | + val dirList = getMatchRepositories(startPath, ::isLfsEnabled, "LFS Enabled") |
| 16 | + log.warn("Found ${dirList.size} repositories with LFS enabled.") |
| 17 | + dirList.forEach { repoDir -> |
| 18 | + val relativePath = repoDir.toRelativeString(File(startPath).canonicalFile).ifEmpty { "." } |
| 19 | + log.info("Pulling [$relativePath]...") |
| 20 | + pullLfsContent(repoDir) |
| 21 | + } |
| 22 | + log.info("✨ Scan and pull complete.") |
| 23 | + } |
| 24 | + |
| 25 | + fun unshallowRepo(args: Array<String>) { |
| 26 | + val startPath = args.firstOrNull() ?: "." |
| 27 | + val dirList = getMatchRepositories(startPath, ::isShallowClone, "Shallow Clone") |
| 28 | + log.warn("Found ${dirList.size} shallow repositories.") |
| 29 | + dirList.forEach { repoDir -> |
| 30 | + val relativePath = repoDir.toRelativeString(File(startPath).canonicalFile).ifEmpty { "." } |
| 31 | + log.info("Unshallowing [$relativePath]...") |
| 32 | + unshallowGit(repoDir) |
| 33 | + } |
| 34 | + log.info("✨ Scan and unshallow complete.") |
| 35 | + } |
| 36 | + |
| 37 | + private fun getMatchRepositories( |
| 38 | + startPath: String, |
| 39 | + checker: (File) -> Boolean, |
| 40 | + checkerName: String = "LFS Enabled" |
| 41 | + ): List<File> { |
| 42 | + val ret = mutableListOf<File>() |
| 43 | + val startDir = File(startPath).canonicalFile |
| 44 | + log.info("🔍 Finding Git repositories using 'repo forall' in: ${startDir.absolutePath}") |
| 45 | + val gitRepositories = findGitRepositoriesWithRepo(startDir) |
| 46 | + if (gitRepositories.isEmpty()) { |
| 47 | + log.info("No Git repositories found. Make sure you are running this in a directory managed by 'repo'.") |
| 48 | + return listOf<File>() |
| 49 | + } |
| 50 | + log.info("✅ Found ${gitRepositories.size} Git repositories. Now checking for $checkerName status ...") |
| 51 | + gitRepositories.forEach { repoDir -> |
| 52 | + val relativePath = repoDir.toRelativeString(startDir).ifEmpty { "." } |
| 53 | + if (checker(repoDir)) { |
| 54 | + log.info("Checking [$relativePath]...") |
| 55 | + log.info(" -> ✅ $checkerName") |
| 56 | + ret.add(repoDir) |
| 57 | + } else { |
| 58 | + //log.info("Checking [$relativePath]...") |
| 59 | + //log.info(" -> 🔴 LFS Not Enabled.") |
| 60 | + } |
| 61 | + } |
| 62 | + return ret |
| 63 | + } |
| 64 | + |
| 65 | + private fun pullLfsContent(repoDir: File) { |
| 66 | + try { |
| 67 | + val commandLine = CommandLine("git").also { |
| 68 | + it.addArgument("lfs") |
| 69 | + it.addArgument("pull") |
| 70 | + } |
| 71 | + DefaultExecutor().also { |
| 72 | + //it.watchdog = ExecuteWatchdog(600000) |
| 73 | + it.streamHandler = PumpStreamHandler(System.out, System.err) |
| 74 | + it.workingDirectory = repoDir |
| 75 | + it.setExitValue(0) |
| 76 | + it.execute(commandLine) |
| 77 | + } |
| 78 | + log.info(" -> ✅ 'git lfs pull' completed successfully.") |
| 79 | + } catch (e: Exception) { |
| 80 | + log.error(" -> ❌ 'git lfs pull' failed for ${repoDir.name}: ${e.message}") |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private fun unshallowGit(repoDir: File) { |
| 85 | + try { |
| 86 | + val commandLine = CommandLine("git").also { |
| 87 | + it.addArgument("fetch") |
| 88 | + it.addArgument("--progress") |
| 89 | + it.addArgument("--unshallow") |
| 90 | + } |
| 91 | + DefaultExecutor().also { |
| 92 | + //it.watchdog = ExecuteWatchdog(180000) |
| 93 | + it.streamHandler = PumpStreamHandler(System.out, System.err) |
| 94 | + it.workingDirectory = repoDir |
| 95 | + it.setExitValue(0) |
| 96 | + it.execute(commandLine) |
| 97 | + } |
| 98 | + log.info(" -> ✅ 'git fetch --unshallow' completed successfully.") |
| 99 | + } catch (e: Exception) { |
| 100 | + log.error(" -> ❌ 'git fetch --unshallow' failed for ${repoDir.name}: ${e.message}") |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private fun findGitRepositoriesWithRepo(workingDir: File): List<File> { |
| 105 | + return try { |
| 106 | + val commandLine = CommandLine("repo").also { |
| 107 | + it.addArgument("forall") |
| 108 | + it.addArgument("-c") |
| 109 | + it.addArgument("pwd") |
| 110 | + } |
| 111 | + val outputStream = ByteArrayOutputStream() |
| 112 | + DefaultExecutor().also { |
| 113 | + it.watchdog = ExecuteWatchdog(60000) // 60 seconds |
| 114 | + it.streamHandler = PumpStreamHandler(outputStream) |
| 115 | + it.workingDirectory = workingDir |
| 116 | + it.setExitValue(0) |
| 117 | + it.execute(commandLine) |
| 118 | + } |
| 119 | + val output = outputStream.toString().trim() |
| 120 | + if (output.isEmpty()) { |
| 121 | + emptyList() |
| 122 | + } else { |
| 123 | + output.split(System.lineSeparator()) |
| 124 | + .map { it.trim() } |
| 125 | + .filter { it.isNotEmpty() } |
| 126 | + .map { File(it) } |
| 127 | + } |
| 128 | + } catch (e: Exception) { |
| 129 | + log.error("Error executing 'repo forall': ${e.message}") |
| 130 | + log.error("Please ensure the 'repo' tool is installed and you are in a valid repo workspace.") |
| 131 | + emptyList() |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private fun isLfsEnabled(repoDir: File): Boolean { |
| 136 | + return try { |
| 137 | + val commandLine = CommandLine("git").also { |
| 138 | + it.addArgument("lfs") |
| 139 | + it.addArgument("track") |
| 140 | + } |
| 141 | + val outputStream = ByteArrayOutputStream() |
| 142 | + DefaultExecutor().also { |
| 143 | + it.watchdog = ExecuteWatchdog(5000) |
| 144 | + it.streamHandler = PumpStreamHandler(outputStream) |
| 145 | + it.workingDirectory = repoDir |
| 146 | + it.setExitValue(0) |
| 147 | + it.execute(commandLine) |
| 148 | + } |
| 149 | + val output = outputStream.toString() |
| 150 | + val lines = output.lines().filter { it.isNotBlank() } |
| 151 | + return lines.size > 1 |
| 152 | + } catch (e: Exception) { |
| 153 | + false |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + fun isShallowClone(repoDir: File): Boolean { |
| 158 | + if (!repoDir.isDirectory) { |
| 159 | + return false |
| 160 | + } |
| 161 | + return try { |
| 162 | + val commandLine = CommandLine("git").also { |
| 163 | + it.addArgument("rev-parse") |
| 164 | + it.addArgument("--is-shallow-repository") |
| 165 | + } |
| 166 | + val outputStream = ByteArrayOutputStream() |
| 167 | + val executor = DefaultExecutor().also { |
| 168 | + it.watchdog = ExecuteWatchdog(5000) |
| 169 | + it.streamHandler = PumpStreamHandler(outputStream) |
| 170 | + it.workingDirectory = repoDir |
| 171 | + it.setExitValue(0) |
| 172 | + } |
| 173 | + executor.execute(commandLine) |
| 174 | + val output = outputStream.toString().trim() |
| 175 | + return output.equals("true", ignoreCase = true) |
| 176 | + |
| 177 | + } catch (e: Exception) { |
| 178 | + false |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments