<commit_before>#!/bin/env python
# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Wrapper around BeyondCompare or kdiff3 so it can be used as svn's diff3-cmd
tool.

The basic idea here is based heavily off of diffwrap.py at:
http://svnbook.red-bean.com/en/1.5/svn.advanced.externaldifftools.html#svn.advanced.externaldifftools.diff3.ex-1
"""

import optparse
import os
import subprocess
import sys

CYGDRIVE = '/cygdrive/'
CYGLEN = len(CYGDRIVE)

# TODO(ojan): Eventually make this a cross-platform script that makes it easy
# to use different diffing tools (an enum of tools maybe?).
# The difficulty is that each tool has its own supported arguments.
def EnsureWindowsPath(path):
  """ Returns a Windows-style path given a Windows path or a cygwin path.
  """
  if path.startswith(CYGDRIVE):
    path = path[CYGLEN:CYGLEN + 1] + ':' + path[CYGLEN + 1:]
    path = path.replace('/', '\\')
  return path
  
def GetPathToBinary(exe):
  """ Try to find a copy of the binary that exists. Search for the full path
  and then the basename.
  """
  if not os.path.exists(exe):
    exe = os.path.basename(exe)
  return exe

def main(args):
  """ Provides a wrapper around 3rd-party diffing tools so they can be used as
  the diff3-cmd in svn merge.

  args: The arguments passed by svn merge to its diff3 tool.
  """
  # Grab the arguments from the end of the list since svn will add any other 
  # arguments provided before these.
  
  # The titles of the files being diffed.
  title_mine = EnsureWindowsPath(args[-8])
  title_older = EnsureWindowsPath(args[-6])
  title_yours = EnsureWindowsPath(args[-4])

  # The paths to the files being diffed. These will be temp files.
  mine = EnsureWindowsPath(args[-3])
  older = EnsureWindowsPath(args[-2])
  yours = EnsureWindowsPath(args[-1])
  
  # The command for which diff3 tool to use.
  diff_tool = args[1]
  
  if diff_tool == "--use-beyondcompare":
    exe = GetPathToBinary("c:/Progra~1/Beyond~1/BComp.exe")
    cmd = [exe, 
           mine,
           yours,
           older,
           mine,
           '/reviewconflicts',
           '/automerge',
           '/leftreadonly',
           '/rightreadonly',
           '/ignoreunimportant',
           '/lefttitle', title_mine, 
           '/righttitle', title_yours, 
           '/centertitle', title_older, 
           '/outputtitle', 'merged']
  elif diff_tool == "--use-kdiff3":
    exe = GetPathToBinary("c:/Progra~1/KDiff3/kdiff3.exe")
    cmd = [exe,
           older,
           mine,
           yours,
           "--merge",
           "--auto",
           "--output", mine]
  else:
    # TODO(ojan): Maybe fall back to diff3?
    raise Exception, "Must specify a diff tool to use."
           
  value = subprocess.call(cmd, stdout=subprocess.PIPE)

  # After performing the merge, this script needs to print the contents
  # of the merged file to stdout. 
  # Return an errorcode of 0 on successful merge, 1 if unresolved conflicts
  # remain in the result.  Any other errorcode will be treated as fatal.
  merged_file_contents = open(mine).read()

  # Ensure that the file doesn't use CRLF, in case the diff program converted
  # line endings.
  merged_file_contents.replace('\r\n', '\n')

  # For reasons I don't understand, an extra line break gets added at the end
  # of the file. Strip it.
  merged_file_contents = merged_file_contents[:-1]
  print merged_file_contents
  sys.exit(value)

if '__main__' == __name__:
  main(sys.argv)<commit_msg>Fix the merge diff3 wrapper to work around a python subprocess bug on windows TBR(ojan) Ojan wrote this with me and we tested it together Review URL: http://codereview.chromium.org/19023<commit_after>#!/bin/env python
# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Wrapper around BeyondCompare or kdiff3 so it can be used as svn's diff3-cmd
tool.

The basic idea here is based heavily off of diffwrap.py at:
http://svnbook.red-bean.com/en/1.5/svn.advanced.externaldifftools.html#svn.advanced.externaldifftools.diff3.ex-1
"""

import optparse
import os
import subprocess
import sys
import traceback

CYGDRIVE = '/cygdrive/'
CYGLEN = len(CYGDRIVE)

# TODO(ojan): Eventually make this a cross-platform script that makes it easy
# to use different diffing tools (an enum of tools maybe?).
# The difficulty is that each tool has its own supported arguments.
def EnsureWindowsPath(path):
  """ Returns a Windows-style path given a Windows path or a cygwin path.
  """
  if path.startswith(CYGDRIVE):
    path = path[CYGLEN:CYGLEN + 1] + ':' + path[CYGLEN + 1:]
    path = path.replace('/', '\\')
  return path
  
def GetPathToBinary(exe):
  """ Try to find a copy of the binary that exists. Search for the full path
  and then the basename.
  """
  if not os.path.exists(exe):
    exe = os.path.basename(exe)
  return exe

def main(args):
  """ Provides a wrapper around 3rd-party diffing tools so they can be used as
  the diff3-cmd in svn merge.

  args: The arguments passed by svn merge to its diff3 tool.
  """
  # Grab the arguments from the end of the list since svn will add any other 
  # arguments provided before these.
  
  # The titles of the files being diffed.
  title_mine = EnsureWindowsPath(args[-8])
  title_older = EnsureWindowsPath(args[-6])
  title_yours = EnsureWindowsPath(args[-4])

  # The paths to the files being diffed. These will be temp files.
  mine = EnsureWindowsPath(args[-3])
  older = EnsureWindowsPath(args[-2])
  yours = EnsureWindowsPath(args[-1])
  
  # The command for which diff3 tool to use.
  diff_tool = args[1]
  
  if diff_tool == "--use-beyondcompare":
    exe = GetPathToBinary("c:/Progra~1/Beyond~1/BComp.exe")
    cmd = [exe, 
           mine,
           yours,
           older,
           mine,
           '/reviewconflicts',
           '/automerge',
           '/leftreadonly',
           '/rightreadonly',
           '/ignoreunimportant',
           '/lefttitle', title_mine, 
           '/righttitle', title_yours, 
           '/centertitle', title_older, 
           '/outputtitle', 'merged']
  elif diff_tool == "--use-kdiff3":
    exe = GetPathToBinary("c:/Progra~1/KDiff3/kdiff3.exe")
    cmd = [exe,
           older,
           mine,
           yours,
           "--merge",
           "--auto",
           "--output", mine]
  else:
    # TODO(ojan): Maybe fall back to diff3?
    raise Exception, "Must specify a diff tool to use."

  try:
    # Work around http://bugs.python.org/issue3905
    # by passing stderr and stdin as well since beyondcompare is a GUI app
    return_code = subprocess.call(cmd,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE,
                                  stdin=subprocess.PIPE,
                                  shell=True)
  except Exception,e:
    print "Error running " + (" ".join(cmd))
    traceback.print_exc(file=sys.stdout)

  # After performing the merge, this script needs to print the contents
  # of the merged file to stdout. 
  # Return an errorcode of 0 on successful merge, 1 if unresolved conflicts
  # remain in the result.  Any other errorcode will be treated as fatal.
  merged_file_contents = open(mine).read()
  # Ensure that the file doesn't use CRLF, in case the diff program converted
  # line endings.
  merged_file_contents.replace('\r\n', '\n')

  # For reasons I don't understand, an extra line break gets added at the end
  # of the file. Strip it.
  merged_file_contents = merged_file_contents[:-1]
  print merged_file_contents
  return return_code

if '__main__' == __name__:
  try:
    return_code = main(sys.argv)
  except Exception,e:
    traceback.print_exc(file=sys.stdout)
    # diff3 uses '1' to mean "conflict" and 2 to mean "I barfed".
    sys.exit(2) # return "I barfed"
  sys.exit(return_code)
{'fraction_non_alphanumeric': 0.10118675827607745, 'fraction_numerical': 0.01099312929419113, 'mean_word_length': 2.7781972628598397, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3611893', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2444, 'n_words': 856}