function git_read_refs($projectroot, $project, $refdir)
{
    if (!is_dir($projectroot . $project . "/" . $refdir)) {
        return null;
    }
    $refs = array();
    if ($dh = opendir($projectroot . $project . "/" . $refdir)) {
        while (($dir = readdir($dh)) !== false) {
            if (strpos($dir, '.') !== 0) {
                if (is_dir($projectroot . $project . "/" . $refdir . "/" . $dir)) {
                    if ($dh2 = opendir($projectroot . $project . "/" . $refdir . "/" . $dir)) {
                        while (($dir2 = readdir($dh2)) !== false) {
                            if (strpos($dir2, '.') !== 0) {
                                $refs[] = $dir . "/" . $dir2;
                            }
                        }
                        closedir($dh2);
                    }
                }
                $refs[] = $dir;
            }
        }
        closedir($dh);
    } else {
        return null;
    }
    $reflist = array();
    foreach ($refs as $i => $ref_file) {
        $ref_id = git_read_hash($projectroot . $project . "/" . $refdir . "/" . $ref_file);
        $refobj = git_read_ref($projectroot, $project, $ref_id, $ref_file);
        if (isset($refobj)) {
            $reflist[] = $refobj;
        }
    }
    $packedrefs = git_read_packed_refs($projectroot, $project, $refdir);
    if (isset($packedrefs) && count($packedrefs) > 0) {
        foreach ($packedrefs as $packedref) {
            $found = false;
            foreach ($reflist as $ref) {
                if (strcmp($ref["name"], $packedref["name"]) === 0) {
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                $reflist[] = $packedref;
            }
        }
    }
    usort($reflist, "epochcmp");
    return $reflist;
}
function git_read_packed_refs($project, $refdir)
{
    if (!is_file($project->dir . '/packed-refs')) {
        return null;
    }
    $refs = array();
    $refs = explode("\n", git_read_hash($project->dir . "/" . 'packed-refs'));
    $reflist = array();
    $dirlen = strlen($refdir);
    foreach ($refs as $i) {
        if (preg_match('/^([0-9a-f]{40}) (.+)$/i', trim($i), $regs)) {
            if (strncmp($refdir, $regs[2], $dirlen) === 0) {
                $regs[2] = substr($regs[2], $dirlen + 1);
                $refobj = git_read_ref($project, $regs[1], $regs[2]);
                if (isset($refobj)) {
                    $reflist[] = $refobj;
                }
            }
        }
    }
    return $reflist;
}