Perl script rummages through Git metadata

Committing Code

Before calling the API to commit a revised README file to a software project on GitHub, you first need to step back and look into Git's inner workings. Under the hood, Git relies on a few simple but powerful data structures, although the git command-line tool completely shields them from regular users. Git represents a file in the repository as a so-called blob. A collection of these blobs in a directory is called a tree, and a tree with a check-in note is a commit (Figure 6). A thorough introduction to Git's guts can be found online [5].

Figure 6: Under Git's hood, commits consist of trees, whereas trees contain blobs and other trees.

Before the script in Listing 6 can commit to the test project mschilli/apitest, I first need to collect a new token with escalated privileges. The token obtained in Listing 1 only let me read the repository data. To gain write access, line 15 in Listing 1 must be replaced by

scopes => ['public_repo'],

and the comment stored in the note parameter needs to be changed. A new call to token-get then stores a new token with advanced privileges in the token file.

Listing 6

readme-upd

 

Blobs, Trees, Commits

The call to the readme-upd script (Listing 6) then generates a new blob of the README file with permissions of 0644 and defines a tree to store the blob. On top of this is the base_tree option in line 40, set to an already existing tree in the repository, which the script extracts from the most recent commit of the master branch. Basing the new tree off this base tree ensures that any other existing files will stay in the newly created version of the repository.

Line 43 then converts the tree into a commit, with a fun date of 11/11/2011, which illustrates that date and author information in Git can be arbitrarily set.

Showing Pointers

Previously, line 19 of the listing retrieved the references to the development branches of the repo. After the new commit is in place, line 54 redirects the head pointer of the project's master branch to it. This can be accomplished without using force (force => 0) because the new commit was derived straight from its ancestor. Figure 7 shows what the automatically injected commit looks like on the GitHub website of the project.

Figure 7: Shortly after the commit was created by script, it is visible on the GitHub website.

When this issue went to press, version 0.71 of the Net::GitHub module still had a bug in its implementation of update_ref(). However, a quick pull request provided a remedy [6]. With a little bit of luck, the module author will have already added the patch in the latest release, which will be on CPAN by the time this article is published.

If you want, you can run one of the query scripts in this article as a daily cronjob to create a timeline that illustrates how hard you have been working on your GitHub projects day in and day out. Or, perhaps the boss would like to be alerted if an employee suddenly experiences a sudden productivity boost.

Infos

  1. "An Alternative Use for GitHub" by Mike Schilli, Linux Magazine, issue 147, pg. 46: http://www.linux-magazine.com/index.php/Issues/2013/147/Perl-CMS-with-GitHub/(tagID)/167
  2. Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/magazine/174
  3. GitHub API scopes: https://developer.github.com/v3/oauth/#scopes
  4. GitHub API rate limits: https://developer.github.com/v3/search/#rate-limit
  5. Ry's Git Tutorial: http://rypress.com/tutorials/git
  6. Pull request for a bug in update_ref(): https://github.com/fayland/perl-net-github/pull/58

The Author

Mike Schilli works as a software engineer with Yahoo! in Sunnyvale, California. He can be contacted at mailto:mschilli@perlmeister.com. Mike's homepage can be found at http://perlmeister.com.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: CMS with GitHub

    With its easy-to-use web interface, GitHub can be put to totally different uses than archiving code. For example, Perlmeister Mike Schilli used GitHub to deploy a content management system for simple websites.

  • GitHub with hub

    The handy hub command-line tool lets you manage your GitHub repository from a terminal window, which can make it easier to automate repetitive tasks.

  • Perl: Travis CI

    A new service on travis-ci.org picks up GitHub projects, runs new code through test suites, and notifies the owners if the build fails. Its API enables Perl scripts to gather historical build data, including who-broke-the-build tabulations.

  • Workspace: GitBook

    Write and publish ebooks with the GitBook software and publishing platform.

  • Perl: Collaborate with GitHub

    GitHub makes it easier for programmers to contribute to open source projects by simplifying and accelerating communications between project maintainers and people willing to contribute.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News