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].
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.
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
- "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
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/magazine/174
- GitHub API scopes: https://developer.github.com/v3/oauth/#scopes
- GitHub API rate limits: https://developer.github.com/v3/search/#rate-limit
- Ry's Git Tutorial: http://rypress.com/tutorials/git
- Pull request for a bug in
update_ref()
: https://github.com/fayland/perl-net-github/pull/58
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs