PHP Class VersionPress\Git\GitRepository

Exibir arquivo Open project: versionpress/versionpress Class Usage Examples

Public Methods

Method Description
__construct ( string $workingDirectoryRoot, string $tempDirectory = null, string $commitMessagePrefix = "[VP] ", string $gitBinary = "git" )
abortRevert ( ) Aborts a revert, e.g., if there was a conflict
clearWorkingDirectory ( ) : boolean Discards all modifications made to files in working directory.
commit ( CommitMessage | string $message, string $authorName, string $authorEmail ) Creates a commit.
getChildCommit ( $commitHash ) : mixed Returns child (newer) commit. Assumes there is only a single child commit.
getCommit ( $commitHash ) : Commit Gets commit object based on its hash
getDiff ( string $hash = null ) : string Returns diff for given commit.
getFileInRevision ( $path, $commitHash )
getFileModifications ( $file )
getInitialCommit ( ) : Commit Returns the initial (oldest) commit in the repo
getLastCommitHash ( string $options = "" ) : string Gets last (most recent) commit hash in the repository, or an empty string is there are no commits.
getModifiedFiles ( string $gitrevisions ) : string[] Returns list of files that were modified in given {@link http://git-scm.com/docs/gitrevisions gitrevisions}
getModifiedFilesWithStatus ( string $gitrevisions ) : array Like getModifiedFiles() but also returns the status of each file ("A" for added, "M" for modified, "D" for deleted and "R" for renamed).
getNumberOfCommits ( string $options = "", string $gitrevisions = "" ) : integer Counts number of commits
getStatus ( $array = false ) : string | array[string] Returns git status in short format, something like:
init ( ) Initializes the repository
isCleanWorkingDirectory ( ) : boolean Returns true if there are no changes to commit.
isVersioned ( ) : boolean True if the working directory is versioned
log ( string $options = "", string $gitrevisions = "" ) : Commit[] Returns an array of Commits based on {@link http://git-scm.com/docs/gitrevisions gitrevisions}
revert ( $commitHash ) : boolean Reverts a single commit. If there is a conflict, aborts the revert and returns false.
revertAll ( $commitHash ) Reverts all changes up to a given commit - performs a "rollback"
setGitProcessTimeout ( integer $gitProcessTimeout ) Changes the timeout of {@link \Symfony\Component\Process\Process}
stageAll ( string | null $path = null ) Stages all files under the given path. No path = stage all files in whole working directory.
wasCreatedAfter ( $commitHash, $afterWhichCommitHash ) : boolean Returns true if $commitHash was created after the $afterWhichCommitHash commit ("after" meaning that $commitHash is more recent commit, a child of $afterWhat). Same two commits return false.
willCommit ( ) : boolean Returns true if there is something to commit

Private Methods

Method Description
runProcess ( $cmd ) : array Low-level helper, generally use runShellCommand()
runShellCommand ( string $command, variadic $args ) : array Run a Git command, either fully specified (e.g., 'git log') or just by the name (e.g., 'log').
runShellCommandWithErrorOutput ( string $command, string $args = '' ) : string Invokes {@see runShellCommand()} and returns its stderr output. The params are the same, only the return type is string instead of an array.
runShellCommandWithStandardOutput ( string $command, string $args = '' ) : string Invokes {@see runShellCommand()} and returns its stdout output. The params are the same, only the return type is string instead of an array.

Method Details

__construct() public method

public __construct ( string $workingDirectoryRoot, string $tempDirectory = null, string $commitMessagePrefix = "[VP] ", string $gitBinary = "git" )
$workingDirectoryRoot string Filesystem path to working directory root (where the .git folder resides)
$tempDirectory string Directory used for commit message temp file. See commit().
$commitMessagePrefix string Standard prefix applied to all commit messages
$gitBinary string Git binary to use

abortRevert() public method

Aborts a revert, e.g., if there was a conflict
public abortRevert ( )

clearWorkingDirectory() public method

Also deletes untracked files.
public clearWorkingDirectory ( ) : boolean
return boolean

commit() public method

Uses a temporary file to construct the commit message because on Windows, multi-line commit message cannot be created on CLI and it's generally a more flexible solution (very long commit messages, etc.).
public commit ( CommitMessage | string $message, string $authorName, string $authorEmail )
$message CommitMessage | string
$authorName string
$authorEmail string

getChildCommit() public method

Returns child (newer) commit. Assumes there is only a single child commit.
public getChildCommit ( $commitHash ) : mixed
$commitHash
return mixed

getCommit() public method

Gets commit object based on its hash
public getCommit ( $commitHash ) : Commit
$commitHash
return Commit

getDiff() public method

If null, returns diff of working directory and HEAD
public getDiff ( string $hash = null ) : string
$hash string
return string

getFileInRevision() public method

public getFileInRevision ( $path, $commitHash )

getFileModifications() public method

public getFileModifications ( $file )

getInitialCommit() public method

Returns the initial (oldest) commit in the repo
public getInitialCommit ( ) : Commit
return Commit

getLastCommitHash() public method

Gets last (most recent) commit hash in the repository, or an empty string is there are no commits.
public getLastCommitHash ( string $options = "" ) : string
$options string Options passed to git log
return string Empty string or SHA1

getModifiedFiles() public method

Returns list of files that were modified in given {@link http://git-scm.com/docs/gitrevisions gitrevisions}
public getModifiedFiles ( string $gitrevisions ) : string[]
$gitrevisions string
return string[]

getModifiedFilesWithStatus() public method

Like getModifiedFiles() but also returns the status of each file ("A" for added, "M" for modified, "D" for deleted and "R" for renamed).
public getModifiedFilesWithStatus ( string $gitrevisions ) : array
$gitrevisions string See gitrevisions
return array Array of things like `array("status" => "M", "path" => "wp-content/vpdb/something.ini" )`

getNumberOfCommits() public method

Counts number of commits
public getNumberOfCommits ( string $options = "", string $gitrevisions = "" ) : integer
$options string Options passed to git log
$gitrevisions string Empty by default, i.e., calling full 'git log'
return integer

getStatus() public method

A path1.txt M path2.txt Clean working directory returns an empty string.
public getStatus ( $array = false ) : string | array[string]
$array bool Return result as array
return string | array[string]

init() public method

Initializes the repository
public init ( )

isCleanWorkingDirectory() public method

Returns true if there are no changes to commit.

isVersioned() public method

True if the working directory is versioned
public isVersioned ( ) : boolean
return boolean

log() public method

Returns an array of Commits based on {@link http://git-scm.com/docs/gitrevisions gitrevisions}
public log ( string $options = "", string $gitrevisions = "" ) : Commit[]
$options string Options passed to git log
$gitrevisions string Empty by default, i.e., calling full 'git log'
return Commit[]

revert() public method

Reverts a single commit. If there is a conflict, aborts the revert and returns false.
public revert ( $commitHash ) : boolean
$commitHash
return boolean True if it succeeded, false if there was a conflict

revertAll() public method

Reverts all changes up to a given commit - performs a "rollback"
public revertAll ( $commitHash )
$commitHash

setGitProcessTimeout() public method

Changes the timeout of {@link \Symfony\Component\Process\Process}
public setGitProcessTimeout ( integer $gitProcessTimeout )
$gitProcessTimeout integer

stageAll() public method

Stages all files under the given path. No path = stage all files in whole working directory.
public stageAll ( string | null $path = null )
$path string | null Null (the default) means the whole working directory

wasCreatedAfter() public method

Returns true if $commitHash was created after the $afterWhichCommitHash commit ("after" meaning that $commitHash is more recent commit, a child of $afterWhat). Same two commits return false.
public wasCreatedAfter ( $commitHash, $afterWhichCommitHash ) : boolean
$commitHash
$afterWhichCommitHash
return boolean

willCommit() public method

Returns true if there is something to commit
public willCommit ( ) : boolean
return boolean