Overview

Namespaces

  • Cz
    • Git

Classes

  • Cz\Git\Git

Interfaces

  • Cz\Git\IGit

Exceptions

  • Cz\Git\GitException
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 
<?php
    /** Default Implementation of IGit Interface
     * 
     * @author      Jan Pecha, <janpecha@email.cz>
     */
    
    namespace Cz\Git;
    
    class Git implements IGit
    {
        /**
         * Creates a tag.
         * @param   string
         * @throws  Cz\Git\GitException
         * @return  Cz\Git\Git
         */
        public function tag($name)
        {
            $this->run('git tag', $name);
            return $this;
        }
        
        
        
        /**
         * Merges branches.
         * @param   string
         * @param   array|NULL
         * @throws  Cz\Git\GitException
         * @return  Cz\Git\Git
         */
        public function merge($branch, $options = NULL)
        {
            $this->run('git merge', $options, $branch);
            return $this;
        }
        
        
        
        /**
         * Creates new branch.
         * @param   string
         * @param   bool
         * @throws  Cz\Git\GitException
         * @return  Cz\Git\Git
         */
        public function branchCreate($name, $checkout = FALSE)
        {
            // git branch $name
            $this->run('git branch', $name);
            
            if($checkout)
            {
                $this->checkout($name);
            }
            
            return $this;
        }
        
        
        
        /**
         * Removes branch.
         * @param   string
         * @throws  Cz\Git\GitException
         * @return  Cz\Git\Git
         */
        public function branchRemove($name)
        {
            $this->run('git branch', array(
                '-d' => $name,
            ));
            return $this;
        }
        
        
        
        /**
         * Gets name of current branch
         * @return  string
         * @throws  Cz\Git\GitException
         */
        public function branchName()
        {
            $output = array();
            $exitCode = NULL;
            
            exec('git branch', $output, $exitCode);
            
            if($exitCode === 0 && is_array($output))
            {
                foreach($output as $line)
                {
                    if($line[0] === '*')
                    {
                        return trim(substr($line, 1));
                    }
                }
            }
            
            throw new GitException('Getting current branch name failed.');
        }
        
        
        
        /**
         * Checkout branch.
         * @param   string
         * @throws  Cz\Git\GitException
         * @return  Cz\Git\Git
         */
        public function checkout($name)
        {
            $this->run('git checkout', $name);
            return $this;
        }
        
        
        
        /**
         * Removes file(s).
         * @param   string|string[]
         * @throws  Cz\Git\GitException
         * @return  Cz\Git\Git
         */
        public function remove($file)
        {
            if(!is_array($file))
            {
                $file = func_get_args();
            }
            
            foreach($file as $item)
            {
                $this->run('git rm', $item, '-r');
            }
            
            return $this;
        }
        
        
        
        /**
         * Adds file(s).
         * @param   string|string[]
         * @throws  Cz\Git\GitException
         * @return  Cz\Git\Git
         */
        public function add($file)
        {
            if(!is_array($file))
            {
                $file = func_get_args();
            }
            
            foreach($file as $item)
            {
                $this->run('git add', $item);
            }
            
            return $this;
        }
        
        
        
        /**
         * Renames file(s).
         * @param   string|string[]  from: array('from' => 'to', ...) || (from, to)
         * @param   string|NULL
         * @throws  Cz\Git\GitException
         * @return  Cz\Git\Git
         */
        public function rename($file, $to = NULL)
        {
            if(!is_array($file)) // rename(file, to);
            {
                $from[$file] = $to;
                $file = $from;
                unset($from);
            }
            
            foreach($file as $from => $to)
            {
                $this->run('git mv', $from, $to);
            }
            
            return $this;
        }
        
        
        
        /**
         * Commits changes
         * @param   string
         * @param   string[]  param => value
         * @throws  Cz\Git\GitException
         * @return  Cz\Git\Git
         */
        public function commit($message, $params = NULL)
        {
            if(!is_array($params))
            {
                $params = array();
            }
            
            $this->run("git commit", $params, array(
                '-m' => $message,
            ));
            return $this;
        }
        
        
        
        /**
         * Exists changes?
         * @return  bool
         */
        public function isChanges()
        {
            $lastLine = exec('git status');
            
            return (strpos($lastLine, 'nothing to commit')) === FALSE; // FALSE => changes
        }
        
        
        
        /** Runs command.
         * @param   string|array
         * @return  void
         * @throws  Cz\Git\GitException
         */
        protected function run($cmd/*, $options = NULL*/)
        {
            $args = func_get_args();
            $cmd = array();
            
            $programName = array_shift($args);
            
            foreach($args as $arg)
            {
                if(is_array($arg))
                {
                    foreach($arg as $key => $value)
                    {
                        $_c = '';
                        
                        if(is_string($key))
                        {
                            $_c = "$key ";
                        }
                        
                        $cmd[] = $_c . escapeshellarg($value);
                    }
                }
                elseif(is_scalar($arg) && !is_bool($arg))
                {
                    $cmd[] = escapeshellarg($arg);
                }
            }
            
            $cmd = "$programName " . implode(' ', $cmd);
            $success = system($cmd, $ret);
            
            if($success === FALSE || $ret !== 0)
            {
                throw new GitException("Command '$cmd' failed.");
            }
        }
    }

czproject/git-php v1.0.2 API documentation API documentation generated by ApiGen