Skip to content

Instantly share code, notes, and snippets.

@marcransome
Last active March 8, 2024 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcransome/046449a4a3efc932e161d2686a3ef3f5 to your computer and use it in GitHub Desktop.
Save marcransome/046449a4a3efc932e161d2686a3ef3f5 to your computer and use it in GitHub Desktop.
git-obj-walk.sh
#!/bin/bash
set +o pipefail
if (( $# != 1 )); then
echo "usage: $0 <ref>" >&2
exit 1
fi
if ! command -v git >/dev/null 2>&1; then
echo "error: git executable not found" >&2
exit 1
fi
ref="$1"
obj=$(git rev-parse "$ref" 2>/dev/null)
if (( $? != 0 )); then
echo "error: unknown revision or not a git repository" >&2
exit 1
fi
while true; do
readarray -d ' ' -t tokens <<< "$(git cat-file -p "$obj" 2>/dev/null | head -1)"
if (( $? != 0 || ${#tokens[@]} < 2 )); then
echo "error: unable to determine details of object: $obj" >&2
exit 1
fi
type=$(git cat-file -t "$obj" 2>/dev/null)
if (( $? != 0 )); then
echo "error: unable to obtain content of object: $obj" >&2
exit 1
fi
node="${node:+ -> }[ $type:${obj:0:6} ]"
echo -n "$node"
case "${tokens[0]}" in
object|tree)
obj="$(tr -d '\n' <<< "${tokens[1]}")"
;;
*)
echo
break
;;
esac
done
@marcransome
Copy link
Author

Example:

$ ./git-obj-walk.sh v1.0.0
[ tag:45de3d ] -> [ commit:45be62 ] -> [ tree:52a266 ]

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