ngcp: add script and flake.nix

This commit is contained in:
2026-06-08 12:58:51 +02:00
parent 4e76a6904f
commit d2fcb5dee0
3 changed files with 205 additions and 0 deletions
Generated
+61
View File
@@ -0,0 +1,61 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1780749050,
"narHash": "sha256-3av0pIjlOWQ6rDbNOmpUSvbNnJkGORQKKjb4LtCZsIY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "a799d3e3886da994fa307f817a6bc705ae538eeb",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
+29
View File
@@ -0,0 +1,29 @@
{
description = "Nix Git Cherry Pick";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils}:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
in {
packages.default = pkgs.writeShellApplication {
name = "ngcp";
text = ./ngcp.sh;
runtimeInputs = with pkgs; [
git
libnotify
jq
];
};
mainProgram = "ngpcp";
apps.default = {
type = "app";
program = "${self.packages.${system}.default}/bin/ngcp";
};
});
}
Executable
+115
View File
@@ -0,0 +1,115 @@
set -euo pipefail
trap 'echo "Interrupted or failed. Repo may be mid-rebase/cherry-pick." ; exit 1' INT ERR
# waits for an internet connection. It pings both Google DNS and Cloudfare dns in case one of them is down
pings=0
until ping -c1 -W1 1.1.1.1 >/dev/null 2>&1 || \
ping -c1 -W1 8.8.8.8 >/dev/null 2>&1
do
sleep 1
((pings++))
if (( pings > 2 )); then
echo "No internet connection. Retrying in 1 second..."
fi
done
CONFIG="$HOME/.config/nix-git-cherry-picker/config.json"
if [[ ! -f "$CONFIG" ]]; then
echo "Error: Config file not found at $CONFIG"
echo "Please create it with the necessary settings."
exit 1
fi
localBranch=$(jq -r '.localBranch' "$CONFIG")
remoteBranch=$(jq -r '.remoteBranch' "$CONFIG")
nixConfigPath=$(jq -r '.nixConfigPath' "$CONFIG")
automatic=0
# if there is the flag --automatic it will fail when merge conflict and tell you to do it manually.
# if there is the flag --no-rebuild it won't rebuild your system to test the new commits
# if there is the flag --continue-with-rebuild it will rebuild if --no-rebuild was set
for flag in "$@"; do
if [[ "$flag" == "--automatic" ]]; then
automatic=1
elif [[ "$flag" == "--help" ]]; then
echo "Usage: ngcp [mode] [options]"
echo "Mode:"
echo " pick <commit 1> <commit2> <...> Cherry-pick commits for the remote branch."
echo " pull Pulls the changes to the local branch."
echo "Options:"
echo " --automatic Exit with no changes if merge conflict and instructs the user to pull manually. This option if for automation."
exit
fi
done
# checks if connection
if [[ "$1" == "pick" ]]; then
if [[ "$#" -lt "2" ]]; then
echo "Invalid arguments, you must specify commit hashes".
exit
fi
#if doesnt exist, just get the branch from the remote
if ! git -C "$nixConfigPath" checkout "$remoteBranch"; then
git -C "$nixConfigPath" checkout -b "$remoteBranch" "origin/$remoteBranch"
fi
for commit in "${@:2}"; do
if ! [[ $commit =~ "--.*" ]]; then # if not a flag
if git -C "$nixConfigPath" cherry-pick "$commit"; then
echo "Applied $commit successfully"
else
echo "Cherry-pick of $commit failed."
echo "Resolve conflicts and run:"
echo " git cherry-pick --continue"
echo
echo "Type 'y' when done."
while git -C "$nixConfigPath" rev-parse --verify CHERRY_PICK_HEAD >/dev/null 2>&1; do
read -r answer
[[ "$answer" == "y" ]] || continue
done
echo "Cherry-pick for $commit resolved."
fi
fi
done
git -C "$nixConfigPath" push --force-with-lease origin "$remoteBranch"
git -C "$nixConfigPath" checkout "$localBranch"
elif [[ "$1" == "pull" ]]; then
gitStatus=$(git -C "$nixConfigPath" status --porcelain)
if [[ -n "$gitStatus" ]]; then
echo "Git directory is dirty. Please clean it up before running ngcp pull"
echo "$gitStatus"
notify-send "Git directory is dirty." "Please clean it up before running ngcp pull\n $gitStatus"
exit 1
fi
if git -C "$nixConfigPath" pull --rebase ; then
echo "Pull successful"
else
if (( automatic )); then
git -C "$nixConfigPath" rebase --abort || true
echo "git pull failed. Please run ngcp pull manually"
notify-send "Git pull failed." "Please run ngcp pull manually"
exit 1
else
echo "Merge conflict detected during pull."
echo "Resolve conflicts and run:"
echo " git rebase --continue"
echo "Type 'y' when done."
while git -C "$nixConfigPath" rev-parse --verify REBASE_HEAD >/dev/null 2>&1; do
read -r answer
[[ "$answer" == "y" ]] || continue
done
echo "Pull successful"
fi
fi
git -C "$nixConfigPath" push --force-with-lease origin "$localBranch"
else
echo "Invalid argument. Please run ngcp --help"
exit
fi