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
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.