Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added git sm delete command

  • Loading branch information...
commit 57d28a12dcb9ee7d727acecff440f3828172d608 1 parent fac582e
Tammer Saleh authored

Showing 1 changed file with 83 additions and 0 deletions. Show diff stats Hide diff stats

  1. +83 0 bin/git-submodule-delete
83 bin/git-submodule-delete
... ... @@ -0,0 +1,83 @@
  1 +#!/usr/bin/env ruby
  2 +
  3 +require "fileutils"
  4 +
  5 +USAGE =<<EOS
  6 +Usage:
  7 + $ git delete-submodule <path-to-submodule>
  8 +
  9 +This command will delete the files of a submodule and remove it from git completely. It is equivalent to
  10 +performing the following three steps:
  11 + * Deleting the lines about the submodule from .gitmodules
  12 + * Deleting the lines about the submodule from .git/config
  13 + * git rm --cached path-to-submodule (from the root of the repo)
  14 + * rm -rf path-to-submodule
  15 +
  16 +Note that you will probably want to commit this change afterwards.
  17 +
  18 +EOS
  19 +
  20 +abort USAGE unless (["-h", "--help"] & ARGV).empty?
  21 +abort USAGE if ARGV.empty?
  22 +
  23 +submodule_path = ARGV[0]
  24 +abort "ARGV[0] does not exist." unless File.exists? submodule_path
  25 +absolute_path = File.absolute_path(submodule_path)
  26 +
  27 +def abort_on_failure(cmd)
  28 + result = `#{cmd}`
  29 + abort unless $?.to_i.zero?
  30 + result
  31 +end
  32 +
  33 +git_root = abort_on_failure("git rev-parse --show-toplevel").strip
  34 +FileUtils.cd git_root
  35 +submodule_path = absolute_path.gsub("#{git_root}/", "").gsub(/\/$/, "")
  36 +
  37 +raw_submodule_status_lines = abort_on_failure("git submodule status").split "\n"
  38 +
  39 +valid_submodule = raw_submodule_status_lines.any? do |line|
  40 + matches = line.match /^.[0-9a-f]{40} (\S+)(\s|$)/
  41 + matches && submodule_path == matches[1]
  42 +end
  43 +
  44 +abort "#{submodule_path} does not appear to be a git submodule." unless valid_submodule
  45 +
  46 +puts "Are you sure you want to delete the git submodule located at #{submodule_path}?"
  47 +print "This will delete the files. Type 'yes' to continue: "
  48 +unless STDIN.gets.chomp == "yes"
  49 + puts "Aborting."
  50 + exit
  51 +end
  52 +
  53 +puts "Removing the appropriate section from .gitmodules..."
  54 +`git config -f .gitmodules --remove-section submodule.#{submodule_path} 2> /dev/null`
  55 +unless $?.to_i.zero?
  56 + abort "There was a problem removing the submodule.#{submodule_path} section from .gitmodules. Aborting."
  57 +end
  58 +
  59 +puts "Removing the appropriate section from .git/config..."
  60 +`git config -f .git/config --remove-section submodule.#{submodule_path} 2> /dev/null`
  61 +# Don't need to check this one -- it might not exist if you haven't run git submodule init yet.
  62 +
  63 +metadata_directory = `cd #{submodule_path} && git rev-parse --git-dir`.strip
  64 +unless metadata_directory.start_with? "/"
  65 + metadata_directory = File.join(submodule_path, metadata_directory)
  66 +end
  67 +
  68 +puts "Deleting the submodule from the git cache..."
  69 +`git rm --cached #{submodule_path}`
  70 +unless $?.to_i.zero?
  71 + abort "There was an error running 'git rm --cached #{submodule_path}'. Aborting."
  72 +end
  73 +
  74 +# Need to explicitly remove the metadata dir for 1.7.8+ compatibility
  75 +puts "Removing the metadata directory..."
  76 +unless File.directory? metadata_directory
  77 + abort "The submodule's metadata directory was reported to be #{metadata_directory}, but it does not exist."
  78 +end
  79 +FileUtils.rm_rf metadata_directory
  80 +
  81 +FileUtils.rm_rf submodule_path
  82 +
  83 +puts "Successfully deleted submodule #{submodule_path}. You should probably commit this change now."

0 comments on commit 57d28a1

Please sign in to comment.
Something went wrong with that request. Please try again.