sgit-mkblob 932 B

12345678910111213141516171819202122232425262728293031323334
  1. #! /bin/bash
  2. source_file="$1"
  3. objdir="$PWD/.git/objects"
  4. [[ ! -f "$source_file" ]] && echo "Usage: sgit-mkblob FILE
  5. Creates a git blob object from FILE.
  6. Assumes $objdir exists." && exit 1
  7. [[ ! -d "$objdir" ]] && echo "Please create $objdir, for example by running sgit-init." && exit 1
  8. # Every object header contains the object type
  9. # followed by the payload byte length terminated
  10. # with a null byte (\0).
  11. function header() {
  12. printf "blob $(wc -c $PWD/$source_file | awk '{print $1}')\0"
  13. }
  14. # A blob contains header + payload.
  15. function blob() {
  16. cat <(header) "$source_file"
  17. }
  18. # Determine where to store the object, based on its
  19. # sha1 hash, and create the necessary directory.
  20. sha=$(blob | sha1sum)
  21. dest_dir="$objdir"/$(echo $sha | awk '{print substr($1,1,2)}')
  22. mkdir -p "$dest_dir"
  23. dest_file=$(echo $sha | awk '{print substr($1,3)}')
  24. # Compress the object with zlib.
  25. blob | openssl zlib > "$dest_dir"/"$dest_file"