Dan Rosenstark
Dan Rosenstark

Reputation: 69757

Git Should Not Checkin If Submodule Has Modified Files

Is there a built-in way (or a simple script) that won't let me check in if any of the submodules have modified files? As it stands now, I have useless commits, since I cannot return to the submodule's state.

Upvotes: 2

Views: 229

Answers (2)

Dan Rosenstark
Dan Rosenstark

Reputation: 69757

Thanks to VonC for explaining what I needed to do. Sorry I don't know Bash, so here's the Ruby version. Works perfectly (in all cases so far, tested with git version 1.7.9.4):

#!/usr/bin/env ruby
# http://stackoverflow.com/questions/12648585/git-should-not-checkin-if-submodule-has-modified-files
# October 1, 2012
# call this file pre-commit, make it executable and put it in .git/hooks
puts "Git pre-commit hook by Dan Rosenstark (yar)"
gitDiff = `git diff`
gitDiffWithSubmodules = `git diff --ignore-submodules`
if (gitDiff.length == gitDiffWithSubmodules.length)
  puts "Submodules are clean, going right ahead!"
else
  puts "One or more submodules are dirty, can't commit.'"
  exit 1
end

Upvotes: 2

VonC
VonC

Reputation: 1324218

That looks like the pre-commit hook "script for submodule hygiene" is for:
preventing the commit if a submodule has pending changes.
(See also that patch/script)

That script was written 4 years ago, so maybe the git diff now includes a simpler way of detecting "dirty" submodules.

Upvotes: 3

Related Questions