ruyadorno / git-bc

Git plugin to interactively list branches and checkout
MIT License
60 stars 6 forks source link

Utility does not create a branch if I select a remote branch #7

Closed mehtaad closed 8 years ago

mehtaad commented 8 years ago

Hi, I really liked your utility. I am new to node. when I use 'git checkout ' and if branch is not tracked locally but available on remote it use to create a local branch and checkout. But git-bc does not create a local branch if it is not tracked. So I added some enhancement to it to create a local branch and switch to it if it is not tracked. Here is the code:

Command to run: git bc -r
function promptList(branches) {
    //path that needs to be pulled out of branch name
    var pathSplicer =['remote/origin/','master/','xyz/'];
    result.prompt = inquirer.prompt([{
      type: 'list',
      name: 'branches',
      message: 'Select a branch to checkout:',
      choices: branches.filter(
        function filterOutDetachedMsg(branchName) {
          return !/\(detached\ from\ .*\)/.test(branchName);
        }
      ).map(
        function formatBranchName(branchName) {
          var branch = branchName.replace('*', '').trim();
          branch = branch.split(' ')[0];
          var r = branch;
          for(var i=0; i<pathSplicer.length, i++) {
            r = r.split(pathSplicer[i]);
            r = r[r.length - 1];
          }
          branch = r;
          return {
            name: branchName,
            value: branch
          };
        }
      )
    }], onBranchChosen);
  }

This code is now able to switch to remote branch.

But I have no clue how this git-bc works. How it know to invoke itself when user type something on command prompt? For example: git bc -r, its a git command so git should be in control, how do git-bc gets control? Could you please share the knowledge? Let me know were I should check to understand how the control is passed to git-bc?

Thanks, Ashok

ruyadorno commented 8 years ago

discussed on #8