function git_commitdiff_plain($projectroot, $project, $hash, $hash_parent)
{
    global $tpl;
    $cachekey = sha1($project) . "|" . $hash . "|" . $hash_parent;
    header("Content-type: text/plain; charset=UTF-8");
    header("Content-disposition: inline; filename=\"git-" . $hash . ".patch\"");
    if (!$tpl->is_cached('diff_plaintext.tpl', $cachekey)) {
        $ret = prep_tmpdir();
        if ($ret !== TRUE) {
            echo $ret;
            return;
        }
        $co = git_read_commit($projectroot . $project, $hash);
        if (!isset($hash_parent)) {
            $hash_parent = $co['parent'];
        }
        $diffout = git_diff_tree($projectroot . $project, $hash_parent . " " . $hash);
        $difftree = explode("\n", $diffout);
        $refs = read_info_ref($projectroot . $project, "tags");
        $listout = git_read_revlist($projectroot . $project, "HEAD");
        foreach ($listout as $i => $rev) {
            if (isset($refs[$rev])) {
                $tagname = $refs[$rev];
            }
            if ($rev == $hash) {
                break;
            }
        }
        $ad = date_str($co['author_epoch'], $co['author_tz']);
        $tpl->assign("from", $co['author']);
        $tpl->assign("date", $ad['rfc2822']);
        $tpl->assign("subject", $co['title']);
        if (isset($tagname)) {
            $tpl->assign("tagname", $tagname);
        }
        $tpl->assign("url", script_url() . "?p=" . $project . "&a=commitdiff&h=" . $hash);
        $tpl->assign("comment", $co['comment']);
        $diffs = array();
        foreach ($difftree as $i => $line) {
            if (preg_match("/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)\$/", $line, $regs)) {
                if ($regs[5] == "A") {
                    $diffs[] = git_diff($projectroot . $project, null, "/dev/null", $regs[4], "b/" . $regs[6]);
                } else {
                    if ($regs[5] == "D") {
                        $diffs[] = git_diff($projectroot . $project, $regs[3], "a/" . $regs[6], null, "/dev/null");
                    } else {
                        if ($regs[5] == "M") {
                            $diffs[] = git_diff($projectroot . $project, $regs[3], "a/" . $regs[6], $regs[4], "b/" . $regs[6]);
                        }
                    }
                }
            }
        }
        $tpl->assign("diffs", $diffs);
    }
    $tpl->display('diff_plaintext.tpl', $cachekey);
}
function git_log($projectroot, $project, $hash, $page)
{
    global $tpl;
    $cachekey = sha1($project) . "|" . $hash . "|" . (isset($page) ? $page : 0);
    $git = new Git($projectroot . $project);
    if (!$tpl->is_cached('shortlog.tpl', $cachekey)) {
        $head = git_read_head($git);
        if (!isset($hash)) {
            $hash = $head;
        } else {
            $hash = $git->revParse($hash);
        }
        if (!isset($page)) {
            $page = 0;
        }
        $refs = read_info_ref($git);
        $tpl->assign("hash", sha1_hex($hash));
        $tpl->assign("head", sha1_hex($head));
        if ($page) {
            $tpl->assign("page", $page);
        }
        $revlist = git_read_revlist($git, $hash, 101, $page * 100);
        $revlistcount = count($revlist);
        $tpl->assign("revlistcount", $revlistcount);
        if (!$revlist) {
            $tpl->assign("norevlist", TRUE);
            $co = git_read_commit($git, $hash);
            $tpl->assign("lastchange", $co['age_string']);
        }
        $commitlines = array();
        $commitcount = min(100, $revlistcount);
        for ($i = 0; $i < $commitcount; ++$i) {
            $commit = $revlist[$i];
            $commithash = sha1_hex($commit->getName());
            $commitline = array();
            $co = git_read_commit($git, $commit->getName());
            $ad = date_str($co['author_epoch']);
            $commitline["project"] = $project;
            $commitline["commit"] = $commithash;
            if (isset($refs[$commit->getName()])) {
                $commitline["commitref"] = $refs[$commit->getName()];
                $commitline["commitclass"] = get_commit_class($refs[$commit->getName()]);
            }
            $commitline["agestring"] = $co['age_string'];
            $commitline["title"] = $co['title'];
            $commitline["authorname"] = $co['author_name'];
            $commitline["rfc2822"] = $ad['rfc2822'];
            $commitline["comment"] = $co['comment'];
            $commitlines[] = $commitline;
        }
        $tpl->assign("commitlines", $commitlines);
    }
    $tpl->display('log.tpl', $cachekey);
}
Exemple #3
0
function git_rss($projectroot, $project)
{
    global $tpl;
    header("Content-type: text/xml; charset=UTF-8");
    $cachekey = sha1($project);
    if (!$tpl->is_cached('rss.tpl', $cachekey)) {
        $head = git_read_head($projectroot . $project);
        $revlist = git_read_revlist($projectroot . $project, $head, GITPHP_RSS_ITEMS);
        $tpl->assign("self", script_url());
        $commitlines = array();
        $revlistcount = count($revlist);
        for ($i = 0; $i < $revlistcount; ++$i) {
            $commit = $revlist[$i];
            $co = git_read_commit($projectroot . $project, $commit);
            if ($i >= 20 && time() - $co['committer_epoch'] > 48 * 60 * 60) {
                break;
            }
            $cd = date_str($co['committer_epoch']);
            $difftree = array();
            $parent = '';
            if (!empty($co['parent'])) {
                $parent = $co['parent'];
            }
            $diffout = git_diff_tree($projectroot . $project, $parent . " " . $co['id']);
            $tok = strtok($diffout, "\n");
            while ($tok !== false) {
                if (preg_match("/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)\$/", $tok, $regs)) {
                    $difftree[] = $regs[7];
                }
                $tok = strtok("\n");
            }
            $commitline = array();
            $commitline["cdmday"] = $cd['mday'];
            $commitline["cdmonth"] = $cd['month'];
            $commitline["cdhour"] = $cd['hour'];
            $commitline["cdminute"] = $cd['minute'];
            $commitline["title"] = $co['title'];
            $commitline["author"] = $co['author'];
            $commitline["cdrfc2822"] = $cd['rfc2822'];
            $commitline["commit"] = $commit;
            $commitline["comment"] = $co['comment'];
            $commitline["difftree"] = $difftree;
            $commitlines[] = $commitline;
        }
        $tpl->assign("commitlines", $commitlines);
    }
    $tpl->display('rss.tpl', $cachekey);
}
function git_shortlog($projectroot, $project, $hash, $page)
{
    global $tpl;
    $cachekey = sha1($project) . "|" . $hash . "|" . (isset($page) ? $page : 0);
    if (!$tpl->is_cached('shortlog.tpl', $cachekey)) {
        $head = git_read_head($projectroot . $project);
        if (!isset($hash)) {
            $hash = $head;
        }
        if (!isset($page)) {
            $page = 0;
        }
        $refs = read_info_ref($projectroot . $project);
        $tpl->assign("hash", $hash);
        $tpl->assign("head", $head);
        if ($page) {
            $tpl->assign("page", $page);
        }
        $revlist = git_read_revlist($projectroot . $project, $hash, 101, $page * 100);
        $revlistcount = count($revlist);
        $tpl->assign("revlistcount", $revlistcount);
        $commitlines = array();
        $commitcount = min(100, count($revlist));
        for ($i = 0; $i < $commitcount; ++$i) {
            $commit = $revlist[$i];
            if (strlen(trim($commit)) > 0) {
                $commitline = array();
                if (isset($refs[$commit])) {
                    $commitline["commitref"] = $refs[$commit];
                }
                $co = git_read_commit($projectroot . $project, $commit);
                $ad = date_str($co['author_epoch']);
                $commitline["commit"] = $commit;
                $commitline["agestringage"] = $co['age_string_age'];
                $commitline["agestringdate"] = $co['age_string_date'];
                $commitline["authorname"] = $co['author_name'];
                $commitline["title_short"] = $co['title_short'];
                if (strlen($co['title_short']) < strlen($co['title'])) {
                    $commitline["title"] = $co['title'];
                }
                $commitlines[] = $commitline;
            }
        }
        $tpl->assign("commitlines", $commitlines);
    }
    $tpl->display('shortlog.tpl', $cachekey);
}
function git_rss($projectroot, $project)
{
    global $tpl;
    header("Content-type: text/xml; charset=UTF-8");
    $cachekey = sha1($project);
    $git = new Git($projectroot . $project);
    if (!$tpl->is_cached('rss.tpl', $cachekey)) {
        $head = git_read_head($git);
        $revlist = git_read_revlist($git, $head, GITPHP_RSS_ITEMS);
        $tpl->assign("self", script_url());
        $commitlines = array();
        $revlistcount = count($revlist);
        for ($i = 0; $i < $revlistcount; ++$i) {
            $commit = $revlist[$i];
            $co = git_read_commit($git, $commit->getName());
            if ($i >= 20 && time() - $co['committer_epoch'] > 48 * 60 * 60) {
                break;
            }
            $cd = date_str($co['committer_epoch']);
            $difftree = array();
            $diffout = GitTree::diffTree(isset($commit->parent[0]) ? $commit->parent[0]->getTree() : null, $commit->getTree());
            foreach ($diffout as $file => $diff) {
                $difftree[] = $file;
            }
            $commitline = array();
            $commitline["cdmday"] = $cd['mday'];
            $commitline["cdmonth"] = $cd['month'];
            $commitline["cdhour"] = $cd['hour'];
            $commitline["cdminute"] = $cd['minute'];
            $commitline["title"] = $co['title'];
            $commitline["author"] = $co['author'];
            $commitline["cdrfc2822"] = $cd['rfc2822'];
            $commitline["commit"] = $commit;
            $commitline["comment"] = $co['comment'];
            $commitline["difftree"] = $difftree;
            $commitlines[] = $commitline;
        }
        $tpl->assign("commitlines", $commitlines);
    }
    $tpl->display('rss.tpl', $cachekey);
}
function git_summary($projectroot, $project)
{
    global $tpl, $gitphp_conf;
    $cachekey = sha1($project);
    if (!$tpl->is_cached('project.tpl', $cachekey)) {
        $descr = git_project_descr($projectroot, $project);
        $head = git_read_head($projectroot . $project);
        $commit = git_read_commit($projectroot . $project, $head);
        $commitdate = date_str($commit['committer_epoch'], $commit['committer_tz']);
        $owner = git_project_owner($projectroot, $project);
        $refs = read_info_ref($projectroot . $project);
        $tpl->assign("head", $head);
        $tpl->assign("description", $descr);
        $tpl->assign("owner", $owner);
        $tpl->assign("lastchange", $commitdate['rfc2822']);
        if (isset($gitphp_conf['cloneurl'])) {
            $tpl->assign('cloneurl', $gitphp_conf['cloneurl'] . $project);
        }
        if (isset($gitphp_conf['pushurl'])) {
            $tpl->assign('pushurl', $gitphp_conf['pushurl'] . $project);
        }
        $revlist = git_read_revlist($projectroot . $project, $head, 17);
        foreach ($revlist as $i => $rev) {
            $revdata = array();
            $revco = git_read_commit($projectroot . $project, $rev);
            $authordate = date_str($revco['author_epoch']);
            $revdata["commit"] = $rev;
            if (isset($refs[$rev])) {
                $revdata["commitref"] = $refs[$rev];
            }
            $revdata["commitage"] = $revco['age_string'];
            $revdata["commitauthor"] = $revco['author_name'];
            if (strlen($revco['title_short']) < strlen($revco['title'])) {
                $revdata["title"] = $revco['title'];
                $revdata["title_short"] = $revco['title_short'];
            } else {
                $revdata["title_short"] = $revco['title'];
            }
            $revlist[$i] = $revdata;
        }
        $tpl->assign("revlist", $revlist);
        $taglist = git_read_refs($projectroot, $project, "refs/tags");
        if (isset($taglist) && count($taglist) > 0) {
            foreach ($taglist as $i => $tag) {
                if (isset($tag['comment'])) {
                    $com = trim($tag['comment'][0]);
                    if (strlen($com) > GITPHP_TRIM_LENGTH) {
                        $com = substr($trimmed, 0, GITPHP_TRIM_LENGTH) . "...";
                    }
                    $taglist[$i]['comment'] = $com;
                }
            }
            $tpl->assign("taglist", $taglist);
        }
        $headlist = git_read_refs($projectroot, $project, "refs/heads");
        if (isset($headlist) && count($headlist) > 0) {
            $tpl->assign("headlist", $headlist);
        }
    }
    $tpl->display('project.tpl', $cachekey);
}
function git_search($projectroot, $project, $hash, $search, $searchtype, $page = 0)
{
    global $tpl, $gitphp_conf;
    $cachekey = sha1($project) . "|" . $hash . "|" . sha1($searchtype) . "|" . sha1($search) . "|" . (isset($page) ? $page : 0);
    if (!$tpl->is_cached('search.tpl', $cachekey)) {
        if (!$gitphp_conf['search']) {
            git_message("Search has been disabled", TRUE, TRUE);
            return;
        }
        if (!isset($search) || strlen($search) < 2) {
            git_message("You must enter search text of at least 2 characters", TRUE, TRUE);
            return;
        }
        if (!isset($hash)) {
            //$hash = git_read_head($projectroot . $project);
            $hash = "HEAD";
        }
        $co = git_read_commit($projectroot . $project, $hash);
        $revlist = git_read_revlist($projectroot . $project, $hash, 101, $page * 100, FALSE, FALSE, $searchtype, $search);
        if (count($revlist) < 1 || strlen($revlist[0]) < 1) {
            git_message("No matches for '" . $search . "'.", FALSE, TRUE);
            return;
        }
        $tpl->assign("hash", $hash);
        $tpl->assign("treehash", $co['tree']);
        $tpl->assign("search", $search);
        $tpl->assign("searchtype", $searchtype);
        $tpl->assign("page", $page);
        $revlistcount = count($revlist);
        $tpl->assign("revlistcount", $revlistcount);
        $tpl->assign("title", $co['title']);
        $commitlines = array();
        $commitcount = min(100, $revlistcount);
        for ($i = 0; $i < $commitcount; ++$i) {
            $commit = $revlist[$i];
            if (strlen(trim($commit)) > 0) {
                $commitline = array();
                $co2 = git_read_commit($projectroot . $project, $commit);
                $commitline["commit"] = $commit;
                $commitline["agestringage"] = $co2['age_string_age'];
                $commitline["agestringdate"] = $co2['age_string_date'];
                $commitline["authorname"] = $co2['author_name'];
                $commitline["title_short"] = $co2['title_short'];
                if (strlen($co2['title_short']) < strlen($co2['title'])) {
                    $commitline["title"] = $co2['title'];
                }
                $commitline["committree"] = $co2['tree'];
                $matches = array();
                foreach ($co2['comment'] as $comline) {
                    $hl = highlight($comline, $search, "searchmatch", GITPHP_TRIM_LENGTH);
                    if ($hl && strlen($hl) > 0) {
                        $matches[] = $hl;
                    }
                }
                $commitline["matches"] = $matches;
                $commitlines[] = $commitline;
            }
        }
        $tpl->assign("commitlines", $commitlines);
    }
    $tpl->display('search.tpl', $cachekey);
}
function git_read_commit($proj, $head)
{
    //Purify html output
    $purifier = Codendi_HTMLPurifier::instance();
    $lines = git_read_revlist($proj, $head, 1, NULL, TRUE, TRUE);
    if (!$lines[0] || !preg_match("/^[0-9a-fA-F]{40}/", $lines[0])) {
        return null;
    }
    $commit = array();
    $tok = strtok($lines[0], " ");
    $commit['id'] = $tok;
    $tok = strtok(" ");
    $parents = array();
    while ($tok !== false) {
        $parents[] = $tok;
        $tok = strtok(" ");
    }
    $commit['parents'] = $parents;
    if (isset($parents[0])) {
        $commit['parent'] = $parents[0];
    }
    $comment = array();
    foreach ($lines as $i => $line) {
        if (preg_match("/^tree ([0-9a-fA-F]{40})\$/", $line, $regs)) {
            $commit['tree'] = $regs[1];
        } else {
            if (preg_match("/^author (.*) ([0-9]+) (.*)\$/", $line, $regs)) {
                $commit['author'] = $purifier->purify($regs[1], CODENDI_PURIFIER_BASIC_NOBR, $_REQUEST['group_id']);
                $commit['author_epoch'] = $regs[2];
                $commit['author_tz'] = $regs[3];
                if (preg_match("/^([^<]+) </", $commit['author'], $r)) {
                    $commit['author_name'] = $r[1];
                } else {
                    $commit['author_name'] = $commit['author'];
                }
            } else {
                if (preg_match("/^committer (.*) ([0-9]+) (.*)\$/", $line, $regs)) {
                    $commit['committer'] = $purifier->purify($regs[1], CODENDI_PURIFIER_BASIC_NOBR, $_REQUEST['group_id']);
                    $commit['committer_epoch'] = $regs[2];
                    $commit['committer_tz'] = $regs[3];
                    $commit['committer_name'] = $commit['committer'];
                    $commit['committer_name'] = preg_replace("/ <.*/", "", $commit['committer_name']);
                } else {
                    $trimmed = trim($line);
                    if (strlen($trimmed) > 0 && !preg_match("/^[0-9a-fA-F]{40}/", $trimmed) && !preg_match("/^parent [0-9a-fA-F]{40}/", $trimmed)) {
                        if (!isset($commit['title'])) {
                            $commit['title'] = $trimmed;
                            if (strlen($trimmed) > GITPHP_TRIM_LENGTH) {
                                $commit['title_short'] = substr($trimmed, 0, GITPHP_TRIM_LENGTH) . "...";
                            } else {
                                $commit['title_short'] = $trimmed;
                            }
                        }
                        $trimmed = $purifier->purify($trimmed, CODENDI_PURIFIER_BASIC_NOBR, $_REQUEST['group_id']);
                        $comment[] = $trimmed;
                    }
                }
            }
        }
    }
    $commit['comment'] = $comment;
    $age = time() - $commit['committer_epoch'];
    $commit['age'] = $age;
    $commit['age_string'] = age_string($age);
    date_default_timezone_set("UTC");
    if ($age > 60 * 60 * 24 * 7 * 2) {
        $commit['age_string_date'] = date("Y-m-d", $commit['committer_epoch']);
        $commit['age_string_age'] = $commit['age_string'];
    } else {
        $commit['age_string_date'] = $commit['age_string'];
        $commit['age_string_age'] = date("Y-m-d", $commit['committer_epoch']);
    }
    return $commit;
}