|
| 1 | +import * as path from 'path'; |
| 2 | +import ini = require('ini'); |
| 3 | +import * as fse from 'fs-extra'; |
| 4 | +import * as globby from 'globby'; |
| 5 | +import { IAddUserConfig, IUserConfig } from '../types/git'; |
| 6 | +import { GLOBAL_GITCONFIG_PATH, TOOLKIT_USER_GIT_CONFIG_DIR } from '../constants'; |
| 7 | +import log from '../utils/log'; |
| 8 | +import { |
| 9 | + updateSSHConfig, |
| 10 | + getSSHConfig, |
| 11 | + removeSSHConfig, |
| 12 | + addSSHConfig, |
| 13 | +} from './ssh'; |
| 14 | + |
| 15 | +const USER_GIT_CONFIG_FILENAME_PREFIX = '.gitconfig-'; |
| 16 | +const IGNORE_CONFIG_KEYS = ['gitDir']; |
| 17 | + |
| 18 | +export async function getGlobalGitConfig() { |
| 19 | + const globalGitConfig = await parseGitConfig(GLOBAL_GITCONFIG_PATH); |
| 20 | + log.info('get-global-git-config', globalGitConfig); |
| 21 | + return globalGitConfig; |
| 22 | +} |
| 23 | + |
| 24 | +export async function updateGlobalGitConfig(gitConfig: object) { |
| 25 | + log.info('update-global-git-config', gitConfig); |
| 26 | + await writeGitConfig(GLOBAL_GITCONFIG_PATH, gitConfig); |
| 27 | +} |
| 28 | + |
| 29 | +export async function getExistedUserGitConfigNames() { |
| 30 | + const filenames = await getUserGitConfigFilenames(); |
| 31 | + return filenames.map((filename: string) => filename.replace(USER_GIT_CONFIG_FILENAME_PREFIX, '')); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * get user git config list |
| 36 | + */ |
| 37 | +export async function getUserGitConfigs(): Promise<IUserConfig[]> { |
| 38 | + const gitConfigFilenames = await getUserGitConfigFilenames(); |
| 39 | + const userGitDirs = await getUserGitDirs(); |
| 40 | + |
| 41 | + const userGitConfigs = []; |
| 42 | + for (const gitConfigFilename of gitConfigFilenames) { |
| 43 | + const configPath = path.join(TOOLKIT_USER_GIT_CONFIG_DIR, gitConfigFilename); |
| 44 | + if (!fse.pathExistsSync(configPath)) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + const gitConfig = await parseGitConfig(configPath); |
| 48 | + const filename = path.basename(configPath); |
| 49 | + const configName = filename.replace(USER_GIT_CONFIG_FILENAME_PREFIX, ''); |
| 50 | + const { SSHPublicKey } = await getSSHConfig(configName); |
| 51 | + userGitConfigs.push({ |
| 52 | + ...gitConfig, |
| 53 | + SSHPublicKey, |
| 54 | + configName, |
| 55 | + gitDirs: userGitDirs[configPath] || [], |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + return userGitConfigs; |
| 60 | +} |
| 61 | + |
| 62 | +export async function addUserGitConfig(userGitConfig: IAddUserConfig) { |
| 63 | + const { configName, user: { name: userName, hostName } } = userGitConfig; |
| 64 | + const gitConfigPath = getGitConfigPath(configName); |
| 65 | + |
| 66 | + checkUserGitConfigExists(configName, gitConfigPath); |
| 67 | + |
| 68 | + await fse.createFile(gitConfigPath); |
| 69 | + // do not save the configName to the gitconfig file |
| 70 | + delete userGitConfig.configName; |
| 71 | + await writeGitConfig(gitConfigPath, userGitConfig); |
| 72 | + await addSSHConfig({ hostName, configName, userName }); |
| 73 | +} |
| 74 | + |
| 75 | +export async function updateUserGitConfig(gitConfig: any, configName: string) { |
| 76 | + const { user = {} } = gitConfig; |
| 77 | + const { name: userName = '', hostName = '' } = user; |
| 78 | + await updateSSHConfig(configName, hostName, userName); |
| 79 | + |
| 80 | + IGNORE_CONFIG_KEYS.forEach((key) => { |
| 81 | + delete gitConfig[key]; |
| 82 | + }); |
| 83 | + // save to ~/.toolkit/git/.gitconfig-${configName} |
| 84 | + const gitConfigPath = `${path.join(TOOLKIT_USER_GIT_CONFIG_DIR, `${USER_GIT_CONFIG_FILENAME_PREFIX}${configName}`)}`; |
| 85 | + await writeGitConfig(gitConfigPath, gitConfig); |
| 86 | + |
| 87 | + log.info('update-user-git-config', configName, gitConfig); |
| 88 | +} |
| 89 | + |
| 90 | +async function getUserGitDirs() { |
| 91 | + const globalGitConfig = await getGlobalGitConfig(); |
| 92 | + |
| 93 | + const userGitDirs = {}; |
| 94 | + |
| 95 | + const configKeys = Object.keys(globalGitConfig); |
| 96 | + |
| 97 | + for (const configKey of configKeys) { |
| 98 | + const { path: gitConfigPath } = globalGitConfig[configKey]; |
| 99 | + if (!gitConfigPath) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + if (!userGitDirs[gitConfigPath]) { |
| 103 | + userGitDirs[gitConfigPath] = []; |
| 104 | + } |
| 105 | + const gitDir = configKey.replace(/includeIf "gitdir:(.*)"/, (match, p1) => p1); |
| 106 | + userGitDirs[gitConfigPath].push(gitDir); |
| 107 | + } |
| 108 | + |
| 109 | + return userGitDirs; |
| 110 | +} |
| 111 | + |
| 112 | +export async function updateUserGitDir( |
| 113 | + originGitDir: string, |
| 114 | + currentGitDir: string, |
| 115 | + configName: string, |
| 116 | +) { |
| 117 | + const globalGitConfig = await parseGitConfig(GLOBAL_GITCONFIG_PATH); |
| 118 | + |
| 119 | + const originIncludeIfKey = `includeIf "gitdir:${originGitDir}"`; |
| 120 | + const currentIncludeIfKey = `includeIf "gitdir:${currentGitDir}"`; |
| 121 | + |
| 122 | + delete globalGitConfig[originIncludeIfKey]; |
| 123 | + const gitConfigPath = getGitConfigPath(configName); |
| 124 | + globalGitConfig[currentIncludeIfKey] = { path: gitConfigPath }; |
| 125 | + await writeGitConfig(GLOBAL_GITCONFIG_PATH, globalGitConfig); |
| 126 | + |
| 127 | + log.info('update-user-git-dir: ', currentIncludeIfKey, globalGitConfig[currentIncludeIfKey]); |
| 128 | +} |
| 129 | + |
| 130 | +export async function removeUserGitDir(gitDir: string, configName: string) { |
| 131 | + const gitConfigPath = getGitConfigPath(configName); |
| 132 | + const globalGitConfig = await parseGitConfig(GLOBAL_GITCONFIG_PATH); |
| 133 | + |
| 134 | + const includeIfKey = `includeIf "gitdir:${gitDir}"`; |
| 135 | + const includeIfValue = globalGitConfig[includeIfKey]; |
| 136 | + if (includeIfValue && includeIfValue.path === gitConfigPath) { |
| 137 | + delete globalGitConfig[includeIfKey]; |
| 138 | + await writeGitConfig(GLOBAL_GITCONFIG_PATH, globalGitConfig); |
| 139 | + log.info('remove-user-git-dir: ', includeIfKey, gitConfigPath); |
| 140 | + } else { |
| 141 | + const error = new Error(`Can not remove ${gitDir}. The ${includeIfValue} is not found.`); |
| 142 | + log.error(error); |
| 143 | + throw error; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +export async function removeUserGitConfig(configName: string, gitDirs = []) { |
| 148 | + await removeSSHConfig(configName); |
| 149 | + |
| 150 | + for (const gitDir of gitDirs) { |
| 151 | + await removeUserGitDir(gitDir, configName); |
| 152 | + } |
| 153 | + |
| 154 | + // remove the gitconfig file |
| 155 | + const gitConfigPath = getGitConfigPath(configName); |
| 156 | + await fse.remove(gitConfigPath); |
| 157 | +} |
| 158 | + |
| 159 | +async function parseGitConfig(gitConfigPath: string) { |
| 160 | + const gitConfigContent = await fse.readFile(gitConfigPath, 'utf-8'); |
| 161 | + return ini.parse(gitConfigContent); |
| 162 | +} |
| 163 | + |
| 164 | +async function writeGitConfig(gitConfigPath: string, config: object) { |
| 165 | + await fse.writeFile(gitConfigPath, ini.stringify(config)); |
| 166 | + log.info('write-git-config', config); |
| 167 | +} |
| 168 | + |
| 169 | +function checkUserGitConfigExists(configName: string, gitConfigPath: string) { |
| 170 | + if (fse.pathExistsSync(gitConfigPath)) { |
| 171 | + const err = new Error(`${configName} config has existed,please use other config name.`); |
| 172 | + err.name = 'add-user-git-config'; |
| 173 | + log.error(err); |
| 174 | + throw err; |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +async function getUserGitConfigFilenames() { |
| 179 | + return await globby([`${USER_GIT_CONFIG_FILENAME_PREFIX}*`], { cwd: TOOLKIT_USER_GIT_CONFIG_DIR, dot: true }); |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * get absolute git config path in ~/.toolkit/git/ |
| 184 | + */ |
| 185 | +function getGitConfigPath(configName: string) { |
| 186 | + return path.join(TOOLKIT_USER_GIT_CONFIG_DIR, `${USER_GIT_CONFIG_FILENAME_PREFIX}${configName}`); |
| 187 | +} |
0 commit comments