Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getChanges method #42

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ class GitRepository implements IGit

/** @var string|NULL @internal */
protected $cwd;

// Constants for file status in git
// reference: https://git-scm.com/docs/git-status/
const GIT_FILE_STATUS_MODIFIED = "M";
const GIT_FILE_STATUS_ADDED = "A";
const GIT_FILE_STATUS_DELETED = "D";
const GIT_FILE_STATUS_RENAMED = "R";
const GIT_FILE_STATUS_COPIED = "C";
const GIT_FILE_STATUS_UPDATED_BUT_UNMERGED = "U";


/**
Expand Down Expand Up @@ -394,6 +403,38 @@ public function getLastCommitId()


/**
* What files were changed?
* `git status` + magic
* @return string[]
* @throws GitException
*/
public function getChanges()
{
// Make sure the `git status` gets a refreshed look at the working tree.
$this->begin()
->run('git update-index -q --refresh')
->end();

$output = $this->extractFromCommand('git status --porcelain');

$files = array();

if(empty($output)){
return $files;
}

foreach($output as $line){
$line = trim($line);

$file = explode(" ", $line, 2);
if(count($file) >= 2){
$files[$file[1]] = $file[0];
}
}
return $files;
}

/**
* Exists changes?
* `git status` + magic
* @return bool
Expand Down
6 changes: 6 additions & 0 deletions src/IGit.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ function commit($message, $params = NULL);
* @return bool
*/
function hasChanges();

/**
* What files were changed?
* @return string[] filename => change type
*/
function getChanges();


/**
Expand Down