Skip to content

Commit 3fee6b0

Browse files
committed
git-safe-sync package
1 parent 3c76030 commit 3fee6b0

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

flake.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242
};
4343
});
4444

45+
packages = eachSystem (
46+
system:
47+
let
48+
pkgs = import nixpkgs { inherit system; };
49+
in
50+
{
51+
git-safe-sync = pkgs.callPackage ./packages/git-safe-sync.nix { };
52+
}
53+
);
54+
4555
devShells = eachSystem (
4656
system:
4757
let

packages/git-safe-sync.nix

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
writeShellApplication,
3+
git,
4+
coreutils,
5+
}:
6+
7+
writeShellApplication {
8+
name = "git-safe-sync";
9+
runtimeInputs = [
10+
git
11+
coreutils
12+
];
13+
text = ''
14+
set -euo pipefail
15+
16+
if [ "$#" -lt 2 ]; then
17+
echo "Usage: git-safe-sync <repo_url> <target_dir> [clone flags...]" >&2
18+
exit 1
19+
fi
20+
21+
REPO_URL="$1"
22+
TARGET_DIR="$2"
23+
shift 2
24+
25+
if [ ! -d "$TARGET_DIR" ]; then
26+
# Pass any extra flags (like --depth 1) to git clone
27+
git clone "$@" "$REPO_URL" "$TARGET_DIR"
28+
cd "$TARGET_DIR"
29+
if ! git verify-commit HEAD; then
30+
echo "Initial clone failed signature verification. Deleting unsafe clone." >&2
31+
cd /
32+
rm -rf "$TARGET_DIR"
33+
exit 1
34+
fi
35+
else
36+
cd "$TARGET_DIR"
37+
git fetch origin
38+
BRANCH=$(git rev-parse --abbrev-ref HEAD)
39+
git merge --ff-only --verify-signatures origin/"$BRANCH"
40+
fi
41+
'';
42+
}

0 commit comments

Comments
 (0)