My dotfiles. Period.

sibling 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. #
  3. # Take first subdir (or a subdir specified in a file) and consider
  4. # it a git repo dir. Get its origin, exchange the last bit for
  5. # other project name and clone the other repo
  6. #
  7. # This is useful if you have a local dir where you store repos
  8. # for various projects from a common namespace that differ only
  9. # in last part of the remote URI. Now all you need to do to
  10. # clone new one is "git sibling aproject". The script will
  11. # do the rest for you including sniffing the URI.
  12. #
  13. # The script simply takes the first subdir. In case this is not
  14. # OK for you (e.g. the dir id likely to be empty or contain
  15. # come "non-compliant" repos, you can specify path to the
  16. # other "template" repo in .gittum-sibling file.
  17. #
  18. # Hint: regarding the "template" repo, you can easily create
  19. # a "fake" repo that would sit elsewhere, e.g.:
  20. # $ mkdir .some/hidden/dir
  21. # $ cd .some/hidden/dir
  22. # $ git init
  23. # $ git remote add origin ssh://our_server/projects/FAKE
  24. ## funz
  25. #
  26. die() {
  27. echo "$@" 1>&2
  28. exit 1
  29. }
  30. usage() {
  31. echo "usage: git $(basename "$0") [-v] [-s SIBLING] [-r REMOTE] PROJECT"
  32. exit 0
  33. }
  34. rewrite_uri () {
  35. local remote=$1
  36. local to=$2
  37. # create a new remote URI based on current one
  38. git remote -v \
  39. | grep "^$remote" \
  40. | grep "(fetch)$" \
  41. | cut -f 2 \
  42. | cut -d" " -f 1 \
  43. | perl -pe "s|[^/]+$|$to|;"
  44. }
  45. think() {
  46. $verbose && echo "$@"
  47. }
  48. ## initz
  49. #
  50. sibling_conf=".gittum-sibling"
  51. verbose=false
  52. sibling=$(find -maxdepth 1 -type d -name "[^.]*" | sort | head -1)
  53. test -f $sibling_conf && sibling=$(cat $sibling_conf)
  54. remote_name="origin"
  55. while true; do case $1 in
  56. -r|--remote-name) remote_name=$2; shift 2 ;;
  57. -s|--sibling) sibling=$2; shift 2 ;;
  58. -v|--verbose) verbose=true; shift ;;
  59. -*) usage ;;
  60. "") break ;;
  61. *) project=$1; shift ;;
  62. esac done
  63. test -n "$project" || usage
  64. test -n "$sibling" || die "could not find older sibling"
  65. test -d "$sibling" || die "sibling does not exist: $sibling"
  66. ## body
  67. #
  68. pushd "$sibling" >/dev/null
  69. new_remote=$(rewrite_uri "$remote_name" "$project")
  70. popd >/dev/null
  71. test -n "$new_remote" || die "no such remote at sibling: $new_remote at $sibling"
  72. think \'git clone $new_remote\'
  73. git clone "$new_remote"