add-subtree.sh 815 B

12345678910111213141516171819202122232425262728
  1. #!/bin/bash
  2. set -e
  3. if [ "$1" = "" ] || [ "$2" = "" ]; then
  4. echo "Usage 1: git add-subtree <path> <repo url> <branch> [subdir]"
  5. echo "Usage 2: git add-subtree <path> <repo url>/tree/<branch>[/subdir]"
  6. exit
  7. fi
  8. path="${1%/}"
  9. repo="${2%/}"
  10. if [ "$3" = "" ]; then
  11. read repo branch subdir <<< "$(sed -E "s|(https?://[^/]+)/([^/]+)/([^/]+)/tree/([^/]+)/(.*)|\1/\2/\3 \4 \5|" <<< "${repo}")"
  12. else
  13. branch="${3}"
  14. subdir="${4%/}"
  15. fi
  16. if [ "${subdir}" = "" ]; then
  17. subdir="/"
  18. git subtree add -P "${path}" "${repo}" "${branch}" -m "Add ${path} from ${repo}"
  19. else
  20. bash .utils/subtree-subdir-helper.sh "${path}" "${repo}" "${branch}" "${subdir}" add
  21. fi
  22. gitsubtree="${path}/.gitsubtree"
  23. echo "${repo} ${branch} ${subdir}" > "${gitsubtree}"
  24. git add "${gitsubtree}"
  25. git commit --amend --no-edit