PHP Class TQ\Git\Repository\Repository

Show file Open project: teqneers/php-stream-wrapper-for-git Class Usage Examples

Protected Properties

Property Type Description
$git TQ\Git\Cli\Binary The Git binary

Public Methods

Method Description
add ( array $file = null, boolean $force = false ) Adds one or more files to the staging area
commit ( string $commitMsg, array $file = null, string | null $author = null, array $extraArgs = [] ) Commits the currently staged changes into the repository
createDirectory ( string $path, string | null $commitMsg = null, integer | null $dirMode = null, boolean $recursive = true, string | null $author = null ) : string Writes data to a file and commit the changes immediately
findRepositoryRoot ( string $path ) : string | null Tries to find the root directory for a given repository path
getBranches ( integer $which = self::BRANCHES_LOCAL ) : array Returns a list of the branches in the repository
getCurrentBranch ( ) : string Returns the name of the current branch
getCurrentCommit ( ) : string Returns the current commit hash
getCurrentRemote ( ) : array Returns the remote info
getGit ( ) : Binary Returns the Git binary
getLog ( integer | null $limit = null, integer | null $skip = null ) : array Returns the current repository log
getObjectInfo ( string $path, string $ref = 'HEAD' ) : array Returns information about an object at a given version
getStatus ( ) : array Returns the current status of the working directory and the staging area
isDirty ( ) : boolean Returns true if there are uncommitted changes in the working directory and/or the staging area
listDirectory ( string $directory = '.', string $ref = 'HEAD' ) : array List the directory at a given version
move ( string $fromPath, string $toPath, boolean $force = false ) Renames a file but does not commit the changes
open ( string $repositoryPath, Binary | string | null $git = null, boolean | integer $createIfNotExists = false, array | null $initArguments = null, boolean $findRepositoryRoot = true ) : Repository Opens a Git repository on the file system, optionally creates and initializes a new repository
remove ( array $file, boolean $recursive = false, boolean $force = false ) Removes one or more files from the repository but does not commit the changes
removeFile ( string $path, string | null $commitMsg = null, boolean $recursive = false, boolean $force = false, string | null $author = null ) : string Removes a file and commit the changes immediately
renameFile ( string $fromPath, string $toPath, string | null $commitMsg = null, boolean $force = false, string | null $author = null ) : string Renames a file and commit the changes immediately
reset ( integer $what = self::RESET_ALL ) Resets the working directory and/or the staging area and discards all changes
showCommit ( string $hash ) : string Returns a string containing information about the given commit
showFile ( string $file, string $ref = 'HEAD' ) : string Returns the content of a file at a given version
writeFile ( string $path, string | array $data, string | null $commitMsg = null, integer | null $fileMode = null, integer | null $dirMode = null, boolean $recursive = true, string | null $author = null ) : string Writes data to a file and commit the changes immediately

Protected Methods

Method Description
__construct ( string $repositoryPath, Binary $git ) Creates a new repository instance - use {@see open()} instead
_parseNamedArguments ( array $regularStyleArguments, array $namedStyleArguments, array $arguments, integer $skipNamedTo ) : array _parseNamedArguments
_prepareNamedArgumentsForCLI ( array $namedArguments ) : array Prepares a list of named arguments for use as command-line arguments.
initRepository ( Binary $git, string $path, array $initArguments = null ) Initializes a path to be used as a Git repository

Method Details

__construct() protected method

Creates a new repository instance - use {@see open()} instead
protected __construct ( string $repositoryPath, Binary $git )
$repositoryPath string
$git TQ\Git\Cli\Binary

_parseNamedArguments() protected method

