public function getHead($default = null)
    {
        $file = '';
        if (file_exists($this->getPath() . '/.git/HEAD')) {
            $file = file_get_contents($this->getPath() . '/.git/HEAD');
        } elseif (file_exists($this->getPath() . '/HEAD')) {
            $file = file_get_contents($this->getPath() . '/HEAD');
        }

        // Find first existing branch
        foreach (explode("\n", $file) as $line) {
            $m = array();
            if (preg_match('#ref:\srefs/heads/(.+)#', $line, $m)) {
                if ($this->hasBranch($m[1])) {
                    return $m[1];
                }
            }
        }

        // If we were given a default branch and it exists, return that.
        if (null !== $default && $this->hasBranch($default)) {
            return $default;
        }

        // Otherwise, return the first existing branch.
        $branches = $this->getBranches();
        if (!empty($branches)) {
            return current($branches);
        }

        // No branches exist - null is the best we can do in this case.
        return null;
    }
public function getHead($default = null)
    {
        $file = '';
        if (file_exists($this->getPath() . '/.git/HEAD')) {
            $file = file_get_contents($this->getPath() . '/.git/HEAD');
        } elseif (file_exists($this->getPath() . '/HEAD')) {
            $file = file_get_contents($this->getPath() . '/HEAD');
        }

        // Find first existing branch
        foreach (explode("\n", $file) as $line) {
            $m = array();
            if (preg_match('#ref:\srefs/heads/(.+)#', $line, $m)) {
                if ($this->hasBranch($m[1])) {
                    return $m[1];
                }
            }
        }

        // If we were given a default branch and it exists, return that.
        if (null !== $default && $this->hasBranch($default)) {
            return $default;
        }

        // Otherwise, return the first existing branch.
        $branches = $this->getBranches();
        if (!empty($branches)) {
            return current($branches);
        }

        // No branches exist - null is the best we can do in this case.
        return null;
    }
public function getHead ( $ default = null ) { $ file = '' ; if ( file_exists ( $ this -> getPath ( ) . '/.git/HEAD' ) ) { $ file = file_get_contents ( $ this -> getPath ( ) . '/.git/HEAD' ) ; } elseif ( file_exists ( $ this -> getPath ( ) . '/HEAD' ) ) { $ file = file_get_contents ( $ this -> getPath ( ) . '/HEAD' ) ; } // Find first existing branch foreach ( explode ( "\n" , $ file ) as $ line ) { $ m = array ( ) ; if ( preg_match ( '#ref:\srefs/heads/(.+)#' , $ line , $ m ) ) { if ( $ this -> hasBranch ( $ m [ 1 ] ) ) { return $ m [ 1 ] ; } } } // If we were given a default branch and it exists, return that. if ( null !== $ default && $ this -> hasBranch ( $ default ) ) { return $ default ; } // Otherwise, return the first existing branch. $ branches = $ this -> getBranches ( ) ; if ( ! empty ( $ branches ) ) { return current ( $ branches ) ; } // No branches exist - null is the best we can do in this case. return null ; }
Get the current HEAD.

@param $default Optional branch to default to if in detached HEAD state.
If not passed, just grabs the first branch listed.

@return string the name of the HEAD branch, or a backup option if
in detached HEAD state
Get the current HEAD .