sgit-commit 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #! /bin/bash
  2. [[ $# -lt 1 || $# -gt 3 ]] && echo "Usage: $0 TREE [PARENT] [MESSAGE]
  3. Creates a commit object from tree object with sha1 TREE.
  4. PARENT is an optional earlier commit.
  5. MESSAGE is an optional description of the commit."
  6. objdir="$PWD/.git/objects"
  7. [[ ! -d "$objdir" ]] && echo "Please create $objdir, for example by running sgit-init." && exit 1
  8. name="Tim Rice"
  9. email="t.rice@ms.unimelb.edu.au"
  10. seconds=$(date +"%s")
  11. tz=$(date +"%z")
  12. tree="$1"
  13. parent=""
  14. [[ $# -eq 2 ]] && parent=$2
  15. msg=""
  16. [[ $# -eq 3 ]] && msg=$3
  17. tree_dir="$objdir"/$(echo $tree | awk '{print substr($1,1,2)}')
  18. tree_file=$(echo $tree | awk '{print substr($1,3)}')
  19. [[ ! -f "$tree_dir"/"$tree_file" ]] && echo "No object associated with $tree." && exit 1
  20. if [[ -n "$parent" ]]; then
  21. parent_dir="$objdir"/$(echo $parent | awk '{print substr($1,1,2)}')
  22. parent_file=$(echo $parent | awk '{print substr($1,3)}')
  23. [[ ! -f "$parent_dir"/"$parent_file" ]] && echo "No object associated with $parent." && exit 1
  24. fi
  25. function payload() {
  26. echo tree $tree
  27. if [[ -n "$parent" ]]; then
  28. echo parent $parent
  29. fi
  30. echo "author $name <$email> $seconds $tz"
  31. echo "committer $name <$email> $seconds $tz"
  32. echo; echo $msg
  33. }
  34. # Every object header contains the object type
  35. # followed by the payload byte length terminated
  36. # with a null byte (\0).
  37. function header() {
  38. printf "commit $(payload | wc -c)\0"
  39. }
  40. # A commit contains header + payload.
  41. function commit() {
  42. cat <(header) <(payload)
  43. }
  44. # Determine where to store the object, based on its
  45. # sha1 hash, and create the necessary directory.
  46. sha=$(commit | sha1sum)
  47. dest_dir="$objdir"/$(echo $sha | awk '{print substr($1,1,2)}')
  48. mkdir -p "$dest_dir"
  49. dest_file=$(echo $sha | awk '{print substr($1,3)}')
  50. # Compress the object with zlib.
  51. commit | openssl zlib > "$dest_dir"/"$dest_file"