Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

GitHub Is Pretty Magical

Warning: Weak browsers (aka Internet Explorer) may not be able to render this blog entry in its glory. Use a real browser!

If you browse on over to the Code and Applications section of this site, you will notice that it takes a little time to load. That’s because that entire page is being dynamically generated by your browser via the GitHub (JSON) APIs. What you see on that page is a live, up to the minute, list of every public project in my GitHub repository.

And here’s the JavaScript code that does it:

But that’s not really the magical part. Here’s the code that inserted the code above into this page:

<pre class="githubfile" file="githubprojects.js" repository="GithubProjects" user="koush"></pre>

The first bit of JavaScript code you see above is a live view of the file that is checked into my GitHub repository… and your browser is using the GitHub API to view it, highlight it (using SyntaxHighlighter), and then render it to your screen. Kudos to Github for a fantastic API!

I wrote GithubProjects up today as an exercise in learning jquery, AJAX, and some other Web 2.0 acronyms. Of course, all of this code is available in my GithubProjects repository.

Gitweb Line Highlighting

codehighlight

I made a few more changes today to support line highlighting of gitweb blobs. Like SyntaxHighlighter, the line highlighting is also completely JavaScript driven. When you highlight a line, it updates the fragment portion of the URL in your browser. Then when you send the URL (with the fragment) to someone, they will see the same highlighted code.

Click here for a sample of code highlighting.

Fun exercise in my quest to catch up and learn JavaScript, CSS, et al.

I couldn’t do this by modifying the query string, because that causes a browser reload when changing window.location. I could perhaps have made a “create hyperlink” button that puts the hyperlink with a modified query string into your paste buffer, but that isn’t as easy as just copying the address out of your browser address bar.

Gitweb Support for SyntaxHighlighter

After getting Gitosis set up on my Hackintosh, I set up gitweb as well. But vanilla gitweb is really ugly; it’s almost as bad as sitting at a console typing the git commands manually. So I spent a good day or so trying to tweak gitweb to work with SyntaxHighlighter. For the longest time, SyntaxHighlighter simply would not work on any page at all. After prodding for a while, I finally figured out that it was due to gitweb.cgi returning the content-type as “application/xhtml+xml” instead of “text/html”.

Click here to see a sample gitweb repository with SyntaxHighlighter enabled. Navigate around the projects, and click any of the language specific blob links (.c, .cs, etc) to see the new highlighting.

// This is SyntaxHighlighter, and
// it now works in gitweb!
if (true)
{
Console.WriteLine("Hooray!");
}

Head over to my GitHub repository to get my git fork with the tweaked gitweb and instructions!

P.S. This was my first time hacking at Perl. I feel violated.

P.P.S. The Perl brush is intentionally disabled. It is a little buggy.

Setting up a Gitosis Server on OS X

I currently run a Team Foundation Server as my source control management for my Windows based projects, but I found that nigh unusable when doing anything related to Linux and Mac development; as the only way to check in is to have a shared directory on the Mac/Linux system that Visual Studio maps to a project in TFS. Rather clumsy.

Since I started working Android, I slowly became git fanboy. The GUI tools are still unbelievably primitive (as is the case with Linux in general it seems…), but the speed, flexibility, and ease of use of the command line tools and the SCM itself more than make up for that aspect. When I set up a Hackintosh last week, I also purged my computers of all Linux VMs and I forgot to take into account that I might want one running for dedicated SCM and other Linux-ish work. But, I figured, I might as well try installing gitosis and see if it works: gitosis is just a Python script that runs in an SSH session. And, it turns out it does!

These steps already assume you have git and Python installed by way of MacPorts.

As I mentioned before, gitosis is actually run in a restricted SSH session to provide the repository access, and RemoteLogin is Apple’s implementation of an SSH daemon/server. So, the first step is to make sure you have Remote Login and Remote Management enabled on OS X:

RemoteLogin

I prefer to keep my git repository in a case sensitive partition/image. I recommend you do the same, as there may be issues otherwise. But, OS X can’t be installed on a case sensitive partition, so you might need to repartition your hard drive to include a case sensitive partition:

WDC WD3000GLFS-01F8U0 Media

Now, let’s add a “git” account that will own the repository and provide repository access via SSH. This can be a standard user account. You should not run gitosis under your account, because it restricts shell access to the SSH session, limiting it to gitosis-serve. (You should also verify that the git account has remote login access in the Sharing section, although it should available by default.)

Accounts-2

Now, let’s set up gitosis. The steps are very similar to the ones found at the site I referenced. Let’s drop to a Terminal. If you do not have an SSH key yet, generate one now with ssh-keygen.

ssh-keygen

Download and install gitosis:

mkdir ~/src
cd ~/src
git clone git://eagain.net/gitosis.git
cd gitosis
sudo python setup.py install

By default, gitosis creates repositories in the ~/repositories directory of the “git” user account. And OS X by default places user HOME directories on the case-insensitive partition. Let’s create the repository directory on the case sensitive partition, and create a ~/repositories symlink to it:

sudo mkdir /Volumes/PartitionName/repositories
sudo ln -s /Volumes/PartitionName/repositories /Users/git/repositories
sudo chown git /Volumes/PartitionName/repositories
sudo chown git /Users/git/repositories

Next we need to make sure the git user has the proper directories in its PATH variable (or you may get a gitosis-serve not found error). We add them by modifying the ~/.bashrc file located at /Users/git/.bashrc:

vi /Users/git/.bashrc

Insert the following into the file:

PATH=/opt/local/bin:/usr/local/bin:$PATH

And make the owner “git”:

sudo chown git /Users/git/.bashrc

The last step is to initialize gitosis and give yourself access to it by adding your public key:

sudo -H -u git gitosis-init < ~/.ssh/id_rsa.pub

This should give you a working gitosis installation! Administration of the git repository is done by checking out the gitosis-admin repository from git, making changes to the config, and checking back in. That is incestuously cool! So, if you can check out the gitosis-admin directory, that means everything is working great:

git clone git@your-host-name:gitosis-admin.git

 

Hopefully that worked. If so, you can begin editing the gitosis.conf file to create more repositories and add users. More information about how to set up repositories and access can be found here.