git subtree

Who does not know this common situation: We have a new Idea to extend our existing Project old. So we create a subdirectory newIdea in the corresponding repository. It turns out that the idea was so good that it would also be useful outside of the project old. It would be sensible to create a new repository new which should only contain the subdirectory newIdea. In fact, this problem seems to be so common that there is a special git subcommand since 2012 for this purpose (and more complicated cases): git subtree

New repository from subdirectory

old/
├─ newIdea/
│  ├─ lib.rs
├─ main.rs

So inside the directory old we execute the following command:

git subtree split --prefix=newIdea/ --branch=onlyNewIdeaBranch

This creates a new Branch onlyNewIdeaBranch which only contains the contents of newIdea, i.e., a new root directory. So this branch has a newly written history consisting only of commits with influence on files below of newIdea/.

Now we can create the new repository new and pull the newly created branch.Branch pullen.

cd .. ; mkdir new ; cd new
git init
git pull ../alt onlyNewIdeaBranch

Maybe we want to delete the newIdea subdirectory from the old repository. Probably we have to change infrastructure code in the new repository.

old/
├─ main.rs
new/
├─ lib.rs

Move a subdirectory into an existing repository

Possibly we notice that the code would fit better into an existing repository instead of a new one? Perhaps because we are in the process of moving our code into a monorepo? No problem at all!

old/
├─ newIdea/
│  ├─ lib.rs
├─ main.rs
monorepo/
├─ project1/
├─ project2/

We already created the onlyNewIdeaBranch, which we want to move into the subdirectory goodIdea of the monorepo. Again, we can solve it with git subtree:

cd monorepo
git branch withGoodIdeadBranch
git checkout withGoodIdeadBranch
git subtree add --prefix=goodIdea/ ../old onlyNewIdeaBranch

As soon as we ensured that our new Branch withGoodIdeadBranch looks good and we modified the infrastructure code, we can merge it into main.

old/
├─ neueIdee/
│  ├─ lib.rs
├─ main.rs
monorepo/
├─ project1/
├─ project2/
├─ goodIdea/
│  ├─ lib.rs

Raspberry Router

You need to connect something with an ethernet cable to the internet, but there is only Wifi and all you have is a Raspberry PI?

No problem, all you need to do is connecting it to the Wifi, plug the ethernet cable in and tell it to forward all traffic from the one interface to the other, as described in the Arch Linux Wiki.

sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT

Since our Raspberry is now a router, it should also assign IP addresses to the devices connected to it via DHCP, for example with dnsmasq and the following configuration in /etc/dnsmasq.conf:

#disable dns
port=0

dhcp-range=192.168.13.50,192.168.13.150,12h
bind-interfaces
dhcp-option=3,0.0.0.0
dhcp-option=6,1.1.1.1,8.8.8.8

This is also a good opportunity to route all traffic through a VPN, by replacing the wlan0 interface above by the configured VPN interface (e.g. tun0 for OpenVPN or wg0 for WireGuard).

compress-pdf

To compress of a pdf with many high resolution images to a sensible filesize (by downscaling and reencoding the images), one can use ghostscript:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dEmbedAllFonts=true -dSubsetFonts=true -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

The available presets are screen, ebook, printer, prepress and default. More options can be listed with:

gs -sDEVICE=pdfwrite -o /dev/null -c "currentpagedevice { exch ==only ( ) print == } forall"