My dotfiles. Period.

remote-edit 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/bash
  2. #
  3. # Edit URL of remote $1 using vipe(1)
  4. #
  5. warn() {
  6. echo "$@" >&2
  7. }
  8. die() {
  9. warn "$@"
  10. exit 3
  11. }
  12. usage() {
  13. warn "usage: git $(basename "$0") [-v|--verbose] REMOTE"
  14. exit 2
  15. }
  16. think() {
  17. $verbose && echo "$@"
  18. }
  19. main() {
  20. local remote
  21. local oldurl
  22. local newurl
  23. local verbose=false
  24. while true; do case $1 in
  25. -v|--verbose) verbose=true; shift ;;
  26. -*) usage ;;
  27. *) break ;;
  28. esac done
  29. remote="$1"
  30. test -n "$remote" || usage
  31. command -v vipe >/dev/null \
  32. || die "need vipe(1) from moreutils"
  33. think "remote='$remote'"
  34. oldurl=$(git remote get-url "$remote") \
  35. || die "failed to get old URL"
  36. test "$newurl" == "$oldurl" && {
  37. warn "URL was not changed"
  38. return 1
  39. }
  40. think "oldurl='$oldurl'"
  41. think "EDITOR='$EDITOR'"
  42. think "VISUAL='$VISUAL'"
  43. newurl=$(vipe <<<"$oldurl") \
  44. || die "failed to edit URL (is vipe installed?)"
  45. think "newurl='$newurl'"
  46. git remote set-url "$remote" "$newurl" \
  47. || die "failed to set new remote URL"
  48. }
  49. main "$@"