logo

shit

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

common.sh (1418B)


  1. # TODO: Find git dir; global options
  2. # TODO: LIBEXECDIR or something
  3. GIT_DIR="${GIT_DIR:-.git}"
  4. INDEX_VERSION=2
  5. gitsort() (
  6. # This will still often be wrong
  7. LANG=C sort
  8. )
  9. # Is it hacky? Hell yes. Is it POSIX? HELL YES.
  10. write_hex() {
  11. hex="$1"
  12. while [ -n "$hex" ]
  13. do
  14. cur=$(printf "%s" "$hex" | cut -c1-2)
  15. next=$(printf "%s" "$hex" | cut -c3-)
  16. printf "\\x$(printf "%s" "$cur")"
  17. hex="$next"
  18. done
  19. }
  20. # Prints an integer to stdout in binary, big-endian
  21. write_int32() (
  22. n="$1"
  23. hex=$(printf "%08X" "$n")
  24. write_hex "$hex"
  25. )
  26. write_int16() (
  27. n="$1"
  28. hex=$(printf "%04X" "$n")
  29. write_hex "$hex"
  30. )
  31. read_text() (
  32. path="$1"
  33. offs="$2"
  34. len="$3"
  35. for oct in $(od -An -txC -N"$len" -j"$offs" "$index")
  36. do
  37. printf "\x$oct"
  38. done
  39. )
  40. read_int16() (
  41. path="$1"
  42. offs="$2"
  43. i16=$(od -An -tdS -j"$offs" -N2 "$path" | tr -d ' ')
  44. i16=$((((i16>>8)&0xff) | ((i16<<8)&0xff00)))
  45. echo "$i16"
  46. )
  47. read_int32() (
  48. path="$1"
  49. offs="$2"
  50. i32=$(od -An -tdI -j"$offs" -N4 "$path" | tr -d ' ')
  51. i32=$((((i32>>24)&0xff) |
  52. ((i32<<8)&0xff0000) |
  53. ((i32>>8)&0xff00) |
  54. ((i32<<24)&0xff000000)))
  55. echo "$i32"
  56. )
  57. read_hex() (
  58. path="$1"
  59. offs="$2"
  60. len="$3"
  61. od -An -txC -N"$len" -j"$offs" "$path" | tr -d ' \n'
  62. )
  63. normalize_path() (
  64. path="$1"
  65. path="${path#./}"
  66. # TODO: Remove the leading / if fully qualified
  67. if [ "${path#.git}" != "$path" ]
  68. then
  69. printf '%s' 'Invalid path %s\n' "$path"
  70. exit 1
  71. fi
  72. printf "%s" "$path"
  73. )