Guest User

Untitled

a guest
Jun 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ## erases lines matching pattern (service name) from file
  4. ## arguments : pattern, file
  5. erase_service_from_file ()
  6. {
  7. LINES=$(grep -n "$1" "$2" |cut -f1 -d:)
  8. if [ "$LINES" = "" ]
  9. then
  10. echo "submodule $1 not found"
  11. return 1
  12. fi
  13.  
  14. PATTERN=$(echo $LINES | sed 's/ /d;/g')"d"
  15.  
  16. if [ "$PATTERN" = "" ]
  17. then
  18. echo "submodule $1 not found"
  19. return 1
  20. fi
  21. sed -i "$PATTERN" "$2"
  22. return 0
  23. }
  24.  
  25. ## erases lines in range matching pattern from file using sed
  26. ## arguments : pattern, file
  27. erase_using_sed ()
  28. {
  29. LINES=$(grep -n -m1 "$1" "$2" |cut -f1 -d:)
  30. if [ "$LINES" = "" ]
  31. then
  32. echo "submodule $1 not found"
  33. return 1
  34. fi
  35.  
  36. PATTERN=$(echo "$LINES $(( LINES + 1)) $(( LINES + 2))" | sed 's/ /d;/g')"d" # no quote to shed newline (no I don't know why it works)
  37.  
  38. if [ "$PATTERN" = "" ]
  39. then
  40. echo "submodule $1 not found"
  41. return 1
  42. fi
  43. sed -i "$PATTERN" "$2"
  44. return 0
  45. }
  46.  
  47. main()
  48. {
  49. echo "Erasing submodule $1 from repository..."
  50. erase_service_from_file "$1" .gitmodules
  51. STAGE_1=$?
  52. if [ "$STAGE_1" = 1 ]
  53. then
  54. echo "$1 does not exist as a submodule"
  55. return 1
  56. fi
  57. echo "Staging changes to .gitmodules..."
  58. git add .gitmodules
  59. echo "Erasing submodule $1 from git config file..."
  60. erase_using_sed "$1" .git/config
  61. STAGE_2=$?
  62. if [ "$STAGE_2" = 1 ]
  63. then
  64. echo "$1 does not exist as a submodule in .git/config"
  65. return 1
  66. fi
  67. echo "Removing $1 from repository..."
  68. git rm -rf --cached "$1" ; rm -rf .git/modules/"$1"
  69. echo "Removing $1 from file system..."
  70. rm -rf "$1"
  71. echo "Done !"
  72. }
  73.  
  74.  
  75. if [ $# -eq 0 ]
  76. then
  77. echo "Usage : sh submodule_remove.sh <submodule>"
  78. return 0
  79. else
  80. main "$1"
  81. fi
Add Comment
Please, Sign In to add comment