Created
February 28, 2018 05:52
-
-
Save yuanliwei/f444ec159ea7b273464947f61692d53c to your computer and use it in GitHub Desktop.
使用worktree同步不同分支的代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 使用worktree同步不同分支的代码 | |
| */ | |
| // 远程分支 | |
| var fromBranch = 'origin/master' | |
| // 项目目录 | |
| var projectPath = 'D:/AndroidStudio_git/android/' | |
| // git worktree add ../wt0228 origin/master | |
| var worktree = 'D:/AndroidStudio_git/wt0228/' | |
| const { exec } = require('child_process'); | |
| const repl = require("repl") | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| var worktreeFiles = [] | |
| function myEval(cmd, context, filename, callback) { | |
| findFilePath(cmd.trim(), callback) | |
| } | |
| function buildWorkTreeFiles(dir, fileArr) { | |
| fs.readdirSync(dir).forEach((file)=> { | |
| var pathname = path.posix.join(dir, file) | |
| if (fs.statSync(pathname).isDirectory()) { | |
| buildWorkTreeFiles(pathname, fileArr) | |
| } else { | |
| fileArr.push(pathname.replace(worktree, '')) | |
| } | |
| }) | |
| } | |
| function findFilePath(command, callback) { | |
| var files = worktreeFiles.filter((file)=>{ | |
| return file.match(new RegExp(`/${command}\.[^/]*$`)) | |
| }) | |
| callback(null, files) | |
| checkoutFiles(files) | |
| } | |
| function checkoutFiles(files) { | |
| var process = exec('cmd') | |
| process.stdin.write(`cd /d ${projectPath}\n`) | |
| files.forEach((file)=> { | |
| process.stdin.write(`git checkout ${fromBranch} ${file}\n`) | |
| }) | |
| setTimeout(()=> { | |
| process.kill() | |
| }, 1000) | |
| } | |
| console.log('building files....'); | |
| buildWorkTreeFiles(worktree, worktreeFiles) | |
| console.log(worktreeFiles) | |
| console.log(`find ${worktreeFiles.length} files`); | |
| repl.start({ prompt: '> ', eval: myEval }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment