Gup*_*e70 5 git git-submodules
我正在开发一个多层应用程序,我为正在构建的每个服务分配了一个项目文件夹。该设置示意性如下所示:
\n\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ProjectA\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .git\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _framework\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 backend\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 frontend\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 info\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ProjectB\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .git\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _framework\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 backend\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 frontend\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 info\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ProjectC\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .git\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _framework\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 backend\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 frontend\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 info\nRun Code Online (Sandbox Code Playgroud)\n\n在每个项目文件夹中,我都在该文件夹中配置了一个子模块_framework。\n由于我在每个文件夹中积极开发,所以我也在该_framework文件夹中进行频繁的更改。我发现保持_framework在所有项目中保持子模块同步需要花费大量时间。
例如:\n当我在ProjectB子模块中进行开发和更改时,我将提交更改并将其推送到远程存储库。然后当我切换到 时ProjectC,我首先需要拉动_framework子模块,在主 GIT 存储库中提交更改,然后才能再次开始工作。
我知道 GIT 子模块如此设置是有充分理由的,但是有什么方法可以自动化该过程吗?这样,当我在 ProjectB 中工作并推送子模块时 - 在其他项目中,相同的子模块会自动拉取并提交到主要的本地 GIT 重现中?
\nProjectA根据 @jingx 的建议,我决定在, ProjectB,的 git 存储库中创建一个预推送钩子ProjectC。预推送挂钩似乎是一个合乎逻辑的事件,因为当您更改并成功测试了 Git 子模块中的代码时,您必须将子模块的新状态推送到主存储库中。
作为 Git hooks 的参考,我使用了 Atlassian 的精彩解释:https ://www.atlassian.com/git/tutorials/git-hooks
因为我对 JavaScript 比 Bash 脚本更熟悉,所以我决定创建一个将作为 pre-push hook 执行的 NodeJS 脚本:
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const util = require('util');
// Logging something to the console so that we can verify if the hook is triggered or not
console.log('Invoking pre-push hook v1.0');
// Global variables
const exec = util.promisify(require('child_process').exec);
const gitRepositoryPathOs = process.argv[1].replace(/(.*)\/\.git\/.*/, '$1');
const gitRepositoryParentFolder = path.dirname(gitRepositoryPathOs);
const gitRepositoryFolderName = path.basename(gitRepositoryPathOs);
const scriptFilePathOs = __filename;
const scriptLogFolderPathOs = `${path.dirname(scriptFilePathOs)}/logs`;
const pushSiblingProjects = false;
const debugRoutine = true;
let debugInfo = "";
let stdIn = [];
// Defines all the project folder names where this hook should be working with
const projectFolderNames = ['ProjectA', 'ProjectB', 'ProjectC'];
// Defines the submodules that this routine should be checking for in each project folder
const submodulePaths = ['_framework'];
/**
* Executes a shell command
*
* @param {string} cmd Shell command to execute
* @param {string} [workingDirectory=gitRepositoryParentFolder] Directory to execute the shell command in
* @returns The result of the shell command
*/
const executeShellCommand = async (cmd, workingDirectory = gitRepositoryPathOs) => {
// if (debugRoutine) debugInfo += `- executeShellCommand('${cmd}', '${workingDirectory}')\n`;
const {stdout, stderr} = await exec(cmd, {
cwd: workingDirectory
});
return stdout;
}
/**
* Starts the logic of this GIT hook routine
*
* @returns Exit code to indicate to the GIT process if it should continue or not
*/
const initGitHookRoutine = async () => {
// Global variables
let shellCommand = '';
let shellResult = '';
let siblingGitRepositoryUpdateExecuted = false;
// Catch all parameters passed to the process.argv
debugInfo += "Passed arguments:\n\n";
process.argv.forEach(function (val, index, array) {
debugInfo += `${index}: ${val}\n`;
});
debugInfo += `Standard In:\n${stdIn.join()}`;
debugInfo += "\n\n";
debugInfo += `- gitRepositoryPathOs: '${gitRepositoryPathOs}'\n`;
debugInfo += `- gitRepositoryParentFolder: '${gitRepositoryParentFolder}'\n`;
debugInfo += `- gitRepositoryFolderName: '${gitRepositoryFolderName}'\n`;
debugInfo += `- scriptFilePathOs: '${scriptFilePathOs}'\n`;
try {
// Retrieve a list of objects that we are about to push to the remote repository
shellCommand = `git diff --stat --cached origin/master`;
shellResult = await executeShellCommand(shellCommand);
} catch (err) {
shellResult = `ERROR: could not execute '${shellCommand}', error details: ${JSON.stringify(err)}`;
}
debugInfo += `- shellCommand: '${shellCommand}', shellResult: '${shellResult}'\n`;
// Mark which submodules we need to process
let submodulePathsToProcess = [];
submodulePaths.forEach((submodulePath) => {
if (shellResult.indexOf(submodulePath) > -1) {
submodulePathsToProcess.push(submodulePath);
}
})
debugInfo += `- submodulePathsToProcess: ${submodulePathsToProcess.join()}\n`;
if (submodulePathsToProcess.length > 0) {
let submodulePath = '';
// Now loop through the other projects and update the submodules there
// Using "old fashioned loop style" here as it seems to work better with async function calls...
for (let i = 0; i < projectFolderNames.length; i++) {
const projectFolderName = projectFolderNames[i];
if (projectFolderName !== gitRepositoryFolderName) {
const siblingGitRepositoryPathOs = `${gitRepositoryParentFolder}/${projectFolderName}`;
debugInfo += `- processing GIT repository '${siblingGitRepositoryPathOs}'\n`;
// Loop through the submodules that we need to update
for (let j = 0; j < submodulePathsToProcess.length; j++) {
submodulePath = submodulePathsToProcess[j];
const siblingGitRepositorySubmodulePathOs = `${siblingGitRepositoryPathOs}/${submodulePath}`;
debugInfo += `- processing GIT submodule '${siblingGitRepositorySubmodulePathOs}'\n`;
try {
// Pull the latest version of the submodule from the remote repository
shellCommand = `git pull origin master`;
shellResult = await executeShellCommand(shellCommand, siblingGitRepositorySubmodulePathOs);
} catch (err) {
shellResult = `ERROR: could not execute '${shellCommand}', error details: ${JSON.stringify(err)}`;
}
debugInfo += `- shellCommand: '${shellCommand}', shellResult: '${shellResult}'\n`;
}
// Use git status to check which submodules need to be committed
try {
shellCommand = `git status`;
shellResult = await executeShellCommand(shellCommand, siblingGitRepositoryPathOs);
} catch (err) {
shellResult = `ERROR: could not execute '${shellCommand}', error details: ${JSON.stringify(err)}`;
}
debugInfo += `- shellCommand: '${shellCommand}', shellResult: '${shellResult}'\n`;
// Now check for each submodule if it needs to be committed to the sibling project repository
for (let j = 0; j < submodulePathsToProcess.length; j++) {
submodulePath = submodulePathsToProcess[j];
if (shellResult.indexOf(`${submodulePath} (new commits)`) > -1) {
// 1) Add the submodule reference to the local git staging area
try {
shellCommand = `git add ${submodulePath}`;
shellResult = await executeShellCommand(shellCommand, siblingGitRepositoryPathOs);
} catch (err) {
shellResult = `ERROR: could not execute '${shellCommand}', error details: ${JSON.stringify(err)}`;
}
debugInfo += `- shellCommand: '${shellCommand}', shellResult: '${shellResult}'\n`;
// 2) Commit the submodule reference to the local git repository
if (shellResult.indexOf('ERROR') === -1) {
siblingGitRepositoryUpdateExecuted = true;
try {
shellCommand = `git commit -m "Submodule ${path.basename(submodulePath)} updated by GIT hook utility"`;
shellResult = await executeShellCommand(shellCommand, siblingGitRepositoryPathOs);
} catch (err) {
shellResult = `ERROR: could not execute '${shellCommand}', error details: ${JSON.stringify(err)}`;
}
debugInfo += `- shellCommand: '${shellCommand}', shellResult: '${shellResult}'\n`;
}
// 3) Optionally push this to the remote repository (not recommended)
if (pushSiblingProjects) {
if (shellResult.indexOf('ERROR') === -1) {
try {
shellCommand = `git push origin master`;
shellResult = await executeShellCommand(shellCommand, siblingGitRepositoryPathOs);
} catch (err) {
shellResult = `ERROR: could not execute '${shellCommand}', error details: ${JSON.stringify(err)}`;
}
debugInfo += `- shellCommand: '${shellCommand}', shellResult: '${shellResult}'\n`;
}
}
}
}
}
}
// Check if we need to execute anything after we have modified the sibling repositories
if (siblingGitRepositoryUpdateExecuted) {
// Put your logic in here
}
}
// Dump the debug information to the console
if (debugRoutine) console.log(`* debugInfo: ${debugInfo}`);
// Dump the debug information in a log file so that we can inspect it later on
try {
fs.writeFileSync(`${scriptLogFolderPathOs}/pre-push.log`, debugInfo);
} catch (err) {
console.log(`ERROR: write the log file in folder '${scriptLogFolderPathOs}', error details: ${JSON.stringify(err)}`);
}
// To avoid push from taking place exit a code > 0
return process.exit(0);
};
/**
* This is where the execution starts
* First we capture the content from the shell standard in and then we start the logic itself
*/
// NOTE: use 'npm install split --save-dev' to install the split module
process.stdin.pipe(require('split')()).on('data', (line) => {
// Push the line into the stdIn array so we can use it later on
if (line.trim() !== '') stdIn.push(line);
}).on('end', initGitHookRoutine)
Run Code Online (Sandbox Code Playgroud)
NodeJS 脚本需要一个外部模块(“split”)来解析来自 shell 命令的标准输入,Git 挂钩在调用脚本时将添加该命令。代码仍然有点粗糙,但我认为这足以供其他人扩展。
为了使 NodeJS 脚本正常工作,您需要将 NodeJS 脚本的文件权限设置为包含执行(请参阅 Atlassian 页面)。
在我的 Mac 上,脚本拒绝运行,因为 hashbang#!/usr/bin/env node拒绝执行。我设法通过使用以下命令创建指向节点可执行文件的符号链接来解决此问题sudo ln -s /usr/bin/node /usr/local/bin/node
在我的机器上,我首先必须禁用系统完整性保护,然后才能实际创建符号链接。更多信息请参见: https: //www.imore.com/el-capitan-system-integrity-protection-helps-keep-malware-away
| 归档时间: |
|
| 查看次数: |
706 次 |
| 最近记录: |