From 5b5f4bf3bdca5f261fef94258018310b674bcbac Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Fri, 15 Mar 2019 16:31:16 +0100 Subject: [PATCH 1/8] implement getChanges --- src/GitRepository.php | 34 ++++++++++++++++++++++++++++++++++ src/IGit.php | 6 ++++++ test.php | 8 ++++++++ 3 files changed, 48 insertions(+) create mode 100644 test.php diff --git a/src/GitRepository.php b/src/GitRepository.php index d6791a5..a473c88 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -15,6 +15,14 @@ class GitRepository implements IGit /** @var string|NULL @internal */ protected $cwd; + + // Constants for file status in git + 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"; /** @@ -396,6 +404,32 @@ public function getLastCommitId() /** * Exists changes? * `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(); + foreach($output as $line){ + $line = trim($line); + + $file = explode(" ", $line, 2); + if(count($file) >= 2){ + $files[$file[1]] = $file[0]; + } + } + return $files; + } + + /** + * What files were changed + * `git status` + magic * @return bool * @throws GitException */ diff --git a/src/IGit.php b/src/IGit.php index 200f096..717da3d 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(); /** diff --git a/test.php b/test.php new file mode 100644 index 0000000..978b700 --- /dev/null +++ b/test.php @@ -0,0 +1,8 @@ +getChanges()); \ No newline at end of file From db4f80bcb785bd7e18a4ce1a7c17dcfc47c5f2d6 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Fri, 15 Mar 2019 16:31:28 +0100 Subject: [PATCH 2/8] Update GitRepository.php --- src/GitRepository.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GitRepository.php b/src/GitRepository.php index a473c88..22e893e 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -17,6 +17,7 @@ class GitRepository implements IGit 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"; From 5f889dbd6ed99398412fa9e672cb096d94bca303 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Fri, 15 Mar 2019 16:32:40 +0100 Subject: [PATCH 3/8] fix getChanges if there are no changes --- src/GitRepository.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/GitRepository.php b/src/GitRepository.php index 22e893e..78df0ec 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -416,7 +416,13 @@ public function getChanges() ->end(); $output = $this->extractFromCommand('git status --porcelain'); + $files = array(); + + if(empty($output)){ + return $files; + } + foreach($output as $line){ $line = trim($line); From 452b81c4a9468667163f35a0a63f343fec498804 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Fri, 15 Mar 2019 16:33:29 +0100 Subject: [PATCH 4/8] Delete test.php --- test.php | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 test.php diff --git a/test.php b/test.php deleted file mode 100644 index 978b700..0000000 --- a/test.php +++ /dev/null @@ -1,8 +0,0 @@ -getChanges()); \ No newline at end of file From 5ba6eb80fc00f84b0bbaf8d9e8703eed2a8a9d11 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Fri, 15 Mar 2019 16:41:13 +0100 Subject: [PATCH 5/8] fix comment intendation --- src/GitRepository.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/GitRepository.php b/src/GitRepository.php index 78df0ec..ebafe90 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -434,7 +434,8 @@ public function getChanges() return $files; } - /** + /** + * * What files were changed * `git status` + magic * @return bool From 974d7358b1c173d4d0d8daa36c7f314f83176f62 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Fri, 15 Mar 2019 16:41:46 +0100 Subject: [PATCH 6/8] fix intendation --- src/GitRepository.php | 1 - src/IGit.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/GitRepository.php b/src/GitRepository.php index ebafe90..d9949a6 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -435,7 +435,6 @@ public function getChanges() } /** - * * What files were changed * `git status` + magic * @return bool diff --git a/src/IGit.php b/src/IGit.php index 717da3d..4dc7ce1 100644 --- a/src/IGit.php +++ b/src/IGit.php @@ -146,7 +146,7 @@ function commit($message, $params = NULL); */ function hasChanges(); - /** + /** * What files were changed? * @return string[] filename => change type */ From 7951cf04814153613cdead7bed8ffe6d99fe657a Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Fri, 15 Mar 2019 16:43:48 +0100 Subject: [PATCH 7/8] Update GitRepository.php --- src/GitRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitRepository.php b/src/GitRepository.php index d9949a6..fd646ad 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -435,7 +435,7 @@ public function getChanges() } /** - * What files were changed + * What files were changed? * `git status` + magic * @return bool * @throws GitException From ef90b9b51d70068e32f64bba7cc6e21633102324 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Fri, 15 Mar 2019 16:44:20 +0100 Subject: [PATCH 8/8] fix comments --- src/GitRepository.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GitRepository.php b/src/GitRepository.php index fd646ad..5841393 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -403,7 +403,7 @@ public function getLastCommitId() /** - * Exists changes? + * What files were changed? * `git status` + magic * @return string[] * @throws GitException @@ -435,7 +435,7 @@ public function getChanges() } /** - * What files were changed? + * Exists changes? * `git status` + magic * @return bool * @throws GitException