Commit affe7b81 authored by gavinp@chromium.org's avatar gavinp@chromium.org

Make git try warn on a dirty index.

Many, many times this has burned me badly; if you have a dirty index,
git try will just upload your commits, and not the index.

R=nsylvain@chromium.org
BUG=None

Review URL: https://chromiumcodereview.appspot.com/10836180

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@151202 0039d316-1c4b-4281-b951-d872f2087c98
parent 492a368f
...@@ -22,6 +22,7 @@ class TryChangeTestsBase(SuperMoxTestBase): ...@@ -22,6 +22,7 @@ class TryChangeTestsBase(SuperMoxTestBase):
def setUp(self): def setUp(self):
SuperMoxTestBase.setUp(self) SuperMoxTestBase.setUp(self)
self.mox.StubOutWithMock(subprocess2, 'communicate') self.mox.StubOutWithMock(subprocess2, 'communicate')
self.mox.StubOutWithMock(trychange, 'RunGit')
self.mox.StubOutWithMock(trychange.scm.GIT, 'Capture') self.mox.StubOutWithMock(trychange.scm.GIT, 'Capture')
self.mox.StubOutWithMock(trychange.scm.GIT, 'GenerateDiff') self.mox.StubOutWithMock(trychange.scm.GIT, 'GenerateDiff')
self.mox.StubOutWithMock(trychange.scm.GIT, 'GetCheckoutRoot') self.mox.StubOutWithMock(trychange.scm.GIT, 'GetCheckoutRoot')
...@@ -46,14 +47,12 @@ class TryChangeUnittest(TryChangeTestsBase): ...@@ -46,14 +47,12 @@ class TryChangeUnittest(TryChangeTestsBase):
"""General trychange.py tests.""" """General trychange.py tests."""
def testMembersChanged(self): def testMembersChanged(self):
members = [ members = [
'EPILOG', 'Escape', 'GIT', 'GuessVCS', 'GetMungedDiff', 'HELP_STRING', 'DieWithError', 'EPILOG', 'Escape', 'GIT', 'GetMungedDiff', 'GuessVCS',
'InvalidScript', 'NoTryServerAccess', 'PrintSuccess', 'SCM', 'SVN', 'HELP_STRING', 'InvalidScript', 'NoTryServerAccess', 'PrintSuccess',
'TryChange', 'USAGE', 'RunCommand', 'RunGit', 'SCM', 'SVN', 'TryChange', 'USAGE', 'breakpad',
'breakpad', 'datetime', 'errno', 'fix_encoding', 'gcl', 'gclient_utils', 'datetime', 'errno', 'fix_encoding', 'gcl', 'gclient_utils', 'gen_parser',
'getpass', 'gen_parser', 'getpass', 'json', 'logging', 'optparse', 'os', 'posixpath', 're', 'scm',
'json', 'logging', 'optparse', 'os', 'posixpath', 're', 'scm', 'shutil', 'shutil', 'subprocess2', 'sys', 'tempfile', 'urllib']
'subprocess2', 'sys', 'tempfile', 'urllib',
]
# If this test fails, you should add the relevant test. # If this test fails, you should add the relevant test.
self.compareMembers(trychange, members) self.compareMembers(trychange, members)
...@@ -138,6 +137,7 @@ class GITUnittest(TryChangeTestsBase): ...@@ -138,6 +137,7 @@ class GITUnittest(TryChangeTestsBase):
trychange.os.path.abspath(self.fake_root).AndReturn(self.fake_root) trychange.os.path.abspath(self.fake_root).AndReturn(self.fake_root)
trychange.scm.GIT.GetCheckoutRoot(self.fake_root).AndReturn(self.fake_root) trychange.scm.GIT.GetCheckoutRoot(self.fake_root).AndReturn(self.fake_root)
trychange.scm.GIT.GetUpstreamBranch(self.fake_root).AndReturn('somewhere') trychange.scm.GIT.GetUpstreamBranch(self.fake_root).AndReturn('somewhere')
trychange.RunGit(['diff-index', 'HEAD'])
trychange.scm.GIT.GenerateDiff(self.fake_root, trychange.scm.GIT.GenerateDiff(self.fake_root,
full_move=True, full_move=True,
files=['foo.txt', 'bar.txt'], files=['foo.txt', 'bar.txt'],
......
...@@ -65,6 +65,28 @@ Examples: ...@@ -65,6 +65,28 @@ Examples:
-f include/b.h -f include/b.h
""" """
def DieWithError(message):
print >> sys.stderr, message
sys.exit(1)
def RunCommand(args, error_ok=False, error_message=None, **kwargs):
try:
return subprocess2.check_output(args, shell=False, **kwargs)
except subprocess2.CalledProcessError, e:
if not error_ok:
DieWithError(
'Command "%s" failed.\n%s' % (
' '.join(args), error_message or e.stdout or ''))
return e.stdout
def RunGit(args, **kwargs):
"""Returns stdout."""
return RunCommand(['git'] + args, **kwargs)
class InvalidScript(Exception): class InvalidScript(Exception):
def __str__(self): def __str__(self):
return self.args[0] + '\n' + HELP_STRING return self.args[0] + '\n' + HELP_STRING
...@@ -272,6 +294,9 @@ class GIT(SCM): ...@@ -272,6 +294,9 @@ class GIT(SCM):
self.diff_against) self.diff_against)
def GenerateDiff(self): def GenerateDiff(self):
if RunGit(['diff-index', 'HEAD']):
print 'Cannot try with a dirty tree. You must commit locally first.'
return None
return scm.GIT.GenerateDiff( return scm.GIT.GenerateDiff(
self.checkout_root, self.checkout_root,
files=self.files, files=self.files,
...@@ -750,7 +775,10 @@ def TryChange(argv, ...@@ -750,7 +775,10 @@ def TryChange(argv,
root = checkouts[0].checkout_root root = checkouts[0].checkout_root
diffs = [] diffs = []
for checkout in checkouts: for checkout in checkouts:
diff = checkout.GenerateDiff().splitlines(True) raw_diff = checkout.GenerateDiff()
if not raw_diff:
return 1
diff = raw_diff.splitlines(True)
path_diff = gclient_utils.PathDifference(root, checkout.checkout_root) path_diff = gclient_utils.PathDifference(root, checkout.checkout_root)
# Munge it. # Munge it.
diffs.extend(GetMungedDiff(path_diff, diff)[0]) diffs.extend(GetMungedDiff(path_diff, diff)[0])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment