InvalidArgumentException("the branch string is not valid") thrown if local repo has a detached HEAD
MLukman opened this issue · 6 comments
An InvalidArgumentException is thrown by \GitElephant\Objects\Branch::getMatches
if a local repo has a detached HEAD. The following output line from git branch -a -v
command is the culprit.
* (HEAD detached at 7a02066) 7a020660489a31ed9d0d6a42d5a0f0379334ba82 Merge branch 'PERFORM' into 'master'
But, do we consider a detached head as a branch, maybe called 'HEAD'? Or should we filter this line out first before parsing?
That line is from Windows version of git binary.
Running Linux version (Ubuntu) of git binary outputs similar line and receives the same exception:
* (detached from 7a02066) 7a020660489a31ed9d0d6a42d5a0f0379334ba82 Merge branch 'PERFORM' into 'master'
good catch....I think that we should handle this in an elegnat way...
This is the modification I did to allow my app to handle detached head:
preg_match('/^\*?\ *?(\S+)\ +(\S{40})\ +(.+)$/', trim($branchString), $matches);
if (!count($matches)) {
preg_match('/^\*?\ *?\(.*(detached).*\)\ +(\S{40})\ +(.+)$/', trim($branchString), $matches);
if (!count($matches)) {
throw new \InvalidArgumentException(sprintf('the branch string is not valid: %s', $branchString));
}
}
This code considers a detached head as another branched "detached". It works for me, for now.
Do you mind opening a PR, @MLukman , respectively if you are still using this library?
@BernhardWebstudio I already implemented suggested fix, with refactoring : https://github.com/matteosister/GitElephant/blob/develop/src/GitElephant/Objects/Branch.php#L207
Oh, sorry @imunhatep, thank you very much, I missed the context there. Excellent! Can we close this issue?