My dotfiles. Period.

sibling 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/sh
  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. ## cfgz
  25. #
  26. origin_name="origin"
  27. sibling_conf=".gittum-sibling"
  28. ## funz
  29. #
  30. die() {
  31. echo "$@" 1>&2
  32. exit 1
  33. }
  34. usage() {
  35. echo "usage: git $(basename $0) NEWPROJ [TEMPLATE]"
  36. exit 0
  37. }
  38. rewrite_uri_to () {
  39. # create a new remote URI based on current one
  40. git remote -v \
  41. | grep "^$origin_name" \
  42. | grep "(fetch)$" \
  43. | cut -f 2 \
  44. | cut -d" " -f 1 \
  45. | perl -pe "s|[^/]+$|$1|;"
  46. }
  47. ## initz
  48. #
  49. project=$1
  50. test -n "$project" || usage
  51. if [ -f $sibling_conf ];
  52. then
  53. sibling=$(cat $sibling_conf);
  54. else
  55. sibling=$(find -maxdepth 1 -type d -name "[^.]*" | sort | head -1)
  56. fi
  57. if [ -z "$sibling" ];
  58. then
  59. echo "could not find older sibling" 1>&2
  60. exit 1
  61. fi
  62. ## body
  63. #
  64. pushd "$sibling" >/dev/null
  65. new_remote=$(rewrite_uri_to $project)
  66. popd >/dev/null
  67. echo \'git clone $new_remote\'
  68. git clone $new_remote