Пример #1
0
/**
 * Registers the commit info for all new commits on the given branch
 *
 * @param PHPGit_Repository $repo repository to parse
 * @param string $gitbranch the real name of the branch to analyze (eg 'master')
 * @param string $branch the future name of the same branch (eg 'MOODLE_28_STABLE')
 * @param string $mergemode either 'merges' or 'no-merges'
 * @param bool $showprogress
 * @internal
 */
function dev_git_record_commits(PHPGit_Repository $repo, $gitbranch, $branch, $mergemode, $showprogress = false)
{
    global $DB;
    $startpoints = get_config('local_dev', 'gitstartpoints');
    if ($startpoints === false) {
        set_config('gitstartpoints', json_encode(array()), 'local_dev');
        $startpoints = get_config('local_dev', 'gitstartpoints');
    }
    $startpoints = json_decode($startpoints, true);
    $reponame = basename($repo->getDir());
    $exclude = empty($startpoints[$branch][$mergemode]) ? '' : $startpoints[$branch][$mergemode];
    if ($mergemode === 'merges') {
        fputs(STDOUT, "Searching merges on {$gitbranch} ({$branch})" . ($exclude ? " from {$exclude}" : "") . PHP_EOL);
        $mergeflag = 1;
    } else {
        if ($mergemode === 'no-merges') {
            fputs(STDOUT, "Searching non-merges on {$gitbranch} ({$branch})" . ($exclude ? " from {$exclude}" : "") . PHP_EOL);
            $mergeflag = 0;
        }
    }
    $exclude = empty($exclude) ? '' : '^' . $exclude;
    $commits = explode(PHP_EOL, $repo->git("rev-list --reverse --{$mergemode} --format='tformat:COMMIT:%H TIMESTAMP:%at AUTHORNAME:%an AUTHOREMAIL:%ae SUBJECT:%s' {$gitbranch} {$exclude}"));
    $total = floor(count($commits) / 2);
    $counter = 0;
    if ($showprogress and $total == 0) {
        fputs(STDOUT, 'no commits found');
    }
    foreach ($commits as $commit) {
        $pattern = '/^COMMIT:([0-9a-f]{40}) TIMESTAMP:([0-9]+) AUTHORNAME:(.+) AUTHOREMAIL:(.+) SUBJECT:(.*)$/';
        if (!preg_match($pattern, $commit, $matches)) {
            continue;
        }
        $record = new stdClass();
        $record->repository = $reponame;
        $record->commithash = $matches[1];
        $record->authordate = $matches[2];
        $record->authorname = $matches[3];
        $record->authoremail = $matches[4];
        $record->subject = $matches[5];
        $record->merge = $mergeflag;
        $record = @fix_utf8($record);
        // register the commit info record if it does not exist yet
        $existing = $DB->get_record('dev_git_commits', array('repository' => $reponame, 'commithash' => $record->commithash), 'id', IGNORE_MISSING);
        if ($existing === false) {
            $commitid = $DB->insert_record('dev_git_commits', $record, true, true);
        } else {
            $commitid = $existing->id;
        }
        // register the branch containing the current commit
        if (!$DB->record_exists('dev_git_commit_branches', array('branch' => $branch, 'commitid' => $commitid))) {
            $branchinfo = new stdClass();
            $branchinfo->commitid = $commitid;
            $branchinfo->branch = $branch;
            $DB->insert_record('dev_git_commit_branches', $branchinfo, false, true);
        }
        if ($showprogress) {
            fputs(STDOUT, ++$counter . '/' . $total . "\r");
        }
        $startpoints[$branch][$mergemode] = $record->commithash;
        if ($counter % 1000 == 0) {
            set_config('gitstartpoints', json_encode($startpoints), 'local_dev');
        }
    }
    set_config('gitstartpoints', json_encode($startpoints), 'local_dev');
    if ($showprogress) {
        fputs(STDOUT, PHP_EOL);
    }
}
Пример #2
0
 /**
  * Returns the git repository directory
  *
  * @return string
  */
 public function getDir()
 {
     return $this->gitRepo->getDir();
 }