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