Git-Frankenstein - Transplant Patches Between Repos

Tags:

How it started

I keep all files related to my Perl distributions in a separate git repository. The files needed to build a distribution, the author tests and some other scaffolding get copied from that repository when I start a fresh repository. I then customize the copied files. But of course, my author tests, or the Makefile.PL evolve, and most changes never propagate to old distributions.

Of course, from time to time, I copy the changed files from Dist::Template into whatever distribution I'm working on.

How it's going

git-frankenstein.pl automates this process. It transplants patches to Dist::Template onto other repos, when they fit. It basically is

for $patch in (`git log $source`) { if( `git-apply --check` ) { git-apply } }

... except that it also can list the applicable patches, before applying them:

corion#speech-recognition-vosk: git-frankenstein.pl list 626d986cdb25f2c71e7eef277c7c998f5cc87e28 update_file() handles (only) UTF-8 files 033e53e0cd9866ed281be0924bed2a265e3e5295 Remove -T from tests d5de9059421538580edbc604e64432b3211f16c7 Don't warn for every thing in our own distribution and test prerequisites 07ec7b5592e6cd96e8d5da9f4643022a52a940b1 Don't use ->import() for version checking

Also, to satisfy a real need, there also is the auto command, which automatically applies all patches that apply.

corion#speech-recognition-vosk: git-frankenstein.pl auto 626d986cdb25f2c71e7eef277c7c998f5cc87e28 update_file() handles (only) UTF-8 files 033e53e0cd9866ed281be0924bed2a265e3e5295 Remove -T from tests d5de9059421538580edbc604e64432b3211f16c7 Don't warn for every thing in our own distribution and test prerequisites 07ec7b5592e6cd96e8d5da9f4643022a52a940b1 Don't use ->import() for version checking

The code lives on Github.

Disappointed by mobile Browser APIs

Tags:

Again the browser APIs disappoint me, especially their (non)implementation for mobile phones.

To display images from my mobile or to share my phone screen on a random TV (or other computer) I'd like a simple photo projection website. Very much like ShareDrop , except that the images are not saved on the other machine(s) but simply displayed there.

Making a directory full of images accessible to the browser does not work, as the File System Access API is not implemented on mobile browsers.

Screen sharing could work via WebRTC and images stay locally as far as possible. But the WebRTC screen sharing API is not implemented on mobile browsers.

This is disappointing.

Simple Deployments using Git as Transport

Tags:

This is largely an elaboration on my other post on using Git for deployment

I like to write small toy programs as web apps, like the Curl-to-Perl converter or my weather forecast app. My current tool of choice for writing such web apps is Mojolicious, and the local development is quite nice as it comes with a local web server built in.

XXX screenshot of the weather app

The internet

But obviously a web application is no fun if you can't put it online and use it from wherever, or show it around. While I have a server on the internet and it has a webserver, updating my software while it is in development is inconvenient. When it is inconvenient, I don't do it often, so I want to remove that inconvenience as far as possible.

Approaches

Copying files to the target machine

It's easy to copy files using scp or rsync. My uplink is pretty fast nowadays so I can conveniently copy 10MB within a second.

I program in Perl, and I would need to copy the needed Perl modules as well. This fails when I need to use Perl modules with a binary component like a C library. So copying files alone will not work.

Running programs on the target machine

If I'm only copying the data that can be easily created locally, I need a way to run programs on the remote machine. This is possible for example through ssh -c.

But as most of my webapps are writen with Perl as the backend, I need to run at least cpanm --installdeps to install the modules needed. Often I also want to regenerate other files like manifest.json and/or compress assets. Usually, these other jobs are done through a Makefile.

Using Git as transport and runner

I use Git as my version control system. In Git, I usually check in almost everything of interest to a project. Instead of writing a shell script that I run locally which uploads the files and then kicks off a remote build, I (ab)use the Git post-receive hook to work as my program runner and the Git transport mechanism for transferring the data.

Git as transport

Git can download and upload changes from other git Repositories. It can use a variety of transport mechanisms:

  • file copy from/to a local directory
  • file copy via the git protocol
  • file copy via the ssh / scp protocol

The last protocol is of convenient interest to me, as it means I can simply have a Git repository on the webserver machine and git push will upload my local changes to the remote webserver.

Anatomy of the Git post-receive hook

Whenever Git receives a complete set of changes in a repository, it will then kick off the post-receive hook. The post-receive hook is a program intended to be customized by the user to perform tasks whenever the event arrives.

In my case, I use that post-receive hook to perform all tasks that I want to be done on the webserver:

  • check out the latest state of my webapp into a directory
  • install modules needed by my webapp
  • perform other tasks as specified by a Makefile

