我正在开发一个多层应用程序,我为每个正在构建的服务分配了一个项目文件夹。设置示意图如下:
.
├── ProjectA
│ ├── .git
│ ├── _framework
│ ├── backend
│ ├── config
│ ├── frontend
│ └── info
├── ProjectB
│ ├── .git
│ ├── _framework
│ ├── backend
│ ├── config
│ ├── frontend
│ └── info
└── ProjectC
├── .git
├── _framework
├── backend
├── config
├── frontend
└── info
在每个项目文件夹中,我在_framework
文件夹中配置了一个子模块。
由于我在每个文件夹中积极开发,我也经常在_framework
文件夹中进行更改。我发现在所有项目中保持_framework
子模块同步需要花费大量时间。
例如:
当我在ProjectB
开发并在子模块中进行更改时,我将提交并将我的更改推送到远程存储库。然后,当我切换到ProjectC
时,我首先需要提取_framework
子模块,在我能够再次开始工作之前提交主GIT存储库中的更改。
我知道为什么GIT子模块的设置有很好的理由,但有没有办法自动化这个过程?因此,当我在ProjectB中工作时,我推送子模块 - 在其他项目中,相同的子模块会在主要的本地GIT复制中自动拉出并提交?
答案 0 :(得分:2)
使用来自@jingx的建议,我决定在ProjectA
,ProjectB
,ProjectC
的git存储库中进行预推钩。预推钩似乎是一个逻辑事件,因为当您更改并成功测试Git子模块中的代码时,无论如何都必须在主存储库中推送子模块的新状态。
作为Git钩子的参考,我使用了Atlassian的这个出色的解释:https://www.atlassian.com/git/tutorials/git-hooks
因为我比使用Bash脚本更熟悉JavaScript,所以我决定创建一个将作为预推钩执行的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)
NodeJS脚本需要一个外部模块(&#39; split&#39;)来解析调用脚本时Git钩子将添加的shell命令的标准输入。 代码仍然有点粗糙,但我认为这足以让其他人扩展。
为了使NodeJS脚本起作用,您需要在NodeJS脚本上设置文件权限以包含execute(请参阅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
答案 1 :(得分:0)
另一种选择是引用_framework
:
-> ../framework_
)这样,只有主项目必须跟踪framework_
版本。