Git 分支合并状态插件

Git 分支合并状态插件

运行方式

1、安装 vscode 拓展 Power Tools
2、ctrl+alt+p 执行命令 Power Tools:Scripts,创建一个脚本
3、执行这段代码

const vscode_helpers = $require("vscode-helpers");
const MAX_USER_COUNT = 20;

var exec = require("child_process").exec;

$clear();

await $withProgress(async (progress, cancelToken) => {
  //   if (false !== CLIENT) {
  // const STD_OUT = (await CLIENT.exec(`branch`,[`--git-dir`])).stdOut;

  // exec(`git --git-dir="C:/Users/Administrator/Documents/worbench.v3/.git" branch`,function (error, stdout, stderr) {
  //     // console.log('stdout: ' + stdout);
  //     $writeMarkdown(`## ${ stdout}`);
  //   })

  // const STD_OUT = (await CLIENT.exec([`--git-dir=C:/Users/Administrator/Documents/worbench.v3/.git/`,`branch`])).stdOut;
  let allBranch = await commandWorker();
  let master = await commandWorker(["--merged", "master"]);
  let pre_release = await commandWorker(["--merged", "pre_release"]);
  let produce = await commandWorker(["--merged", "produce"]);

  let obj = {};
  setOjb(obj, "pre_release", allBranch, pre_release);
  setOjb(obj, "produce", allBranch, produce);
  setOjb(obj, "master", allBranch, master);

  allBranch.forEach(element => {
    // $write(JSON.stringify(obj[element]))
    let inBranch = ""
    allBranch.forEach(v=>{
        if(obj[element][v]==true && v!=element){
            inBranch=inBranch+" "+v+" "
        }
    })
    let status=""
    if(inBranch){
      status = `(${inBranch})`
    }
    $writeMarkdown(`## ${element} ${status}`)
  });
//   $write(JSON.stringify(obj));
});
function setOjb(obj, branch, allBranch, arr) {
  let inPre = [];
  arr.forEach(element => {
    let v = allBranch.find(item => {
      return item === element;
    });
    // if (v != branch) {
      inPre.push(v);
    // }
  });
  inPre.forEach(e => {
    if (!obj[e]) {
      obj[e] = {};
    }
    obj[e][branch] = true;
  });
}
async function commandWorker(args) {
  const CLIENT = await vscode_helpers.tryCreateGitClient();
  if (false !== CLIENT) {
    var preArgs = [
      `--git-dir=C:/Users/Administrator/Documents/worbench.v3/.git/`,
      `branch`
    ];
    // let c = preArgs.concat(args);
    let c = preArgs;
    if (args) {
      c = preArgs.concat(args);
    }
    // let c = preArgs.concat(args)
    // $write(c);
    let RES = (await CLIENT.exec(c)).stdOut;
    let allBranchRaw = RES.toString();
    //   let res = allBranchRaw
    let filterStar = allBranchRaw.match(/\S+/g);
    //   let arr = res.match(/\b/)
    // "--merged",
    //   "pre_release"

    //   $write(`${allBranch.toString()}`);
    let branchList = filterStar.filter(s => {
      if (s == "*") {
        return false;
      } else {
        return true;
      }
    });
    return branchList;
  } else {
    // no git client found
  }
}

运行效果,可以快速查看分支的合并状态