diff --git a/src/GitRepository.php b/src/GitRepository.php index d6791a5..5841393 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -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"; /** @@ -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 diff --git a/src/IGit.php b/src/IGit.php index 200f096..4dc7ce1 100644 --- a/src/IGit.php +++ b/src/IGit.php @@ -145,6 +145,12 @@ function commit($message, $params = NULL); * @return bool */ function hasChanges(); + + /** + * What files were changed? + * @return string[] filename => change type + */ + function getChanges(); /**