logo

shit

Unnamed repository; edit this file 'description' to name the repository. git clone https://hacktivis.me/git/mirror/shit.git

hash-object (1344B)


  1. #!/bin/sh -eu
  2. SHIT_PATH=$(dirname "$0")
  3. . $SHIT_PATH/common.sh
  4. header() (
  5. objtype="$1"
  6. case "$objtype" in
  7. blob|tree|commit)
  8. len="$2"
  9. printf '%s %d\u0000' "$objtype" "$len"
  10. ;;
  11. *)
  12. printf 'Unknown object type %s\n' "$1" >&2
  13. exit 1
  14. ;;
  15. esac
  16. )
  17. write_object() (
  18. object_type="$1"
  19. path="$2"
  20. len=$(wc -c "$path" | cut -d' ' -f1)
  21. header "$object_type" "$len"
  22. cat "$path"
  23. )
  24. object_type=blob
  25. write=0
  26. while getopts t:w opt
  27. do
  28. case $opt in
  29. t)
  30. object_type="$OPTARG"
  31. ;;
  32. w)
  33. write=1
  34. ;;
  35. ?)
  36. printf "Usage: %s [-t <type>] [-w] <files...>\n" "$0" >&2
  37. exit 1
  38. ;;
  39. esac
  40. done
  41. shift $((OPTIND-1))
  42. process() {
  43. path="$1"
  44. if [ $write -eq 1 ]
  45. then
  46. sha=$(write_object "$object_type" "$path" | sha1sum | cut -d' ' -f1)
  47. prefix=$(printf "%s" "$sha" | cut -c1-2)
  48. suffix=$(printf "%s" "$sha" | cut -c3-)
  49. mkdir -p "$GIT_DIR"/objects/"$prefix"
  50. if ! [ -e "$GIT_DIR"/objects/"$prefix"/"$suffix" ]
  51. then
  52. write_object "$object_type" "$path" | "$SHIT_PATH"/zlib \
  53. >"$GIT_DIR"/objects/"$prefix"/"$suffix"
  54. fi
  55. else
  56. sha=$(write_object "$object_type" "$path" | sha1sum | cut -d' ' -f1)
  57. fi
  58. printf '%s\n' "$sha"
  59. }
  60. for path in "$@"
  61. do
  62. process "$path"
  63. done
  64. if [ $# -eq 0 ]
  65. then
  66. tee > "$GIT_DIR"/objects/NEW_OBJECT
  67. trap "rm '$GIT_DIR/objects/NEW_OBJECT'" EXIT
  68. process "$GIT_DIR"/objects/NEW_OBJECT
  69. fi