Setup

Setting up the post-receive hook is fairly simple:

  1. Create a remote directory for the repository

mkdir my-webapp.git

  1. Initialize the directory as Git repository

cd my-webapp.git && git init --bare

  1. Add the post-receive hook

Don't forget to make the file executable.

  1. Add the machine as a remote on your local repository

git remote add demo corion@that.machine.example:my-webapp.git

Deployment

Deployment now looks like

git push demo

The steps of the post-receive hook

The steps performed by the hook in detail are:

Check out the latest state of my webapp into a directory

git "--work-tree=${CHECKOUT_DIR}" "--git-dir=${REPO}" checkout -f

From the Git repository, we checkout the current state into a target directory.

Install modules needed by my webapp

I like to install all modules needed by a webapp into a directory local to that webapp. This means more maintenance, but it also means that changes to one webapp don't break other webapps. For additional safety, I also reset the PERL5LIB environment variable so even if the hook is run manually it won't install or use modules outside of the app-specific directory.

PERL5LIB=${BASE}/${DIST}/lib /home/corion/perl/bin/cpanm --installdeps "${CHECKOUT_DIR}" -l "${CHECKOUT_DIR}/extlib" --notest

Run post-install steps

Some assets of the webapp might need to be (re)compressed. make is a convenient tool to update files based on the date of other files:

cd "${CHECKOUT_DIR}/public" && make deploy

The post-receive hook in its full glory

#!/bin/sh
unset PERL_MB_OPT PERL_MM_OPT PERL_LOCAL_LIB_ROOT PERL5LIB

REPO=$( cd "$GIT_DIR" || exit; pwd)
BASE=$(cd "${REPO}/.." || exit; pwd)
CHECKOUT_DIR=${REPO%.git}
DIST=$(basename "${CHECKOUT_DIR}")

git "--work-tree=${CHECKOUT_DIR}" "--git-dir=${REPO}" checkout -f
PERL5LIB=${BASE}/${DIST}/lib /home/corion/perl/bin/cpanm --installdeps "${CHECKOUT_DIR}"  -l "${CHECKOUT_DIR}/extlib" --notest

cd "${CHECKOUT_DIR}/public" && make deploy

See Also

Git::Hooks - a Perl program for Git hooks

git-init - create or edit your default Git hooks

Other approaches

Carton

A bundler for Perl. This requires you to have the same Perl compiler and compiler flags locally as you have on the remote machine as it compiles all artifacts locally.

move-year - A minute tool to help me

Tags:

Time progresses on its own, which manifests itself in timestamps in filenames increasing. Such files are files from the tax office, bank statements and other stuff I download from such websites. I like to have them moved from the Downloads directory into directories that are in my backup run, and already have a script that reacts to such downloads being completed.

This script previously dumped the files in the root directory for documents of my bank or the tax office via

mv -i ~/Downloads/bank-statement-2022-01-01.pdf ~/Documents/finance/my-bank/
mv -i ~/Downloads/tax-report-01012022.pdf ~/Documents/finance/taxes/

So from time to time, I went through ~/Documents/finance/my-bank/ and moved the files into directories according to the year they were in.

But I realized that a small tool can do that automatically and also create the directories directly. And ideally, I don't need to tell the tool very much at all:

move-year --create -ymd -i ~/Downloads/bank-statement-2022-01-01.pdf ~/Documents/finance/my-bank/
move-year --create -dmy -i ~/Downloads/tax-report-31112021.pdf ~/Documents/finance/taxes/

This way, the files will automatically land in the directories ~/Documents/finance/my-bank/2022 and ~/Documents/finance/taxes/2021 .

The tool is not yet on CPAN, but it lives on Github

Movies I did watch

Tags:

While staying at home this weekend, I caught up with some movies that I was made aware of via JWZ:

Bullet Train

Bullet Train Movie Poster

Amusing action thriller, with acceptable fight scenes. It's somewhat over the top, but that's to be expected for a movie set on a train, in Japan, where each role gets their own intro.

The movie could have been 15 to 30 minutes shorter but still is entertaining enough.

Day Shift

Bullet Train Movie Poster

Story-wise, it's standard vampire hunter fare, but it has some great callbacks to rap songs of the 90's and 2000. Even though Body Count's in the House may have been a bit too much on the nose. I guess 90's rap counts now as oldies.

Jwz lauded the stunts as "the stunt director was a Jackie Chan fan", but while I can't deny that this is true, the (stunt) director also didn't have the budget and patience of Jackie Chan to make the stunts as good as Jackie Chans fights. Obviously, this is a Netflix production, but still entertaining enough.