Git
in package
HordeComponents\Helper\Git:: wraps git operations.
Git Helper provides two classes of methods:
Primitives which just do what they say Primitives do not produce output unless by invoking commands
Workflows which provide some higher level usefulness Workflow methods understand pretend mode and use output
Copyright 2020-2021 Horde LLC (http://www.horde.org/)
See the enclosed file LICENSE for license information (LGPL). If you did not receive this file, see http://www.horde.org/licenses/lgpl21.
Tags
Table of Contents
- $cwd : string
- $gitBin : string
- __construct() : mixed
- Constructor
- add() : void
- Add a path to be included in the commit and record the working directory for this git operation.
- addRemote() : void
- branchFromLocal() : SystemCallResult
- Create a branch from a local source branch
- checkoutBranch() : SystemCallResult
- Checkout a local branch (primitive)
- checkoutIsClean() : bool
- Ensure the git checkout in a dir does not contain uncommitted changes
- clone() : SystemCallResult
- Clone a remote repo (primitive)
- commit() : void
- Add all modified files and commit them.
- createRemoteTrackingBranch() : SystemCallResult
- Create a local branch from a remote of same name
- deleteLocalBranch() : mixed
- detectGitBin() : string
- Check some well known locations, fallback to which
- fetch() : SystemCallResult
- Fetch remote metadata
- getCurrentBranch() : string
- Get currently checked out branch
- getCurrentRefName() : string
- Check the git repo's current position for a branch name, tag name or bare position.
- getLocalBranches() : array<string|int, string>
- Run git branch
- getLocalTags() : array<string|int, string>
- List the local git tags in the repository
- getRemoteBranches() : array<string|int, string>
- Run git branch -r
- localBranchExists() : bool
- Check if a local branch exists
- localTagExists() : bool
- pull() : void
- push() : void
- rebase() : string
- Rebase branch on a local or remote source
- remoteBranchExists() : bool
- Check if a remote branch exists as of last fetch
- removeRemote() : void
- tag() : void
- Tag the component.
- updateRemote() : void
- workflowBranch() : bool
- Workflow create a branch if missing
- workflowCheckout() : bool
- Workflow Checkout a branch
- workflowClone() : bool
- Workflow: Clone a component
- workflowUpdate() : bool
- Workflow update a branch
- exec() : SystemCallResult
- Run a system call and capture output.
- execInDirectory() : SystemCallResult
- Run a system call in a given dir and capture output.
- system() : string
- Run a system call.
- systemInDirectory() : string
- Run a system call.
Properties
$cwd
protected
string
$cwd
Working directory
$gitBin
protected
string
$gitBin
Path to git binary
Methods
__construct()
Constructor
public
__construct([string $gitBin = '' ][, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $gitBin : string = ''
-
Path to git binary. Empty string to autodetect.
- $options : array<string|int, mixed> = []
-
Any options this helper consumes. None yet.
Return values
mixed —add()
Add a path to be included in the commit and record the working directory for this git operation.
public
add(string $item) : void
Parameters
- $item : string
-
The relative path to the modified file
Return values
void —addRemote()
public
addRemote() : void
Return values
void —branchFromLocal()
Create a branch from a local source branch
public
branchFromLocal(string $localDir, string $branch, string $source) : SystemCallResult
Parameters
- $localDir : string
-
The repo path
- $branch : string
-
The new branch's name
- $source : string
-
The source branch to use
Return values
SystemCallResult —checkoutBranch()
Checkout a local branch (primitive)
public
checkoutBranch(string $localDir, string $branch) : SystemCallResult
Parameters
- $localDir : string
-
Full path to repo
- $branch : string
-
The branch to check for
Return values
SystemCallResult —checkoutIsClean()
Ensure the git checkout in a dir does not contain uncommitted changes
public
checkoutIsClean(string $localDir) : bool
Parameters
- $localDir : string
Return values
bool —clone()
Clone a remote repo (primitive)
public
clone(string $uri, string $localDir[, string $branch = '' ]) : SystemCallResult
Parameters
- $uri : string
-
The remote repo to clone
- $localDir : string
-
The local target dir
- $branch : string = ''
-
Optional: Checkout a specific branch
Return values
SystemCallResult —commit()
Add all modified files and commit them.
public
commit(string $localDir, string $log) : void
Parameters
- $localDir : string
- $log : string
-
The commit message.
Return values
void —createRemoteTrackingBranch()
Create a local branch from a remote of same name
public
createRemoteTrackingBranch(string $localDir, string $branch[, string $remote = 'origin' ]) : SystemCallResult
Parameters
- $localDir : string
-
The repo path
- $branch : string
-
The new branch's name
- $remote : string = 'origin'
-
The remote
Return values
SystemCallResult —deleteLocalBranch()
public
deleteLocalBranch(string $localDir, string $branch) : mixed
Parameters
- $localDir : string
- $branch : string
Return values
mixed —detectGitBin()
Check some well known locations, fallback to which
public
detectGitBin() : string
Tags
Return values
string —Fully qualified location of git command
fetch()
Fetch remote metadata
public
fetch(string $localDir) : SystemCallResult
Parameters
- $localDir : string
-
Full path to repo
Return values
SystemCallResult —getCurrentBranch()
Get currently checked out branch
public
getCurrentBranch(string $localDir) : string
Parameters
- $localDir : string
-
Full path to repo
Return values
string —The active branch
getCurrentRefName()
Check the git repo's current position for a branch name, tag name or bare position.
public
getCurrentRefName(string $localDir) : string
Parameters
- $localDir : string
Return values
string —getLocalBranches()
Run git branch
public
getLocalBranches(string $localDir) : array<string|int, string>
Parameters
- $localDir : string
-
Full path to repo
Return values
array<string|int, string> —Local branches
getLocalTags()
List the local git tags in the repository
public
getLocalTags(string $localDir) : array<string|int, string>
Parameters
- $localDir : string
-
The path to the repository
Return values
array<string|int, string> —A list of available tags
getRemoteBranches()
Run git branch -r
public
getRemoteBranches(string $localDir) : array<string|int, string>
Parameters
- $localDir : string
Return values
array<string|int, string> —Remote branches
localBranchExists()
Check if a local branch exists
public
localBranchExists(string $localDir, string $branch) : bool
Parameters
- $localDir : string
-
Full path to repo
- $branch : string
-
The branch to check for
Return values
bool —True if exists
localTagExists()
public
localTagExists(string $localDir, string $tag) : bool
Parameters
- $localDir : string
- $tag : string
Return values
bool —pull()
public
pull() : void
Return values
void —push()
public
push(string $localDir[, string $remote = 'origin' ][, string|null $ref = null ][, bool $force = false ]) : void
Parameters
- $localDir : string
-
The checkout dir of the component
- $remote : string = 'origin'
-
Optional remote, defaults to origin
- $ref : string|null = null
- $force : bool = false
Return values
void —rebase()
Rebase branch on a local or remote source
public
rebase(string $localDir, string $branch[, string $source = '' ]) : string
Parameters
- $localDir : string
-
Full path to repo
- $branch : string
-
Full path to repo
- $source : string = ''
-
Full path to repo. If empty, origin/branch
Return values
string —SystemCallResult
remoteBranchExists()
Check if a remote branch exists as of last fetch
public
remoteBranchExists(string $localDir, string $branch[, string $remote = 'origin' ]) : bool
Parameters
- $localDir : string
-
Full path to repo
- $branch : string
-
The branch to check for
- $remote : string = 'origin'
Return values
bool —True if exists
removeRemote()
public
removeRemote() : void
Return values
void —tag()
Tag the component.
public
tag(string $localDir, string $tag, string $message[, bool $force = false ]) : void
Parameters
- $localDir : string
- $tag : string
-
Tag name.
- $message : string
-
Tag message.
- $force : bool = false
-
If the tag already exists, overwrite it.
Return values
void —updateRemote()
public
updateRemote() : void
Return values
void —workflowBranch()
Workflow create a branch if missing
public
workflowBranch(Output $output, string $componentDir, string $branch, string $source) : bool
Return success if already present. If missing, Set up a known remote branch as a tracking local branch Create a missing branch from local source branch Create a missing local source branch from remote first
Return false if missing and source branch also missing.
Do nothing and return success if already checked out Return false if neither local nor remote branch found
Parameters
- $output : Output
-
The output utility
- $componentDir : string
-
The Component checkout dir
- $branch : string
-
The branch to check out
- $source : string
-
The source branch
Return values
bool —True if workflow was successful
workflowCheckout()
Workflow Checkout a branch
public
workflowCheckout(Output $output, string $componentDir, string $branch) : bool
Checkout an existing local branch or Set up a known remote branch as a tracking local branch.
Do nothing and return success if already checked out Return false if neither local nor remote branch found
Parameters
- $output : Output
-
The output utility
- $componentDir : string
-
The Component checkout dir
- $branch : string
-
The branch to check out
Return values
bool —True if workflow was successful
workflowClone()
Workflow: Clone a component
public
workflowClone(Output $output, string $cloneUrl, string $componentDir[, string $branch = '' ]) : bool
Add safety logic and output to the bare clone command Authentication/Secrets handling is out of scope. Configure git secrets handler or ssh key externally if needed.
Parameters
- $output : Output
-
The output utility
- $cloneUrl : string
-
The source repo URI
- $componentDir : string
-
Where to clone to
- $branch : string = ''
-
Optional the branch to check out
Return values
bool —True if workflow was successful
workflowUpdate()
Workflow update a branch
public
workflowUpdate(Output $output, string $componentDir, string $branch, string $source) : bool
Intended for release preparation.
Run fetch Create local source branch from remote if missing. Update the source branch from remote source. Create branch from local source branch if missing. Update the target branch from remote. Update the target branch from local source.
Return false if missing and source branch also missing.
Do nothing and return success if already checked out Return false if neither local nor remote branch found
Parameters
- $output : Output
-
The output utility
- $componentDir : string
-
The Component checkout dir
- $branch : string
-
The branch to check out
- $source : string
-
The source branch
Return values
bool —True if workflow was successful
exec()
Run a system call and capture output.
protected
exec(string $call) : SystemCallResult
Parameters
- $call : string
-
The system call to execute.
Return values
SystemCallResult —The command output.
execInDirectory()
Run a system call in a given dir and capture output.
protected
execInDirectory(string $call, string $targetDir) : SystemCallResult
Parameters
- $call : string
-
The system call to execute.
- $targetDir : string
-
Run the command in the provided target path.
Return values
SystemCallResult —The command output.
system()
Run a system call.
protected
system(string $call) : string
Parameters
- $call : string
-
The system call to execute.
Return values
string —The command output.
systemInDirectory()
Run a system call.
protected
systemInDirectory(string $call, string $target_dir) : string
Parameters
- $call : string
-
The system call to execute.
- $target_dir : string
-
Run the command in the provided target path.
Return values
string —The command output.