|
| 1 | +const util = require("util"); |
| 2 | +const exec = util.promisify(require("child_process").exec); |
| 3 | +const fs = require("fs"); |
| 4 | +const path = require("path"); |
| 5 | + |
| 6 | +class gitHelper { |
| 7 | + constructor() { |
| 8 | + this.gitRepos = []; |
| 9 | + this.baseDir = path.normalize(__dirname + "/../../../"); |
| 10 | + if (process.env.JEST_WORKER_ID === undefined) { |
| 11 | + this.Log = require("logger"); |
| 12 | + } else { |
| 13 | + // if we are running with jest |
| 14 | + this.Log = require("../../../tests/unit/mocks/logger.js"); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + getRefRegex(branch) { |
| 19 | + return new RegExp("s*([a-z,0-9]+[.][.][a-z,0-9]+) " + branch, "g"); |
| 20 | + } |
| 21 | + |
| 22 | + async execShell(command) { |
| 23 | + let res = { stdout: "", stderr: "" }; |
| 24 | + const { stdout, stderr } = await exec(command); |
| 25 | + |
| 26 | + res.stdout = stdout; |
| 27 | + res.stderr = stderr; |
| 28 | + return res; |
| 29 | + } |
| 30 | + |
| 31 | + async isGitRepo(moduleFolder) { |
| 32 | + let res = await this.execShell("cd " + moduleFolder + " && git remote -v"); |
| 33 | + if (res.stderr) { |
| 34 | + this.Log.error("Failed to fetch git data for " + moduleFolder + ": " + res.stderr); |
| 35 | + return false; |
| 36 | + } else { |
| 37 | + return true; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + add(moduleName) { |
| 42 | + let moduleFolder = this.baseDir; |
| 43 | + if (moduleName !== "default") { |
| 44 | + moduleFolder = moduleFolder + "modules/" + moduleName; |
| 45 | + } |
| 46 | + try { |
| 47 | + this.Log.info("Checking git for module: " + moduleName); |
| 48 | + // Throws error if file doesn't exist |
| 49 | + fs.statSync(path.join(moduleFolder, ".git")); |
| 50 | + // Fetch the git or throw error if no remotes |
| 51 | + if (this.isGitRepo(moduleFolder)) { |
| 52 | + // Folder has .git and has at least one git remote, watch this folder |
| 53 | + this.gitRepos.unshift({ module: moduleName, folder: moduleFolder }); |
| 54 | + } |
| 55 | + } catch (err) { |
| 56 | + // Error when directory .git doesn't exist or doesn't have any remotes |
| 57 | + // This module is not managed with git, skip |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + async getStatusInfo(repo) { |
| 62 | + let gitInfo = { |
| 63 | + module: repo.module, |
| 64 | + // commits behind: |
| 65 | + behind: 0, |
| 66 | + // branch name: |
| 67 | + current: "", |
| 68 | + // current hash: |
| 69 | + hash: "", |
| 70 | + // remote branch: |
| 71 | + tracking: "", |
| 72 | + isBehindInStatus: false |
| 73 | + }; |
| 74 | + let res; |
| 75 | + if (repo.module === "default") { |
| 76 | + // the hash is only needed for the mm repo |
| 77 | + res = await this.execShell("cd " + repo.folder + " && git rev-parse HEAD"); |
| 78 | + if (res.stderr) { |
| 79 | + this.Log.error("Failed to get current commit hash for " + repo.module + ": " + res.stderr); |
| 80 | + } |
| 81 | + gitInfo.hash = res.stdout; |
| 82 | + } |
| 83 | + res = await this.execShell("cd " + repo.folder + " && git status -sb"); |
| 84 | + if (res.stderr) { |
| 85 | + this.Log.error("Failed to get git status for " + repo.module + ": " + res.stderr); |
| 86 | + // exit without git status info |
| 87 | + return; |
| 88 | + } |
| 89 | + // only the first line of stdout is evaluated |
| 90 | + let status = res.stdout.split("\n")[0]; |
| 91 | + // examples for status: |
| 92 | + // ## develop...origin/develop |
| 93 | + // ## master...origin/master [behind 8] |
| 94 | + status = status.match(/(?![.#])([^.]*)/g); |
| 95 | + // examples for status: |
| 96 | + // [ ' develop', 'origin/develop', '' ] |
| 97 | + // [ ' master', 'origin/master [behind 8]', '' ] |
| 98 | + gitInfo.current = status[0].trim(); |
| 99 | + status = status[1].split(" "); |
| 100 | + // examples for status: |
| 101 | + // [ 'origin/develop' ] |
| 102 | + // [ 'origin/master', '[behind', '8]' ] |
| 103 | + gitInfo.tracking = status[0].trim(); |
| 104 | + if (status[2]) { |
| 105 | + // git fetch was already called before so `git status -sb` delivers already the behind number |
| 106 | + gitInfo.behind = parseInt(status[2].substring(0, status[2].length - 1)); |
| 107 | + gitInfo.isBehindInStatus = true; |
| 108 | + } |
| 109 | + return gitInfo; |
| 110 | + } |
| 111 | + |
| 112 | + async getRepoInfo(repo) { |
| 113 | + let gitInfo = await this.getStatusInfo(repo); |
| 114 | + if (!gitInfo) { |
| 115 | + return; |
| 116 | + } |
| 117 | + if (gitInfo.isBehindInStatus) { |
| 118 | + return gitInfo; |
| 119 | + } |
| 120 | + let res = await this.execShell("cd " + repo.folder + " && git fetch --dry-run"); |
| 121 | + // example output: |
| 122 | + // From https://github.com/MichMich/MagicMirror |
| 123 | + // e40ddd4..06389e3 develop -> origin/develop |
| 124 | + // here the result is in stderr (this is a git default, don't ask why ...) |
| 125 | + const matches = res.stderr.match(this.getRefRegex(gitInfo.current)); |
| 126 | + if (!matches || !matches[0]) { |
| 127 | + // no refs found, nothing to do |
| 128 | + return; |
| 129 | + } |
| 130 | + // get behind with refs |
| 131 | + res = await this.execShell("cd " + repo.folder + " && git rev-list --ancestry-path --count " + matches[0]); |
| 132 | + gitInfo.behind = parseInt(res.stdout); |
| 133 | + return gitInfo; |
| 134 | + } |
| 135 | + |
| 136 | + async getStatus() { |
| 137 | + const gitResultList = []; |
| 138 | + for (let repo of this.gitRepos) { |
| 139 | + const gitInfo = await this.getStatusInfo(repo); |
| 140 | + if (gitInfo) { |
| 141 | + gitResultList.unshift(gitInfo); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return gitResultList; |
| 146 | + } |
| 147 | + |
| 148 | + async getRepos() { |
| 149 | + const gitResultList = []; |
| 150 | + for (let repo of this.gitRepos) { |
| 151 | + const gitInfo = await this.getRepoInfo(repo); |
| 152 | + if (gitInfo) { |
| 153 | + gitResultList.unshift(gitInfo); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return gitResultList; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +module.exports.gitHelper = gitHelper; |
0 commit comments