GitElephant icon indicating copy to clipboard operation
GitElephant copied to clipboard

Calling Repository::getBranches() will execute "git branch" command the same number of times as the number of branches + 1

Open MLukman opened this issue 8 years ago • 0 comments

I added the following to GitElephant\Command\Caller\Caller::execute method to see how GitElephant calls the underlying Git binary:

    print $cmd.'<br />';

And found out that whenever GitElephant\Repository::getBranches is called, the command git branch will be executed the same number of times as the number of local branches plus one. So if I have four branches, the command will be executed five times. Not so optimized.

Diving into the code, I found out that the following method calls happen when calling getBranches:

  1. BranchCommand::listBranches is executed. This is the first git branch command execution.
  2. For each output line, Branch::createFromOutputLine is called.
  3. In Branch::createFromOutputLine, constructor of Branch class is called.
  4. The constructor calls Branch::createFromCommand method.
  5. Inside Branch::createFromCommand method, BranchCommand::listBranches is executed. This will be repeated for each output line processed in step 2 above.
  6. Branch::createFromCommand method will call Branch::parseOutputLine. Returning to the constructor and then returning to Branch::createFromOutputLine method, Branch::parseOutputLine is called again using output line passed from Repository::getBranches method.

In step 6, the parseOutputLine method is called twice, which can be reduced to one if we can make the Branch::createFromOutputLine method call the Branch class constructor without calling Branch::createFromCommand because the output line needed to be parsed is already been provided as a parameter of Branch::createFromOutputLine.

Using version 1.1.0 via composer.

Please comment.

MLukman avatar May 24 '16 14:05 MLukman