mfadd 907 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env bash
  2. #
  3. # mfadd user[:branch] [copyname]
  4. #
  5. # Add a remote and fetch it. Optionally copy a branch.
  6. #
  7. # Examples:
  8. # mfadd thefork
  9. # mfadd thefork:patch-1
  10. # mfadd thefork:patch-1 the_patch_12345
  11. #
  12. [[ $# > 0 && $# < 3 && $1 != "-h" && $1 != "--help" ]] || { echo "usage: `basename $0` user[:branch] [copyname]" 1>&2 ; exit 1; }
  13. # If a colon or slash is included, split the parts
  14. if [[ $1 =~ ":" || $1 =~ "/" ]]; then
  15. [[ $1 =~ ":" ]] && IFS=':' || IFS="/"
  16. read -a DATA <<< "$1"
  17. USER=${DATA[0]}
  18. BRANCH=${DATA[1]}
  19. NAME=${2:-$BRANCH}
  20. else
  21. USER=$1
  22. fi
  23. MFINFO=$(mfinfo) || exit 1
  24. IFS=' ' read -a INFO <<< "$MFINFO"
  25. REPO=${INFO[2]}
  26. set -e
  27. echo "Adding and fetching $USER/$REPO..."
  28. git remote add "$USER" "git@github.com:$USER/$REPO.git" >/dev/null 2>&1 || echo "Remote exists."
  29. git fetch "$USER"
  30. [[ ! -z "$BRANCH" && ! -z "$NAME" ]] && git checkout -b "$NAME" --track "$USER/$BRANCH"