view contrib/editmergeps.ps1 @ 44594:c23877cb25a5 stable

darwin: use vim, not vi, to avoid data-loss inducing posix behavior Apple's version of vim, available at opensource.apple.com/release/macos-1015.html (for Catalina, but this behavior has been there for a while) has several tweaks from the version of vim from vim.org. Most of these tweaks appear to be for "Unix2003" compatibility. One of the tweaks is that if any ex command raises an error, the entire process will (when you exit, possibly minutes/hours later) also exit non-zero. Ex commands are things like `:foo`. Luckily, they only enabled this if vim was executed (via a symlink or copying the binary) as `vi` or `ex`. If you start it as `vim`, it doesn't have this behavior, so let's do that. To see this in action, run the following two commands on macOS: ``` $ vi -c ':unknown' -c ':qa' ; echo $? 1 $ vim -c ':unknown' -c ':qa' ; echo $? 0 ``` We don't want to start ignoring non-zero return types from the editor because that will mean you can't use `:cquit` to intentionally exit 1 (which, shows up as 2 if you combine an ex command error and a cquit, but only a 1 if you just use cquit, so we can't differentiate between the two statuses). Since we can't differentiate, we have to assume that all non-zero exit codes are intentional and an indication of the user's desire to not continue with whatever we're doing. If this was a complicated `hg split` or `hg histedit`, this is especially disastrous :( Differential Revision: https://phab.mercurial-scm.org/D8321
author Kyle Lippincott <spectral@google.com>
date Mon, 23 Mar 2020 14:38:00 -0700
parents 92bcaef3420b
children
line wrap: on
line source

# 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