Takes a set of regular arguments and a set of extended/named arguments, combines them, and returns the results. The merging method is far from foolproof, but should take care of the vast majority of situations. Where it fails is function calls in which the an argument is regular-style, is an array, and only has keys which are present in the named arguments. The easy way to trigger it would be to pass an empty array in one of the arguments. There's a bunch of array_splices. Those are in place so that if named arguments have orders that they should be called in, they're not disturbed. So... calling with getLog(5, ['reverse', 'diff' => 'git', 'path/to/repo/file.txt'] will keep things in the order for the git call: git-log --limit=5 --skip=10 --reverse --diff=git path/to/to/repo/file.txt and will put defaults at the beginning of the call, as well.
protected _parseNamedArguments ( array $regularStyleArguments, array $namedStyleArguments, array $arguments, integer $skipNamedTo ) : array
$regularStyleArguments array An ordered list of the names of regular-style arguments that should be accepted.
$namedStyleArguments array An associative array of named arguments to their default value, or null where no default is desired.
$arguments array The result of func_get_args() in the original function call we're helping.
$skipNamedTo integer Index to which array arguments should be assumed NOT to be named arguments.
return array A filtered associative array of the resulting arguments.

_prepareNamedArgumentsForCLI() protected method

Preserves ordering, while prepending - and -- to argument names, then leaves value alone.
protected _prepareNamedArgumentsForCLI ( array $namedArguments ) : array
$namedArguments array Named argument list to format
return array

add() public method

Adds one or more files to the staging area
public add ( array $file = null, boolean $force = false )
$file array The file(s) to be added or NULL to add all new and/or changed files to the staging area
$force boolean

commit() public method

Commits the currently staged changes into the repository
public commit ( string $commitMsg, array $file = null, string | null $author = null, array $extraArgs = [] )
$commitMsg string The commit message
$file array Restrict commit to the given files or NULL to commit all staged changes
$author string | null The author
$extraArgs array Allow the user to pass extra args eg array('-i')

createDirectory() public method

Writes data to a file and commit the changes immediately
public createDirectory ( string $path, string | null $commitMsg = null, integer | null $dirMode = null, boolean $recursive = true, string | null $author = null ) : string
$path string The directory path
$commitMsg string | null The commit message used when committing the changes
$dirMode integer | null The mode for creating the intermediate directories
$recursive boolean Create intermediate directories recursively if required
$author string | null The author
return string The current commit hash

findRepositoryRoot() public static method

Tries to find the root directory for a given repository path
public static findRepositoryRoot ( string $path ) : string | null
$path string The file system path
return string | null NULL if the root cannot be found, the root path otherwise

getBranches() public method

Returns a list of the branches in the repository
public getBranches ( integer $which = self::BRANCHES_LOCAL ) : array
$which integer Which branches to retrieve (all, local or remote-tracking)
return array

getCurrentBranch() public method

Returns the name of the current branch
public getCurrentBranch ( ) : string
return string

getCurrentCommit() public method

Returns the current commit hash
public getCurrentCommit ( ) : string
return string

getCurrentRemote() public method

Returns the remote info
public getCurrentRemote ( ) : array
return array

getGit() public method

Returns the Git binary
public getGit ( ) : Binary
return TQ\Git\Cli\Binary

getLog() public method

Returns the current repository log
public getLog ( integer | null $limit = null, integer | null $skip = null ) : array
$limit integer | null The maximum number of log entries returned
$skip integer | null Number of log entries that are skipped from the beginning
return array

getObjectInfo() public method

The information returned is an array with the following structure array( 'type' => blob|tree|commit, 'mode' => 0040000 for a tree, 0100000 for a blob, 0 otherwise, 'size' => the size )
public getObjectInfo ( string $path, string $ref = 'HEAD' ) : array
$path string The path to the object
$ref string The version ref
return array The object info

getStatus() public method

The returned array structure is array( 'file' => '...', 'x' => '.', 'y' => '.', 'renamed' => null/'...' )
public getStatus ( ) : array
return array

initRepository() protected static method

Initializes a path to be used as a Git repository
protected static initRepository ( Binary $git, string $path, array $initArguments = null )
$git TQ\Git\Cli\Binary The Git binary
$path string The repository path
$initArguments array Arguments to pass to git-init when initializing the repository

isDirty() public method

Returns true if there are uncommitted changes in the working directory and/or the staging area
public isDirty ( ) : boolean
return boolean

listDirectory() public method

List the directory at a given version
public listDirectory ( string $directory = '.', string $ref = 'HEAD' ) : array
$directory string The path ot the directory
$ref string The version ref
return array

move() public method

Renames a file but does not commit the changes
public move ( string $fromPath, string $toPath, boolean $force = false )
$fromPath string The source path
$toPath string The destination path
$force boolean True to continue even though Git reports a possible conflict

open() public static method

Opens a Git repository on the file system, optionally creates and initializes a new repository
public static open ( string $repositoryPath, Binary | string | null $git = null, boolean | integer $createIfNotExists = false, array | null $initArguments = null, boolean $findRepositoryRoot = true ) : Repository
$repositoryPath string The full path to the repository
$git TQ\Git\Cli\Binary | string | null The Git binary
$createIfNotExists boolean | integer False to fail on non-existing repositories, directory creation mode, such as 0755 if the command should create the directory and init the repository instead
$initArguments array | null Arguments to be passed to git-init if initializing a repository
$findRepositoryRoot boolean False to use the repository path as the root directory.
return Repository

remove() public method

Removes one or more files from the repository but does not commit the changes
public remove ( array $file, boolean $recursive = false, boolean $force = false )
$file array The file(s) to be removed
$recursive boolean True to recursively remove subdirectories
$force boolean True to continue even though Git reports a possible conflict

removeFile() public method

Removes a file and commit the changes immediately
public removeFile ( string $path, string | null $commitMsg = null, boolean $recursive = false, boolean $force = false, string | null $author = null ) : string
$path string The file path
$commitMsg string | null The commit message used when committing the changes
$recursive boolean True to recursively remove subdirectories
$force boolean True to continue even though Git reports a possible conflict
$author string | null The author
return string The current commit hash

renameFile() public method

Renames a file and commit the changes immediately
public renameFile ( string $fromPath, string $toPath, string | null $commitMsg = null, boolean $force = false, string | null $author = null ) : string
$fromPath string The source path
$toPath string The destination path
$commitMsg string | null The commit message used when committing the changes
$force boolean True to continue even though Git reports a possible conflict
$author string | null The author
return string The current commit hash

reset() public method

Resets the working directory and/or the staging area and discards all changes
public reset ( integer $what = self::RESET_ALL )
$what integer Bit mask to indicate which parts should be reset

showCommit() public method

Returns a string containing information about the given commit
public showCommit ( string $hash ) : string
$hash string The commit ref
return string

showFile() public method

Returns the content of a file at a given version
public showFile ( string $file, string $ref = 'HEAD' ) : string
$file string The path to the file
$ref string The version ref
return string

writeFile() public method

Writes data to a file and commit the changes immediately
public writeFile ( string $path, string | array $data, string | null $commitMsg = null, integer | null $fileMode = null, integer | null $dirMode = null, boolean $recursive = true, string | null $author = null ) : string
$path string The file path
$data string | array The data to write to the file
$commitMsg string | null The commit message used when committing the changes
$fileMode integer | null The mode for creating the file
$dirMode integer | null The mode for creating the intermediate directories
$recursive boolean Create intermediate directories recursively if required
$author string | null The author
return string The current commit hash

Property Details

$git protected property

The Git binary
protected Binary,TQ\Git\Cli $git
return TQ\Git\Cli\Binary