Git.php

A PHP git library

This project is maintained by kbjr

Git.php

Description

A PHP git repository control library. Allows the running of any git command from a PHP class. Runs git commands using proc_open, not exec or the type, therefore it can run in PHP safe mode.

Requirements

A system with git installed

Basic Use

require_once('Git.php');

$repo = Git::open('/path/to/repo');  // -or- Git::create('/path/to/repo')

$repo->add('.');
$repo->commit('Some commit message');
$repo->push('origin', 'master');

API Reference

Git

string Git::get_bin ( void )

Returns the path currently being used to run git.

echo Git::get_bin();  // "/usr/bin/git"

void Git::set_bin ( string $path )

Sets the location where git can be found

void Git::windows_mode ( void )

Enables "windows mode", a set of changes designed to make the library work better in a Windows environment. As of the moment, those changes include:

GitRepo Git::create ( string $repo_path[, string $source = null ])

Creates a new git repositiory at the given $repo_path location. If a $source is given, the new repo will be cloned from that location.

$repo = Git::create('/home/me/projects/create_repo_here');

GitRepo Git::open ( string $repo_path )

Opens an existing git repository.

$repo = Git::open('/home/me/projects/existing_repo');

GitRepo Git::is_repo ( mixed $repo )

Returns true if $repo is an instance of GitRepo, or returns false otherwise.

GitRepo

GitRepo GitRepo::create_new ( string $repo_path[, string $source ])

Used internally by Git::create.

GitRepo->__construct ([ string $repo_path = null[, bool $create_new = false ]])

The GitRepo constructor. If no arguments are given, a non-initialize GitRepo object will be created. As a non-initialize instance has no path, you cannot call git commands with it. The first param, $repo_path, defines the path of the repository to open. The second, $create_new, defines whether or not a new repository should be created if one was not found at the given path.

// Open an existing repo
$repo = new GitRepo('/home/me/projects/existing_repo');

// Create a new repo
$newRepo = new GitRepo('/home/me/projects/create_repo_here', true);

void GitRepo->set_repo_path ( string $repo_path[, bool $create_new ])

Takes the same parameters as the constructor above, except that the repo path is required here. Will set the path on a non-initialize GitRepo object, or change the path on an already existing one.

bool GitRepo->test_git ( void )

Tests if git is installed in the defined location and working properly by attempting to call to git. Returns true if git is installed, false if the call could not be made.

string GitRepo->run_command ( string $command )

Runs an arbitrary command in the defined path using proc_open. Returns the contents of stdout in the case of a successful run, throws an Exception in the case of a non-zero status.

echo $repo->run_command('ls');

string GitRepo->run ( string $command )

Runs a git command in the defined path using GitRepo->run_command.

echo $repo->run('status');  // Runs `$ git status`

string GitRepo->status ([ bool $html = false ])

Returns the result of calling $ git status. If the $html param is set to true, newlines will be replaced with "<br />" for output in HTML.

string GitRepo->add ([ mixed $files = "*" ])

Runs $ git add ${files} -v. If $files is an array, it will be converted into a properly formatted string.

string GitRepo->rm ([ mixed $files = "*"[, bool $cached = false ]])

Runs $ git rm ${files}. If $files is an array, it will be converted into a properly formatted string. If the $cached is true, the --cached flag will be added to the command.

string GitRepo->commit ([ string $message = "" ])

Runs $ git commit -av -m "${message}".

string GitRepo->clone_to ( string $target )

Clones the repo into a new path using $ git clone --local.

string GitRepo->clone_from ( string $source )

Clones another repo into this path using $ git clone --local.

string GitRepo->clone_remote ( string $source )

Clones a remote repo into this path using $ git clone.

string GitRepo->clean ([ bool $dirs = false[, bool $force = false ]])

Runs $ git clean. If $dirs is true, the -d flag will be added. If $force is true, the -f flag will be added.

string GitRepo->create_branch ( string $branch )

Creates a new branch using $ git branch ${branch}.

string GitRepo->delete_branch ( string $branch[, bool $force = false ])

Deletes an existing branch using $ git branch -d ${branch}. If $force is true, the deletion will be forced using the -D flag.

array GitRepo->list_branches ([ bool $keep_asterisk = false ])

Returns an array of all existing local branches. If the $keep_asterisk flag is true, the currently checked out branch will still have the asterisk by its name, otherwise it is removed.

$branches = $repo->list_branches();
foreach ($branches as $branch) {
    echo $branch;
}

array GitRepo->list_remote_branches ( void )

Returns an array of all remote branches using $ git branch -r.

string GitRepo->active_branch ([ bool $keep_asterisk = false ])

Returns the name of the currently active branch. If the $keep_asterisk flag is true, the currently checked out branch will still have the asterisk by its name, otherwise it is removed.

string GitRepo->checkout ( string $branch )

Checkout the given branch.

string GitRepo->merge ( string $branch )

Merge another branch into the current branch using $ git merge ${branch} --no-ff.

string GitRepo->fetch ( void )

Run $ git fetch.

string GitRepo->add_tag ( string $tag[, string $message = null ])

Adds a new tag to the current ref using $ git tag -a ${tag} -m ${message}. If a message is not given, the tag name will be used as the message.

$repo->add_tag('v1.0');  // Runs `$ git tag -a "v1.0" -m "v1.0"`

array GitRepo->list_tags ([ string $pattern = null ])

Returns an array of all existing tags matching the given pattern using $ git tag -l ${pattern}.

string GitRepo->push ( string $remote, string $branch )

Pushes the given branch out to the given remote.

$repo->push('origin', 'master');  // Runs `$ git push origin master`

string GitRepo->pull ( string remote, string $branch )

Pulls the given branch from the given remote.

void GitRepo->set_description ( string $description )

Sets the project description stored in "repo/.git/description".

string GitRepo->get_description ( void )

Gets the project description stored in "repo/.git/description".

void GitRepo->set_env ( string $key, string $value )

Sets an environment variable for future commands.