logo

shit

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

update-index (2345B)


  1. #!/bin/sh -eu
  2. SHIT_PATH=$(dirname "$0")
  3. . $SHIT_PATH/common.sh
  4. write_index_header() (
  5. nentries="$1"
  6. printf 'DIRC'
  7. write_int32 $INDEX_VERSION
  8. write_int32 $nentries
  9. # TODO: Extensions would go here if we cared
  10. )
  11. write_index_file() (
  12. path="$(normalize_path "$1")"
  13. stat=$(stat -c "%W %Y %d %i %u %g %s" "$path") # XXX: GNUism
  14. ctime=$(printf "%s" "$stat" | cut -d' ' -f1)
  15. mtime=$(printf "%s" "$stat" | cut -d' ' -f2)
  16. dev=$(printf "%s" "$stat" | cut -d' ' -f3)
  17. inode=$(printf "%s" "$stat" | cut -d' ' -f4)
  18. uid=$(printf "%s" "$stat" | cut -d' ' -f5)
  19. gid=$(printf "%s" "$stat" | cut -d' ' -f6)
  20. size=$(printf "%s" "$stat" | cut -d' ' -f7)
  21. write_int32 "$ctime"
  22. write_int32 0 # nanoseconds
  23. write_int32 "$mtime"
  24. write_int32 0 # nanoseconds
  25. write_int32 "$dev"
  26. write_int32 "$inode"
  27. # object type & mode
  28. if [ -x "$path" ]
  29. then
  30. write_int32 $((0x8000 | 0755))
  31. else
  32. write_int32 $((0x8000 | 0644))
  33. fi
  34. write_int32 "$uid"
  35. write_int32 "$gid"
  36. write_int32 "$size"
  37. sha=$("$SHIT_PATH"/hash-object -w "$path")
  38. write_hex "$sha"
  39. # XXX: If file name length is >0xFFF this is wrong
  40. write_int16 "${#path}"
  41. printf '%s\0' "$path"
  42. padding=$((${#path} + 1 + 62))
  43. padding=$((padding % 8))
  44. padding=$(((8 - padding) % 8))
  45. while [ $padding -gt 0 ]
  46. do
  47. printf '\0'
  48. padding=$((padding-1))
  49. done
  50. )
  51. write_index_link() (
  52. printf '%s' "Symlinks are not implemented\n" >&2
  53. exit 1
  54. )
  55. do_add=0
  56. do_remove=0
  57. force_remove=0
  58. while [ $# -ne 0 ]
  59. do
  60. case "$1" in
  61. --add)
  62. do_add=1
  63. ;;
  64. --remove)
  65. do_remove=1
  66. ;;
  67. --force-remove)
  68. do_remove=1
  69. force_remove=1
  70. ;;
  71. *)
  72. break
  73. ;;
  74. esac
  75. shift
  76. done
  77. # TODO: Update existing index
  78. cleanup_old_index() {
  79. if [ $? -eq 0 ]
  80. then
  81. rm "$GIT_DIR"/index.old
  82. else
  83. # Restore old index on error
  84. mv "$GIT_DIR"/index.old "$GIT_DIR"/index
  85. fi
  86. }
  87. if [ -f "$GIT_DIR"/index ]
  88. then
  89. mv "$GIT_DIR"/index "$GIT_DIR"/index.old
  90. trap cleanup_old_index EXIT
  91. fi
  92. write_index_header "$#" >>"$GIT_DIR"/index
  93. for path in "$@"
  94. do
  95. printf "%s\n" "$path"
  96. done | gitsort | while read -r path
  97. do
  98. if [ -f "$path" ]
  99. then
  100. write_index_file "$path" >>"$GIT_DIR"/index
  101. elif [ -L "$path" ]
  102. then
  103. write_index_link "$path" >>"$GIT_DIR"/index
  104. else
  105. printf "Invalid path for indexing: %s\n" "$path" >&2
  106. exit 1
  107. fi
  108. done
  109. sha=$(sha1sum "$GIT_DIR"/index | cut -d' ' -f1)
  110. write_hex "$sha" >>"$GIT_DIR"/index