tangled
alpha
login
or
join now
dunkirk.sh
/
dots
3
fork
atom
Kieran's opinionated (and probably slightly dumb) nix config
3
fork
atom
overview
issues
pulls
pipelines
bug: fix knot sync
dunkirk.sh
2 months ago
fbadff7c
735743c4
verified
This commit was signed with the committer's
known signature
.
dunkirk.sh
SSH Key Fingerprint:
SHA256:DqcG0RXYExE26KiWo3VxJnsxswN1QNfTBvB+bdSpk80=
+64
-56
1 changed file
expand all
collapse all
unified
split
modules
nixos
services
knot-sync.nix
+64
-56
modules/nixos/services/knot-sync.nix
···
61
61
log() { echo "$(date +'%Y-%m-%d %H:%M:%S'): $1" >> "$LOG_FILE"; }
62
62
63
63
# Create the post-receive hook template
64
64
-
cat <<'EOF' > /tmp/post-receive.template
65
65
-
#!${pkgs.bash}/bin/bash
66
66
-
# post-receive hook to sync to GitHub - AUTOGENERATED
64
64
+
# Use plain 'git' - it's in PATH via environment.systemPackages
65
65
+
cat <<'HOOKEOF' > /tmp/post-receive.template
66
66
+
#!/usr/bin/env bash
67
67
+
# post-receive hook to sync to GitHub - AUTOGENERATED
67
68
68
68
-
# Load environment variables from secrets file
69
69
-
if [ -f "${cfg.secretsFile}" ]; then
70
70
-
source "${cfg.secretsFile}"
71
71
-
fi
69
69
+
# Load environment variables from secrets file
70
70
+
if [ -f "${cfg.secretsFile}" ]; then
71
71
+
source "${cfg.secretsFile}"
72
72
+
fi
72
73
73
73
-
# Variables
74
74
-
GITHUB_USERNAME="${cfg.githubUsername}"
75
75
-
LOG_FILE="${cfg.logFile}"
76
76
-
REPO_NAME=$(basename $(pwd))
74
74
+
# Variables
75
75
+
GITHUB_USERNAME="${cfg.githubUsername}"
76
76
+
LOG_FILE="${cfg.logFile}"
77
77
+
REPO_NAME=$(basename $(pwd))
77
78
78
78
-
# Log function
79
79
-
log() { echo "$(date +'%Y-%m-%d %H:%M:%S'): $1" >> "''${LOG_FILE}"; }
79
79
+
# Log function
80
80
+
log() { echo "$(date +'%Y-%m-%d %H:%M:%S'): $1" >> "''${LOG_FILE}"; }
80
81
81
81
-
# Check for nosync marker
82
82
-
if [ -f "$(pwd)/.nosync" ]; then
83
83
-
log "Skipping sync for $REPO_NAME (nosync marker present)"
84
84
-
exit 0
85
85
-
fi
82
82
+
# Check for nosync marker
83
83
+
if [ -f "$(pwd)/.nosync" ]; then
84
84
+
log "Skipping sync for $REPO_NAME (nosync marker present)"
85
85
+
exit 0
86
86
+
fi
86
87
87
87
-
# Function to sync to GitHub
88
88
-
sync_to_github() {
89
89
-
log "Syncing $REPO_NAME to GitHub"
90
90
-
expected_url="https://''${GITHUB_USERNAME}:''${GITHUB_TOKEN}@github.com/''${GITHUB_USERNAME}/''${REPO_NAME}.git"
91
91
-
current_url=$(${pkgs.git}/bin/git remote get-url origin 2>/dev/null || echo "")
88
88
+
# Function to sync to GitHub
89
89
+
sync_to_github() {
90
90
+
log "Syncing $REPO_NAME to GitHub"
91
91
+
expected_url="https://''${GITHUB_USERNAME}:''${GITHUB_TOKEN}@github.com/''${GITHUB_USERNAME}/''${REPO_NAME}.git"
92
92
+
current_url=$(git remote get-url origin 2>/dev/null || echo "")
92
93
93
93
-
if [ -z "$current_url" ]; then
94
94
-
log "Adding origin remote"
95
95
-
${pkgs.git}/bin/git remote add origin "$expected_url"
96
96
-
elif [ "$current_url" != "$expected_url" ]; then
97
97
-
log "Updating origin remote URL"
98
98
-
${pkgs.git}/bin/git remote set-url origin "$expected_url"
99
99
-
fi
94
94
+
if [ -z "$current_url" ]; then
95
95
+
log "Adding origin remote"
96
96
+
git remote add origin "$expected_url"
97
97
+
elif [ "$current_url" != "$expected_url" ]; then
98
98
+
log "Updating origin remote URL"
99
99
+
git remote set-url origin "$expected_url"
100
100
+
fi
100
101
101
101
-
# Mirror push everything (refs, tags, branches)
102
102
-
if ${pkgs.git}/bin/git push --mirror origin 2>&1 | tee -a "''${LOG_FILE}"; then
103
103
-
log "Sync succeeded for $REPO_NAME"
104
104
-
return 0
105
105
-
else
106
106
-
log "Sync failed for $REPO_NAME"
107
107
-
return 1
108
108
-
fi
109
109
-
}
102
102
+
# Mirror push everything (refs, tags, branches)
103
103
+
if git push --mirror origin 2>&1 | tee -a "''${LOG_FILE}"; then
104
104
+
log "Sync succeeded for $REPO_NAME"
105
105
+
return 0
106
106
+
else
107
107
+
log "Sync failed for $REPO_NAME"
108
108
+
return 1
109
109
+
fi
110
110
+
}
110
111
111
111
-
# Main
112
112
-
while read oldrev newrev refname; do
113
113
-
log "Received push for ref '$refname' (old revision: $oldrev, new revision: $newrev)"
114
114
-
sync_to_github
115
115
-
done
116
116
-
EOF
112
112
+
# Main
113
113
+
while read oldrev newrev refname; do
114
114
+
log "Received push for ref '$refname' (old revision: $oldrev, new revision: $newrev)"
115
115
+
sync_to_github
116
116
+
done
117
117
+
HOOKEOF
117
118
118
119
HOOK_TEMPLATE="/tmp/post-receive.template"
119
120
120
120
-
# Create the post-receive hook
121
121
+
# Create or update the post-receive hook
121
122
create_hook() {
122
123
local new_repo_path="$1"
123
124
local hook_path="$new_repo_path/hooks/post-receive.d/forward"
···
129
130
return 0
130
131
fi
131
132
132
132
-
if [ -d "$new_repo_path" ] && [ ! -f "$hook_path" ]; then
133
133
+
if [ -d "$new_repo_path" ]; then
133
134
# Check that it's a git repository, specifically a bare repo
134
135
if [ -f "$new_repo_path/config" ]; then
135
136
# Create hooks directory if it doesn't exist
136
137
mkdir -p "$(dirname "$hook_path")"
137
137
-
# Create hook from the template file, substituting variables.
138
138
+
139
139
+
# Check if hook is new or needs updating
140
140
+
local is_new=false
141
141
+
[ ! -f "$hook_path" ] && is_new=true
142
142
+
143
143
+
# Always write the hook (they're autogenerated)
138
144
if cat "$HOOK_TEMPLATE" > "$hook_path" && chmod +x "$hook_path"; then
139
139
-
log "Created hook for $new_repo_path"
140
140
-
# Check if repo has any commits before pushing
141
141
-
if (cd "$new_repo_path" && ${pkgs.git}/bin/git rev-parse HEAD >/dev/null 2>&1); then
142
142
-
# Auto push by simulating a post-receive hook trigger
143
143
-
log "Triggering initial push for $new_repo_path"
144
144
-
(cd "$new_repo_path" && \
145
145
-
echo "0000000000000000000000000000000000000000 $(${pkgs.git}/bin/git rev-parse HEAD) refs/heads/main" | \
146
146
-
"$hook_path")
145
145
+
if [ "$is_new" = true ]; then
146
146
+
log "Created hook for $new_repo_path"
147
147
+
# Check if repo has any commits before pushing
148
148
+
if (cd "$new_repo_path" && git rev-parse HEAD >/dev/null 2>&1); then
149
149
+
# Auto push by simulating a post-receive hook trigger
150
150
+
log "Triggering initial push for $new_repo_path"
151
151
+
(cd "$new_repo_path" && \
152
152
+
echo "0000000000000000000000000000000000000000 $(git rev-parse HEAD) refs/heads/main" | \
153
153
+
"$hook_path")
154
154
+
fi
147
155
fi
148
156
else
149
157
log "Hook creation failed for $new_repo_path"