subtree-subdir-helper.sh 672 B

12345678910111213141516171819202122
  1. #!/bin/bash
  2. set -e
  3. if [ "${1}" = "" ] || [ "${2}" = "" ] || [ "${3}" = "" ] || [ "${4}" = "" ] || [ "${5}" = "" ]; then
  4. echo "Usage: <path> <repo url> <branch> <subdir> <action>"
  5. exit
  6. fi
  7. path="${1}"
  8. repo="${2}"
  9. branch="${3}"
  10. subdir="${4}"
  11. action="${5}"
  12. prev="$(git branch --show-current)"
  13. temp="$(rev <<< "${repo%/}" | cut -d/ -f1,2 | rev | tr / -)-${branch}"
  14. fetch="_fetch-${temp}"
  15. split="_split-${temp}-$(tr / - <<< "${subdir}")"
  16. git fetch --no-tags "${repo}" "${branch}:${fetch}"
  17. git checkout "${fetch}"
  18. git subtree split -P "${subdir}" -b "${split}"
  19. git checkout "${prev}"
  20. git subtree "${action}" -P "${path}" "${split}" -m "${action^} ${path} from ${repo}"