add-subtree.sh 896 B

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