0

I Have this code that should display the current revision on my web-page. I ran it over my linux-server through php index.php. The code worked fine. It the HTML codes and showed the revision. But when i tried to visit my web-page through my web-browser(google-chrome 40). It doesn't show the revision.

<?php
    $revision = substr(shell_exec('git rev-parse origin/master'),0,7);
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Test - shell_exec</title>
    </head>
    <body>
        <footer class="footer">
            <p>Revision <?php echo $revision; ?></p>
        </footer>
    </body>
</html>
KeiKun
  • 35
  • 9
  • http://stackoverflow.com/questions/8562544/executing-git-commands-via-php ? – rjv Feb 06 '15 at 07:56
  • You should add a Git Hook an store the revision in a File which can be transferred to the remote Server. Most Server block the execution of functions like `shell_exec`. – KFO Feb 06 '15 at 08:03
  • @KFO the server im using is already my git-server. the folder where my index.php is already owned by `git:root` so whenever I push it gets updated through `git fetch && git reset --hard origin/master`. – KeiKun Feb 06 '15 at 08:06
  • Then is the `shell_exec` function blocked in `php.ini` for the webserver. – KFO Feb 06 '15 at 08:11
  • @KFO no i run `php index.php` with the same code it showed the revision hash – KeiKun Feb 06 '15 at 08:13
  • @KeiKun yeah, but when you run the script on the console and with a webserver, different `php.ini` files will be used. – KFO Feb 06 '15 at 08:44

2 Answers2

0

Okay I just solved it. it seems when you disable exec, shell_exec also doesn't work.

KeiKun
  • 35
  • 9
0

Maybe a bit of overkill for your problem, but I'm using ANT to build my website before deploying. With this ANT-building it is possible to remove, move, add or change certain files. This way you can put the GIT tag (I always use tags before building/deploy) in a config file. See my other post on configuration files.

I use ANT to do roughly the following:

  1. make GIT create a copy in a separate folder
  2. strip all files that shouldn't go online
  3. make some changes to config files
  4. create a CHANGELOG file with all the commits in neat order, separated by the GIT Tags for better reading.

Then, all is safe to blindly upload the whole thing in one FTP run.

Steps to get your latest tag in a config file:

  • create a config file in e.g. your root folder ("site.default.properties")
  • put a revision attribute in there (SITE_REVISION_NUMBER=xxxx)
  • make ANT work with GIT to get GIT info

This last thing can work like this in ANT build script:

<property name="GIT-src" location="/home/martin/deploy/build"/>
<exec executable="git"
      failonerror="true"
      outputproperty="tag.current"
      dir="${GIT-src}">
      <arg line="describe --tag"/>
</exec>

then you have a property ("tag.current") you can put in a config file and read it from PHP:

<propertyfile
        file="${deploy}/site.default.properties"
        comment="Site properties">
        <entry  key="SITE_REVISION_NUMBER" value="${tag.current}"/>
</propertyfile>
Community
  • 1
  • 1
Pianoman
  • 327
  • 2
  • 10