contrib/editmergeps.ps1
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 32570 92bcaef3420b
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.

# A simple script for opening merge conflicts in editor
# A loose translation of contrib/editmerge to powershell
# Please make sure that both editmergeps.bat and editmerge.ps1 are available
# via %PATH% and use the following Mercurial settings to enable it
#
# [ui]
# editmergeps
# editmergeps.args=$output
# editmergeps.check=changed
# editmergeps.premerge=keep

$file=$args[0]

function Get-Lines
{
  Select-String "^<<<<<<" $file | % {"$($_.LineNumber)"}
}

$ed = $Env:HGEDITOR;
if ($ed -eq $nil)
{
  $ed = $Env:VISUAL;
}
if ($ed -eq $nil)
{
  $ed = $Env:EDITOR;
}
if ($ed -eq $nil)
{
  $ed = $(hg showconfig ui.editor);
}
if ($ed -eq $nil)
{
  Write-Error "merge failed - unable to find editor"
  exit 1
}

if (($ed -eq "vim") -or ($ed -eq "emacs") -or `
    ($ed -eq "nano") -or ($ed -eq "notepad++"))
{
  $lines = Get-Lines
  $firstline = if ($lines.Length -gt 0) { $lines[0] } else { $nil }
  $previousline = $nil;


  # open the editor to the first conflict until there are no more
  # or the user stops editing the file
  while (($firstline -ne $nil) -and ($firstline -ne $previousline))
  {
    if ($ed -eq "notepad++")
    {
        $linearg = "-n$firstline"
    }
    else
    {
        $linearg = "+$firstline"
    }

    Start-Process -Wait -NoNewWindow $ed $linearg,$file
    $previousline = $firstline
    $lines = Get-Lines
    $firstline = if ($lines.Length -gt 0) { $lines[0] } else { $nil }
  }
}
else
{
  & "$ed" $file
}

$conflicts=Get-Lines
if ($conflicts.Length -ne 0)
{
  Write-Output "merge failed - resolve the conflicts (line $conflicts) then use 'hg resolve --mark'"
  exit 1
}

exit 0