-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpre-commit
81 lines (65 loc) · 3.64 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
#01VCSHere
echo "🤓📝 Storing files' timestamp, CID/hash; and other files/commit's metadata..."
commit_author="$(git config user.name)"" <""$(git config user.email)"">"
commit_message=$(cat $1)
current_branch=$(git rev-parse --abbrev-ref HEAD) #from https://dev.to/anibalardid/how-to-check-commit-message-and-branch-name-with-git-hooks-without-any-new-installation-n34
first_remote_name=$(git remote | head -n 1) # Define the first remote's name
first_remote_url=$(git remote | head -n 1 | xargs git remote get-url) # Get the first remote's URL
# https://github.com/typicode/husky/discussions/1171
rocketlaunch_dir=`pwd` #from https://unix.stackexchange.com/a/52919/470623
# Get a list of all staged files
staged_files=$(git diff --cached --name-only)
# Exit if no files are staged
if [ -z "$staged_files" ]; then
echo "No files staged. Exiting pre-commit hook."
exit 0
fi
if [ ! -f ".gitmeta-cid" ]; then touch .gitmeta-cid; fi
previous_commit_cid=$(ipfs add -q --only-hash .gitmeta-cid)
previous_commit_githash=$(git rev-parse HEAD)
echo ".gitmeta-cid version: r4 " > .gitmeta-cid #use ">" instead of ">>" as a way of emptying .gitmeta-cid before writing new commit data
echo "Branch: $current_branch" >> .gitmeta-cid
echo "Previous commit: $previous_commit_cid" >> .gitmeta-cid
echo "Previous commit (classic git hash): $previous_commit_githash" >> .gitmeta-cid
echo "" >> .gitmeta-cid && echo "------------------------------" >> .gitmeta-cid && echo "" >> .gitmeta-cid
echo "Files:" >> .gitmeta-cid
# Hash (IPFS CID) the contents of each staged file, to the pipe
for file in $staged_files; do
if [ -f "$file" ]; then
file_cid=$(ipfs add -q --only-hash "$file")
echo "$file"": ""$file_cid" >> .gitmeta-cid
fi
done
commit_cid=$(ipfs add -q --only-hash .gitmeta-cid)
echo "[01VCS] 🆔Commit ID: $commit_cid"
echo "commit ""$commit_cid" > .gitmeta #use ">" instead of ">>" as a way of emptying .gitmeta before writing new commit data
echo "Branch: ""$current_branch" >> .gitmeta
# If the .eth file exists, sign the commit data with the Ethereum account
if [ -f ".git/hooks/.eth" ]; then
echo "💎 Found .git/hooks/.eth!"
eth_account=$(cat .git/hooks/.eth)
# signature=$(ethereal signature sign --nohash --data="$commit_cid" --signer="$eth_account" --passphrase="your_passphrase_here")
echo "🪄 Signing the commit $commit_cid with your choosen ETH address $eth_account..."
signature=$(ethereal signature sign --offline --nohash --data="$commit_cid" --signer="$eth_account" --passphrase="$(jq -r '.passphrase' $HOME/.ethereal.json)" --config $HOME/.ethereal.json)
enscheck="$(ethereal ens domain get --address=$eth_account)"
if [ "$enscheck" = "Failed to check reverse resolution: not a resolver" ]; then
echo "Signed-off by: $eth_account" >> .gitmeta
else
echo "Signed-off by: $enscheck ($eth_account)" >> .gitmeta
fi
echo "Signature: $signature" >> .gitmeta
echo "🔦 Verifying signature..."
ethereal signature verify --nohash --data="$commit_cid" --signature="$signature" --signer="$eth_account" #from https://github.com/wealdtech/ethereal?tab=readme-ov-file#signature-signer
fi
echo "" >> .gitmeta && echo "------------------------------" >> .gitmeta && echo "" >> .gitmeta
echo ".gitmeta version: r6" >> .gitmeta
echo "Local folder: ""$rocketlaunch_dir" >> .gitmeta
echo "Remote Name: $first_remote_name" >> .gitmeta
echo "Remote URL: $first_remote_url" >> .gitmeta
echo "" >> .gitmeta && echo "------------------------------" >> .gitmeta && echo "" >> .gitmeta
bash .git/hooks/git-meta --store
git add .gitmeta
git add .gitmeta-cid
echo "✅ Done. Meta has been preserved!"
echo "🎉 Your snapshot/commit is done!"