# Copyright 2013 Google Inc. All rights reserved.
# Use of this source code is governed by the Apache license that can be
# found in the LICENSE file.

"""Presubmit checks for bitmapper."""

import subprocess
import sys


def CheckChangeOnUpload(input_api, output_api):
  """Checks that the linter has been run on all .js files"""
  results = [];

  try:
    subprocess.check_output(['make', 'lint'])
  except subprocess.CalledProcessError:
    results.append(output_api.PresubmitError(
        'Linter has errors or could not be run.'))

  return results


def CheckChangeOnCommit(input_api, output_api):
  """Checks that the CL got an lgtm, and sets the current branch's remote to
  'origin' so that 'git cl push' pushes to the Github repo."""
  results = []
  checks = input_api.canned_checks

  results.extend(checks.CheckChangeWasUploaded(input_api, output_api))

  if not results:
    results.extend(checks.CheckOwners(input_api, output_api))

  if not results:
    branch = subprocess.check_output(
        ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip()
    remote = subprocess.check_output(
        ['git', 'config', 'branch.%s.remote' % branch]).strip()
    if (remote != 'origin'):
      set_remote = raw_input(
          'Remote (%s) should be set to \'origin\', set it now [y|n]?' % remote)
      if (set_remote.startswith('y')):
        subprocess.check_output(
            ['git', 'config', 'branch.%s.remote' % branch, 'origin'])
      else:
        results.append(output_api.PresubmitError(
            'Remote must be set to \'origin\' to push to Github.'))

  return results

@@ -213,16 +213,72 @@
 ort sys%0A
+from git_cl import Changelist%0Aimport tempfile%0Aimport os%0A
 %0A%0Adef Ch
@@ -398,9 +398,8 @@
 = %5B%5D
-;
 %0A%0A  
@@ -605,16 +605,925 @@
 esults%0A%0A
+def _WriteTemporaryFile(data):%0A  f = tempfile.NamedTemporaryFile('w')%0A  f.write(data)%0A  f.flush()%0A  os.fsync(f)%0A  return f%0A%0A%0Adef _CheckLocalBranchMatchesPatchset(input_api, output_api):%0A  issue = input_api.change.issue%0A  cl = Changelist(issue=issue)%0A  patch_set = cl.GetMostRecentPatchset()%0A%0A  patch_set_diff = cl.GetPatchSetDiff(issue, patch_set)%0A  local_diff = subprocess.check_output(%5B'git', 'diff', 'origin/master'%5D)%0A%0A  with _WriteTemporaryFile(patch_set_diff) as patch_set_diff_file:%0A    with _WriteTemporaryFile(local_diff) as local_diff_file:%0A      diff_diff = subprocess.check_output(%0A          %5B'interdiff', patch_set_diff_file.name, local_diff_file.name%5D)%0A%0A  if diff_diff:%0A    return %5Boutput_api.PresubmitError(%0A        'Local branch does not match patch set %25s for issue %25s. '%0A        'Revert or upload new changes to branch to resolve.%5Cn%5Cn%25s' %25%0A        (patch_set, issue, diff_diff))%5D%0A  return %5B%5D%0A
 %0Adef Che
@@ -1754,16 +1754,16 @@
 checks%0A%0A
-
   result
@@ -1816,32 +1816,126 @@
 pi, output_api))
+%0A  if not results:%0A    results.extend(_CheckLocalBranchMatchesPatchset(input_api, output_api))
 %0A%0A  if not resul

Add check to PRESUBMIT.py that local branch matches uploaded patch set