My dotfiles. Period.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_to () {
  35. # create a new remote URI based on current one
  36. git remote -v \
  37. | grep "^$origin_name" \
  38. | grep "(fetch)$" \
  39. | cut -f 2 \
  40. | cut -d" " -f 1 \
  41. | perl -pe "s|[^/]+$|$1|;"
  42. }
  43. ## initz
  44. #
  45. origin_name="origin"
  46. sibling_conf=".gittum-sibling"
  47. verbose=false
  48. sibling=$(find -maxdepth 1 -type d -name "[^.]*" | sort | head -1)
  49. test -f $sibling_conf && sibling=$(cat $sibling_conf)
  50. remote_name="origin"
  51. while true;
  52. do
  53. case $1 in
  54. -r|--remote-name)
  55. remote_name=$2
  56. shift 2
  57. ;;
  58. -s|--sibling)
  59. sibling=$2
  60. shift 2
  61. ;;
  62. "")
  63. break
  64. ;;
  65. *)
  66. project=$1
  67. shift
  68. ;;
  69. esac
  70. done
  71. test -n "$project" || usage
  72. test -n "$sibling" || die "could not find older sibling"
  73. ## body
  74. #
  75. pushd "$sibling" >/dev/null
  76. new_remote=$(rewrite_uri_to $project)
  77. popd >/dev/null
  78. echo \'git clone $new_remote\'
  79. git clone $new_remote