Commit 81e012ce authored by maruel@chromium.org's avatar maruel@chromium.org

Modify scm.GIT.GetUpstreamBranch to behave like git-cl.

It now defaults to origin/master when no branch is tracked.

TEST=git-try on untracked branch now doesn't result in an empty diff
BUG=none

Review URL: http://codereview.chromium.org/1739021

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@45935 0039d316-1c4b-4281-b951-d872f2087c98
parent 1dd0eea6
...@@ -237,11 +237,11 @@ class GitWrapper(SCMWrapper): ...@@ -237,11 +237,11 @@ class GitWrapper(SCMWrapper):
# 4) current branch based on a remote, switches to a new remote # 4) current branch based on a remote, switches to a new remote
# - exit # - exit
# GetUpstream returns something like 'refs/remotes/origin/master' for a # GetUpstreamBranch returns something like 'refs/remotes/origin/master' for
# tracking branch # a tracking branch
# or 'master' if not a tracking branch (it's based on a specific rev/hash) # or 'master' if not a tracking branch (it's based on a specific rev/hash)
# or it returns None if it couldn't find an upstream # or it returns None if it couldn't find an upstream
upstream_branch = scm.GIT.GetUpstream(self.checkout_path) upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
if not upstream_branch or not upstream_branch.startswith('refs/remotes'): if not upstream_branch or not upstream_branch.startswith('refs/remotes'):
current_type = "hash" current_type = "hash"
logging.debug("Current branch is based off a specific rev and is not " logging.debug("Current branch is based off a specific rev and is not "
......
...@@ -202,6 +202,7 @@ class GIT(object): ...@@ -202,6 +202,7 @@ class GIT(object):
def FetchUpstreamTuple(cwd): def FetchUpstreamTuple(cwd):
"""Returns a tuple containg remote and remote ref, """Returns a tuple containg remote and remote ref,
e.g. 'origin', 'refs/heads/master' e.g. 'origin', 'refs/heads/master'
Tries to be intelligent and understand git-svn.
""" """
remote = '.' remote = '.'
branch = GIT.GetBranch(cwd) branch = GIT.GetBranch(cwd)
...@@ -217,10 +218,18 @@ class GIT(object): ...@@ -217,10 +218,18 @@ class GIT(object):
# Fall back on trying a git-svn upstream branch. # Fall back on trying a git-svn upstream branch.
if GIT.IsGitSvn(cwd): if GIT.IsGitSvn(cwd):
upstream_branch = GIT.GetSVNBranch(cwd) upstream_branch = GIT.GetSVNBranch(cwd)
# Fall back on origin/master if it exits.
elif GIT.Capture(['branch', '-r'], in_directory=cwd
)[0].split().count('origin/master'):
remote = 'origin'
upstream_branch = 'refs/heads/master'
else:
remote = None
upstream_branch = None
return remote, upstream_branch return remote, upstream_branch
@staticmethod @staticmethod
def GetUpstream(cwd): def GetUpstreamBranch(cwd):
"""Gets the current branch's upstream branch.""" """Gets the current branch's upstream branch."""
remote, upstream_branch = GIT.FetchUpstreamTuple(cwd) remote, upstream_branch = GIT.FetchUpstreamTuple(cwd)
if remote != '.': if remote != '.':
...@@ -235,7 +244,7 @@ class GIT(object): ...@@ -235,7 +244,7 @@ class GIT(object):
full_move means that move or copy operations should completely recreate the full_move means that move or copy operations should completely recreate the
files, usually in the prospect to apply the patch for a try job.""" files, usually in the prospect to apply the patch for a try job."""
if not branch: if not branch:
branch = GIT.GetUpstream(cwd) branch = GIT.GetUpstreamBranch(cwd)
command = ['diff', '-p', '--no-prefix', branch + "..." + branch_head] command = ['diff', '-p', '--no-prefix', branch + "..." + branch_head]
if not full_move: if not full_move:
command.append('-C') command.append('-C')
...@@ -255,7 +264,7 @@ class GIT(object): ...@@ -255,7 +264,7 @@ class GIT(object):
def GetDifferentFiles(cwd, branch=None, branch_head='HEAD'): def GetDifferentFiles(cwd, branch=None, branch_head='HEAD'):
"""Returns the list of modified files between two branches.""" """Returns the list of modified files between two branches."""
if not branch: if not branch:
branch = GIT.GetUpstream(cwd) branch = GIT.GetUpstreamBranch(cwd)
command = ['diff', '--name-only', branch + "..." + branch_head] command = ['diff', '--name-only', branch + "..." + branch_head]
return GIT.Capture(command, cwd)[0].splitlines(False) return GIT.Capture(command, cwd)[0].splitlines(False)
......
...@@ -122,7 +122,8 @@ from :3 ...@@ -122,7 +122,8 @@ from :3
'FetchUpstreamTuple', 'FetchUpstreamTuple',
'GenerateDiff', 'GetBranch', 'GetBranchRef', 'GetCheckoutRoot', 'GenerateDiff', 'GetBranch', 'GetBranchRef', 'GetCheckoutRoot',
'GetDifferentFiles', 'GetEmail', 'GetPatchName', 'GetSVNBranch', 'GetDifferentFiles', 'GetEmail', 'GetPatchName', 'GetSVNBranch',
'GetUpstream', 'IsGitSvn', 'RunAndFilterOutput', 'ShortBranchName', 'GetUpstreamBranch', 'IsGitSvn', 'RunAndFilterOutput',
'ShortBranchName',
] ]
# If this test fails, you should add the relevant test. # If this test fails, you should add the relevant test.
self.compareMembers(scm.GIT, members) self.compareMembers(scm.GIT, members)
......
...@@ -19,8 +19,9 @@ class TryChangeTestsBase(SuperMoxTestBase): ...@@ -19,8 +19,9 @@ class TryChangeTestsBase(SuperMoxTestBase):
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')
self.mox.StubOutWithMock(trychange.scm.GIT, 'GetPatchName')
self.mox.StubOutWithMock(trychange.scm.GIT, 'GetEmail') self.mox.StubOutWithMock(trychange.scm.GIT, 'GetEmail')
self.mox.StubOutWithMock(trychange.scm.GIT, 'GetPatchName')
self.mox.StubOutWithMock(trychange.scm.GIT, 'GetUpstreamBranch')
self.mox.StubOutWithMock(trychange.scm.SVN, 'DiffItem') self.mox.StubOutWithMock(trychange.scm.SVN, 'DiffItem')
self.mox.StubOutWithMock(trychange.scm.SVN, 'GenerateDiff') self.mox.StubOutWithMock(trychange.scm.SVN, 'GenerateDiff')
self.mox.StubOutWithMock(trychange.scm.SVN, 'GetCheckoutRoot') self.mox.StubOutWithMock(trychange.scm.SVN, 'GetCheckoutRoot')
...@@ -88,10 +89,11 @@ class GITUnittest(TryChangeTestsBase): ...@@ -88,10 +89,11 @@ class GITUnittest(TryChangeTestsBase):
def testBasic(self): def testBasic(self):
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.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'],
branch=None).AndReturn('A diff') branch='somewhere').AndReturn('A diff')
trychange.scm.GIT.GetPatchName(self.fake_root).AndReturn('bleh-1233') trychange.scm.GIT.GetPatchName(self.fake_root).AndReturn('bleh-1233')
trychange.scm.GIT.GetEmail(self.fake_root).AndReturn('georges@example.com') trychange.scm.GIT.GetEmail(self.fake_root).AndReturn('georges@example.com')
self.mox.ReplayAll() self.mox.ReplayAll()
......
...@@ -213,6 +213,12 @@ class GIT(SCM): ...@@ -213,6 +213,12 @@ class GIT(SCM):
self.options.name = scm.GIT.GetPatchName(self.checkout_root) self.options.name = scm.GIT.GetPatchName(self.checkout_root)
if not self.options.email: if not self.options.email:
self.options.email = scm.GIT.GetEmail(self.checkout_root) self.options.email = scm.GIT.GetEmail(self.checkout_root)
if not self.diff_against:
self.diff_against = scm.GIT.GetUpstreamBranch(self.checkout_root)
if not self.diff_against:
print "Unable to determine default branch to diff against."
print "Verify this branch is set up to track another"
print "(via the --track argument to \"git checkout -b ...\""
logging.info("GIT(%s)" % self.checkout_root) logging.info("GIT(%s)" % self.checkout_root)
def ReadRootFile(self, filename): def ReadRootFile(self, filename):
......
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