Skip to content

Instantly share code, notes, and snippets.

@EkriirkE
Created February 12, 2026 16:24
Show Gist options
  • Select an option

  • Save EkriirkE/1fb96c2f8d144e205682338d3f187fed to your computer and use it in GitHub Desktop.

Select an option

Save EkriirkE/1fb96c2f8d144e205682338d3f187fed to your computer and use it in GitHub Desktop.
Preserve file ownership and timestamps of entire working folder with git commits
#EkriirkE stats
IGNORE=("." ".stats")
if [ -f .statsignore ]; then
while read -r l; do
[[ ! -z "$l" ]] && IGNORE+=("$l")
done < .statsignore
fi
if [ -f .gitignore ]; then
while read -r l; do
[[ ! -z "$l" ]] && IGNORE+=("$l")
done < .gitignore
fi
ARGS=()
for i in "${IGNORE[@]}"; do
if [[ "$i" =~ "/" ]]; then
ARGS+=(-not -path "./$i")
else
ARGS+=(-not -name "$i")
fi
done
echo "#EkriirkE stats" >.stats
find -not -path "*/.git*" "${ARGS[@]}" -printf 'F="%p"\n' -printf 'chown -h %U:%G "$F"\n' \( \! -type l -printf 'chmod %#m "$F"\n' \) -printf 'touch -hcmd "%TY-%Tm-%Td %TH:%TM:%TS %TZ" "$F"\n' >>.stats
echo 'touch -r $(ls -t | head -1) ".stats"'>>.stats
touch -r $(ls -t | head -1) ".stats"
git add .stats
@EkriirkE
Copy link
Author

EkriirkE commented Feb 12, 2026

v2 based on actual commits, not whole working directory

ABOUT="#EkriirkE stats"

#Ensure reversal script is executed when pulled
cmd='[[ -f ".stats" ]] && sh .stats && rm .stats'
for F in ".git/hooks/post-merge" ".git/hooks/post-receive"; do
	grep -qxF "$cmd" "$F" || echo -e "$ABOUT\n$cmd\n">>"$F" && chmod +x "$F"
done

#Build change stats
echo "$ABOUT">.stats
git diff --cached --name-only --diff-filter=ACMRTX | while read F; do
	stat "$F" -c 'F="%n";	chown -h %U:%G "$F";chmod %a "$F";touch -hcmd "%y" "$F"'>>.stats
done

#Commit stats
git add .stats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment