git::exec PHP Method

exec() static public method

Execute a git command
static public exec ( $cmd, $input = null, $gitdir = null, $worktree = null, $allow_guess = false, $ignore_git_errors = false )
    static function exec($cmd, $input = null, $gitdir = null, $worktree = null, $allow_guess = false, $ignore_git_errors = false)
    {
        # build cmd
        if ($gitdir === null && !$allow_guess) {
            $gitdir = gb::$site_dir . '/.git';
        }
        if ($worktree === null && !$allow_guess) {
            $worktree = gb::$site_dir;
        }
        $cmd_prefix = 'git';
        if ($gitdir) {
            $cmd_prefix .= ' --git-dir=' . escapeshellarg($gitdir);
        }
        if ($worktree) {
            $cmd_prefix .= ' --work-tree=' . escapeshellarg($worktree);
        }
        $cmd = $cmd_prefix . ' ' . $cmd;
        #var_dump($cmd);
        gb::log(LOG_DEBUG, 'exec$ ' . $cmd);
        $r = gb::shell($cmd, $input, gb::$site_dir);
        git::$query_count++;
        # fail?
        if ($r === null) {
            return null;
        }
        # check for errors
        if ($r[0] != 0) {
            $msg = trim($r[1] . "\n" . $r[2]);
            if ($ignore_git_errors && strpos($msg, 'sh: ') !== 0) {
                return $msg;
            }
            if (strpos($r[2], 'Not a git repository') !== false) {
                throw new GitUninitializedRepoError($msg, $r[0], $cmd);
            } else {
                throw new GitError($msg, $r[0], $cmd);
            }
        }
        return $r[1];
    }

Usage Example

Example #1
0
 /**
  * Rebuild caches, indexes, etc.
  */
 static function rebuild($forceFullRebuild = false)
 {
     gb::log(LOG_NOTICE, 'rebuilding cache' . ($forceFullRebuild ? ' (forcing full rebuild)' : ''));
     $time_started = microtime(1);
     $failures = array();
     # Load rebuild plugins
     gb::load_plugins('rebuild');
     # Load rebuilders if needed
     if (empty(self::$rebuilders)) {
         self::loadRebuilders();
     }
     # Create rebuilder instances
     $rebuilders = array();
     foreach (self::$rebuilders as $cls) {
         $rebuilders[] = new $cls($forceFullRebuild);
     }
     # Load rebuild plugins (2nd offer)
     gb::load_plugins('rebuild');
     # Query ls-tree
     $ls = rtrim(git::exec('ls-files --stage'));
     if ($ls) {
         # Iterate objects
         $ls = explode("\n", $ls);
         foreach ($ls as $line) {
             try {
                 # <mode> SP <object> SP <stage no> TAB <name>
                 if (!$line) {
                     continue;
                 }
                 $line = explode(' ', $line, 3);
                 $id = $line[1];
                 $name = gb_normalize_git_name(substr($line[2], strpos($line[2], "\t") + 1));
                 foreach ($rebuilders as $rebuilder) {
                     $rebuilder->onObject($name, $id);
                 }
             } catch (RuntimeException $e) {
                 gb::log(LOG_ERR, 'failed to rebuild object %s %s: %s', var_export($name, 1), $e->getMessage(), $e->getTraceAsString());
                 $failures[] = array($rebuilder, $name);
             }
         }
     }
     # Let rebuilders finalize
     foreach ($rebuilders as $rebuilder) {
         try {
             $rebuilder->finalize();
         } catch (RuntimeException $e) {
             gb::log(LOG_ERR, 'rebuilder %s (0x%x) failed to finalize: %s', get_class($rebuilder), spl_object_hash($rebuilder), GBException::format($e, true, false, null, 0));
             $failures[] = array($rebuilder, null);
         }
     }
     gb::log(LOG_NOTICE, 'cache updated -- time spent: %s', gb_format_duration(microtime(1) - $time_started));
     return $failures;
 }
All Usage Examples Of git::exec