Difference between revisions of "Git"

From Organic Design wiki
(Webhooks)
(Github Webhooks: code with validation)
Line 32: Line 32:
 
*[http://tumblr.intranation.com/post/766290565/how-set-up-your-own-private-git-server-linux How to set up your own private Git server on Linux]. Contains a comparison between Gitolite and Gitosis. This author, having tried both, has a preference for Gitolite.
 
*[http://tumblr.intranation.com/post/766290565/how-set-up-your-own-private-git-server-linux How to set up your own private Git server on Linux]. Contains a comparison between Gitolite and Gitosis. This author, having tried both, has a preference for Gitolite.
  
== Github WebHooks ==
+
== Github Webhooks ==
Github offers notifications via [https://developer.github.com/webhooks/ WebHooks] so that services can respond dynamically to events occurring on their repositories. We use this to have some clones on the server automatically update whenever a ''push'' occurs. Here's an example PHP script that responds to Github notifications (they must be configured as form data not JSON format for this script to work).
+
Github offers notifications via [https://developer.github.com/webhooks/ Webhooks] so that services can respond dynamically to events occurring on their repositories. We use this to have some clones on the server automatically update whenever a ''push'' occurs. Here's an example PHP script that responds to Github notifications that are in JSON format (the default) with the optional ''secret'' used to validate the request. Note that the web-server must have permission to execute ''git pull'' on the repo in question.
{{code|<php><?php
+
{{code|<php>if( array_key_exists( 'HTTP_X_HUB_SIGNATURE', $_SERVER ) ) {
if(array_key_exists('HTTP_X_HUB_SIGNATURE', $_SERVER)) {
+
        $sig = $_SERVER['HTTP_X_HUB_SIGNATURE'];
$repo = json_decode($_POST['payload'])->repository->name;
+
        $body = file_get_contents( 'php://input' );
if( $repo === 'extensions' ) $repo = '/var/www/new-extensions';
+
        $hmac =  hash_hmac( 'sha1', $body, '*********' );
elseif( $repo === 'tools' ) $repo = '/var/www/tools';
+
        if( $sig === "sha1=$hmac" ) {
else $repo = false;
+
                $repo = json_decode( $body )->repository->name;
if( $repo ) exec( "cd $repo && sudo git pull" );
+
                if( $repo === 'extensions' ) $repo = '/var/www/new-extensions';
 +
                elseif( $repo === 'tools' ) $repo = '/var/www/tools';
 +
                else $repo = false;
 +
                if( $repo ) exec( "cd $repo && git pull" );
 +
        }
 
}</php>}}
 
}</php>}}
  

Revision as of 18:53, 11 May 2015

Broom icon.svg The content of this article requires cleaning up to meet OD's quality standards. Check the wiki best practices for guidelines on improving article and categorisation quality.

Git is a distributed revision control / software code management project created by Linus Torvalds, initially for the Linux kernel development.

Git's design was inspired by BitKeeper and Monotone. Git was originally designed only as a low-level engine that others could use to write front ends such as Cogito or StGIT. However, the core Git project has since become a complete revision control system that is usable directly. Several high-profile software projects now use Git for revision control, most notably the Linux kernel, X.org Server, One Laptop per Child (OLPC) core development, and the Ruby on Rails web framework.

Git differs from systems such as CVS, or SVN in that the database is maintained beside the working filesystem on peers. Each peer is easily sync'ed to any other by using push/pull or fetch. In subversion you use three main directory structures;

  • trunk
  • branch
  • tag

In git the trunk is equivalent to HEAD, and is a sha1sum to the latest commit. Branches are used to fork development from the HEAD if for example bug fixing is required. A tag in git is just a named sha1sum commit which is effectilvely a static reference to a particular snapshot of code.

If a repository is cloned, git tracks the master which is the latest commit on the remote repository, as well as the origin/master which is the last known commit from the source sourced repository. This is updated if your remote changes are pushed back to the repository you cloned from.

Using Git to update Wikimedia extensions

git clone https://gerrit.wikimedia.org/r/p/mediawiki/extensions/<EXT>.git

This clones the entire repo including all revisions to your local system. To change the local directory structure to a specific revision, use checkout with the required commit hash, e.g.

git checkout 6854b712f200a833220c156a2499f502317c20c1

Reverting a working copy

<bash>git reset --hard <branch/tag/rev></bash>

Updating just a single file

<bash>git fetch

git checkout origin/master -- path/to/file</bash>

Links to Setting up Git Servers

Github Webhooks

Github offers notifications via Webhooks so that services can respond dynamically to events occurring on their repositories. We use this to have some clones on the server automatically update whenever a push occurs. Here's an example PHP script that responds to Github notifications that are in JSON format (the default) with the optional secret used to validate the request. Note that the web-server must have permission to execute git pull on the repo in question.

{{{1}}}

See also