Skip to content

Instantly share code, notes, and snippets.

@sunel
Forked from phpfunk/pre-commit
Created December 10, 2015 12:00
Show Gist options
  • Save sunel/6aaa3fe6a8cc4bbdbe89 to your computer and use it in GitHub Desktop.
Save sunel/6aaa3fe6a8cc4bbdbe89 to your computer and use it in GitHub Desktop.

Revisions

  1. @phpfunk phpfunk revised this gist Jun 4, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,5 @@ It will only check files that have one of the extensions listed in the `$extensi

    Things to note:
    * You may have to change the `#!/usr/bin/php` to point to your path for PHP
    * If you don't want to check untracked files, remove the `$untracked` variable.
    * If you don't want to check untracked files, remove the `$untracked` variable.
    * Place `pre-commit` file in your `.git/hooks` folder and `chmod +x .git/hooks/pre-commit`
  2. @phpfunk phpfunk revised this gist Jun 4, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pre-commit
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ foreach ($files as $file) {
    if ($return == 0) {
    continue;
    }
    echo implode("\n", $output), "\n";
    echo implode("\n", $output) . "\n";
    $status = 1;
    }
    }
  3. @phpfunk phpfunk created this gist Jun 4, 2014.
    36 changes: 36 additions & 0 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/usr/bin/php
    <?php

    // Set empty files array
    $files = array();

    // Get untracked files
    // Get modified files
    exec('git ls-files --others --exclude-standard', $untracked);
    exec('git diff --cached --diff-filter=ACMRTUX --name-only', $modified);

    // Merge em
    $files = array_merge($untracked, $modified);

    // Set extensions to check
    $extensions = array('php', 'tmpl');

    // Check their syntax
    foreach ($files as $file) {
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    if (in_array($ext, $extensions)) {

    $output = array();
    exec('php -l ' . escapeshellarg($file), $output, $return);
    if ($return == 0) {
    continue;
    }
    echo implode("\n", $output), "\n";
    $status = 1;
    }
    }

    // Set exit status
    // 0 = good, 1 = fail
    $status = (!isset($status)) ? 0 : 1;
    exit($status);
    7 changes: 7 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    This is a very easy way to check your PHP syntax before each commit. Git will call the script before it runs your commit. If there are syntax errors found, they won't be committed and they will be reported.

    It will only check files that have one of the extensions listed in the `$extensions` array. It's a pretty simple script and comes in handy if you are working locally before you push to a remote for testing.

    Things to note:
    * You may have to change the `#!/usr/bin/php` to point to your path for PHP
    * If you don't want to check untracked files, remove the `$untracked` variable.