Skip to content

Instantly share code, notes, and snippets.

View neilmayhew's full-sized avatar

Neil Mayhew neilmayhew

View GitHub Profile
@neilmayhew
neilmayhew / notify-gh-run-completion.sh
Created September 29, 2026 13:43
Post a notification when a GitHub CI run completes
#!/usr/bin/env bash
set -euo pipefail
shopt -s inherit_errexit
# Post a notification when a GitHub CI run completes
#
# Usage: notify-gh-run-completion URL
#
# Example URL: https://github.com/owner/repo/actions/runs/123456789
@neilmayhew
neilmayhew / git-problem-repro.sh
Created November 13, 2024 19:41
A script to reproduce `git fsck` failures on `.gitmodules` history
#!/usr/bin/env -S bash -euo pipefail
# Reproduce git fsck problem
DIR=$(mktemp -d)
trap 'cd /; rm -rf "$DIR"' EXIT
cd "$DIR"
git init
@neilmayhew
neilmayhew / GracefulTermination.hs
Last active September 29, 2026 14:41
Graceful handling of SIGTERM for Haskell programs
module System.GracefulTermination where
import Control.Concurrent.Async (race)
import Control.Concurrent.MVar
import Data.Functor (void)
import System.Posix.Signals
-- | Ensure that @SIGTERM@ is handled gracefully, because it's how containers are stopped.
--
-- @action@ will receive an 'AsyncCancelled' exception if @SIGTERM@ is received by the process.
@neilmayhew
neilmayhew / ToXML.hs
Created January 13, 2023 19:21
Convert Haskell datatypes to XML using their ToJSON instance
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
import Data.Aeson (ToJSON, Value(..), eitherDecode, toJSON)
import Data.Aeson.Key (toString)
import Data.Aeson.KeyMap (fromList, toAscList)
@neilmayhew
neilmayhew / AOC-2022
Last active September 29, 2026 14:50
Advent of Code 2022
Solutions to aoc-5 and aoc-7 in Haskell
@neilmayhew
neilmayhew / Identity-HowTo.md
Created December 16, 2021 18:26
Configuring git to use a different GitHub/GitLab identity for some projects

Using a different GitHub/GitLab account for some projects

If you're working on a project that requires you to use a separate account on GitHub/GitLab, it can be a bit tricky to set up. Your ssh key identifies you uniquely, so you can't use the same key for two different accounts. However, git doesn't have a simple way for you to configure an ssh key for a repo or a group of repos. You have to do it by creating an alias for the host name and associating a different key with that host name. Here's one way to do it.

Note: Replace all instances of something with the project or client name.

Create a new ssh key

ssh-keygen -f ~/.ssh/id_rsa-something [other-options]
@neilmayhew
neilmayhew / Median2.cs
Last active March 19, 2021 13:53
Median of two sorted lists
// Return the median of a ∪ b ≠ ∅ if a and b are sorted
static double Median2(int[] a, int[] b)
{
int m = a.Length;
int n = b.Length;
int h = (m + n + 1) / 2; // 'Half' - the number elements in the subset
int Search(int lo, int hi) {
if (lo == hi)
return lo; // No other choices left
@neilmayhew
neilmayhew / isPrefix.cc
Created December 4, 2020 20:17
A test harness for the isPrefix function
#include <iostream>
#include <string>
inline int isPrefix(std::string pre, std::string str)
{
return str.compare(0, pre.size(), pre);
}
int main(int argc, char const *argv[])
{
@neilmayhew
neilmayhew / update-oem-vmware.sh
Last active July 22, 2023 21:42
Update a Flatcar installation on VMWare to use the latest OEM content
#!/usr/bin/env bash
# Update a Flatcar installation on VMWare to use the latest OEM content
#
# Copyright 2020, Neil Mayhew <neil@kerith.ca>
# LICENSE: MIT
set -ex
shopt -s extglob nullglob
@neilmayhew
neilmayhew / ordering-semigroup.hs
Created August 7, 2020 03:51
Ordering Semigroup instance
class Eq a => Ord a where
compare :: a -> a -> Ordering
class Semigroup a where
(<>) :: a -> a -> a
instance Semigroup Ordering where
(<>) = undefined
func :: (Ord u, Ord v) => (u, u) -> (v, v) -> Ordering