Mercurial > hg
view tests/test-eol.t @ 35569:964212780daf
rust: implementation of `hg`
This commit provides a mostly-working implementation of the
`hg` script in Rust along with scaffolding to support Rust in
the repository.
If you are familiar with Rust, the contents of the added rust/
directory should be pretty straightforward. We create an "hgcli"
package that implements a binary application to run Mercurial.
The output of this package is an "hg" binary.
Our Rust `hg` (henceforth "rhg") essentially is a port of the existing
`hg` Python script. The main difference is the creation of the embedded
CPython interpreter is handled by the binary itself instead of relying
on the shebang. In that sense, rhg is more similar to the "exe wrapper"
we currently use on Windows. However, unlike the exe wrapper, rhg does
not call the `hg` Python script. Instead, it uses the CPython APIs to
import mercurial modules and call appropriate functions. The amount of
code here is surprisingly small.
It is my intent to replace the existing C-based exe wrapper with rhg.
Preferably in the next Mercurial release. This should be achievable -
at least for some Mercurial distributions. The future/timeline for
rhg on other platforms is less clear. We already ship a hg.exe on
Windows. So if we get the quirks with Rust worked out, shipping a
Rust-based hg.exe should hopefully not be too contentious.
Now onto the implementation.
We're using python27-sys and the cpython crates for talking to the
CPython API. We currently don't use too much functionality of the
cpython crate and could have probably cut it out. However, it does
provide a reasonable abstraction over unsafe {} CPython function
calls. While we still have our fair share of those, at least we're
not dealing with too much refcounting, error checking, etc. So I
think the use of the cpython crate is justified. Plus, there is
not-yet-implemented functionality that could benefit from cpython. I
see our use of this crate only increasing.
The cpython and python27-sys crates are not without their issues.
The cpython crate didn't seem to account for the embedding use case
in its design. Instead, it seems to assume that you are building
a Python extension. It is making some questionable decisions around
certain CPython APIs. For example, it insists that
PyEval_ThreadsInitialized() is called and that the Python code
likely isn't the main thread in the underlying application. It
is also missing some functionality that is important for embedded
use cases (such as exporting the path to the Python interpreter
from its build script). After spending several hours trying to
wrangle python27-sys and cpython, I gave up and forked the project
on GitHub. Our Cargo.toml tracks this fork. I'm optimistic that
the upstream project will accept our contributions and we can
eventually unfork.
There is a non-trivial amount of code in our custom Cargo build
script. Our build.rs (which is called as part of building the hgcli
crate):
* Validates that the Python interpreter that was detected by the
python27-sys crate provides a shared library (we only support
shared library linking at this time - although this restriction
could be loosened).
* Validates that the Python is built with UCS-4 support. This ensures
maximum Unicode compatibility.
* Exports variables to the crate build allowing the built crate to e.g.
find the path to the Python interpreter.
The produced rhg should be considered alpha quality. There are several
known deficiencies. Many of these are documented with inline TODOs.
Probably the biggest limitation of rhg is that it assumes it is
running from the ./rust/target/<target> directory of a source
distribution. So, rhg is currently not very practical for real-world
use. But, if you can `cargo build` it, running the binary *should*
yield a working Mercurial CLI.
In order to support using rhg with the test harness, we needed to hack
up run-tests.py so the path to Mercurial's Python files is set properly.
The change is extremely hacky and is only intended to be a stop-gap
until the test harness gains first-class support for installing rhg.
This will likely occur after we support running rhg outside the
source directory.
Despite its officially alpha quality, rhg copes extremely well with
the test harness (at least on Linux). Using
`run-tests.py --with-hg ../rust/target/debug/hg`, I only encounter
the following failures:
* test-run-tests.t -- Warnings emitted about using an unexpected
Mercurial library. This is due to the hacky nature of setting the
Python directory when run-tests.py detected rhg.
* test-devel-warnings.t -- Expected stack trace missing frame for `hg`
(This is expected since we no longer have an `hg` script!)
* test-convert.t -- Test running `$PYTHON "$BINDIR"/hg`, which obviously
assumes `hg` is a Python script.
* test-merge-tools.t -- Same assumption about `hg` being executable with
Python.
* test-http-bad-server.t -- Seeing exit code 255 instead of 1 around
line 358.
* test-blackbox.t -- Exit code 255 instead of 1.
* test-basic.t -- Exit code 255 instead of 1.
It certainly looks like we have a bug around exit code handling. I
don't think it is severe enough to hold up review and landing of this
initial implementation. Perfect is the enemy of good.
Differential Revision: https://phab.mercurial-scm.org/D1581
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 10 Jan 2018 08:53:22 -0800 |
parents | efa6a420ba57 |
children | b85b0bbed6de |
line wrap: on
line source
Test EOL extension $ cat >> $HGRCPATH <<EOF > [diff] > git = True > EOF Set up helpers $ cat > switch-eol.py <<EOF > from __future__ import absolute_import > import os > import sys > try: > import msvcrt > msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) > msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) > except ImportError: > pass > (old, new) = sys.argv[1] == 'LF' and ('\n', '\r\n') or ('\r\n', '\n') > print("%% switching encoding from %r to %r" % (old, new)) > for path in sys.argv[2:]: > data = file(path, 'rb').read() > data = data.replace(old, new) > file(path, 'wb').write(data) > EOF $ seteol () { > if [ $1 = "LF" ]; then > EOL='\n' > else > EOL='\r\n' > fi > } $ makerepo () { > seteol $1 > echo "% setup $1 repository" > hg init repo > cd repo > cat > .hgeol <<EOF > [repository] > native = $1 > [patterns] > mixed.txt = BIN > **.txt = native > EOF > printf "first${EOL}second${EOL}third${EOL}" > a.txt > hg commit --addremove -m 'checkin' > echo > cd .. > } $ dotest () { > seteol $1 > echo "% hg clone repo repo-$1" > hg clone --noupdate repo repo-$1 > cd repo-$1 > cat > .hg/hgrc <<EOF > [extensions] > eol = > [eol] > native = $1 > EOF > hg update > echo '% a.txt' > cat a.txt > echo '% hg cat a.txt' > hg cat a.txt > printf "fourth${EOL}" >> a.txt > echo '% a.txt' > cat a.txt > hg diff > $PYTHON ../switch-eol.py $1 a.txt > echo '% hg diff only reports a single changed line:' > hg diff > echo "% reverting back to $1 format" > hg revert a.txt > cat a.txt > printf "first\r\nsecond\n" > mixed.txt > hg add mixed.txt > echo "% hg commit of inconsistent .txt file marked as binary (should work)" > hg commit -m 'binary file' > echo "% hg commit of inconsistent .txt file marked as native (should fail)" > printf "first\nsecond\r\nthird\nfourth\r\n" > a.txt > hg commit -m 'inconsistent file' > echo "% hg commit --config eol.only-consistent=False (should work)" > hg commit --config eol.only-consistent=False -m 'inconsistent file' > echo "% hg commit of binary .txt file marked as native (binary files always okay)" > printf "first${EOL}\0${EOL}third${EOL}" > a.txt > hg commit -m 'binary file' > cd .. > rm -r repo-$1 > } $ makemixedrepo () { > echo > echo "# setup $1 repository" > hg init mixed > cd mixed > printf "foo\r\nbar\r\nbaz\r\n" > win.txt > printf "foo\nbar\nbaz\n" > unix.txt > #printf "foo\r\nbar\nbaz\r\n" > mixed.txt > hg commit --addremove -m 'created mixed files' > echo "# setting repository-native EOLs to $1" > cat > .hgeol <<EOF > [repository] > native = $1 > [patterns] > **.txt = native > EOF > hg commit --addremove -m 'added .hgeol' > cd .. > } $ testmixed () { > echo > echo "% hg clone mixed mixed-$1" > hg clone mixed mixed-$1 > cd mixed-$1 > echo '% hg status (eol extension not yet activated)' > hg status > cat > .hg/hgrc <<EOF > [extensions] > eol = > [eol] > native = $1 > EOF > echo '% hg status (eol activated)' > hg status > echo '% hg commit' > hg commit -m 'synchronized EOLs' > echo '% hg status' > hg status > cd .. > rm -r mixed-$1 > } Basic tests $ makerepo LF % setup LF repository adding .hgeol adding a.txt $ dotest LF % hg clone repo repo-LF 2 files updated, 0 files merged, 0 files removed, 0 files unresolved % a.txt first second third % hg cat a.txt first second third % a.txt first second third fourth diff --git a/a.txt b/a.txt --- a/a.txt +++ b/a.txt @@ -1,3 +1,4 @@ first second third +fourth % switching encoding from '\n' to '\r\n' % hg diff only reports a single changed line: diff --git a/a.txt b/a.txt --- a/a.txt +++ b/a.txt @@ -1,3 +1,4 @@ first second third +fourth % reverting back to LF format first second third % hg commit of inconsistent .txt file marked as binary (should work) % hg commit of inconsistent .txt file marked as native (should fail) abort: inconsistent newline style in a.txt % hg commit --config eol.only-consistent=False (should work) % hg commit of binary .txt file marked as native (binary files always okay) $ dotest CRLF % hg clone repo repo-CRLF 2 files updated, 0 files merged, 0 files removed, 0 files unresolved % a.txt first\r (esc) second\r (esc) third\r (esc) % hg cat a.txt first second third % a.txt first\r (esc) second\r (esc) third\r (esc) fourth\r (esc) diff --git a/a.txt b/a.txt --- a/a.txt +++ b/a.txt @@ -1,3 +1,4 @@ first second third +fourth % switching encoding from '\r\n' to '\n' % hg diff only reports a single changed line: diff --git a/a.txt b/a.txt --- a/a.txt +++ b/a.txt @@ -1,3 +1,4 @@ first second third +fourth % reverting back to CRLF format first\r (esc) second\r (esc) third\r (esc) % hg commit of inconsistent .txt file marked as binary (should work) % hg commit of inconsistent .txt file marked as native (should fail) abort: inconsistent newline style in a.txt % hg commit --config eol.only-consistent=False (should work) % hg commit of binary .txt file marked as native (binary files always okay) $ rm -r repo $ makerepo CRLF % setup CRLF repository adding .hgeol adding a.txt $ dotest LF % hg clone repo repo-LF 2 files updated, 0 files merged, 0 files removed, 0 files unresolved % a.txt first second third % hg cat a.txt first\r (esc) second\r (esc) third\r (esc) % a.txt first second third fourth diff --git a/a.txt b/a.txt --- a/a.txt +++ b/a.txt @@ -1,3 +1,4 @@ first\r (esc) second\r (esc) third\r (esc) +fourth\r (esc) % switching encoding from '\n' to '\r\n' % hg diff only reports a single changed line: diff --git a/a.txt b/a.txt --- a/a.txt +++ b/a.txt @@ -1,3 +1,4 @@ first\r (esc) second\r (esc) third\r (esc) +fourth\r (esc) % reverting back to LF format first second third % hg commit of inconsistent .txt file marked as binary (should work) % hg commit of inconsistent .txt file marked as native (should fail) abort: inconsistent newline style in a.txt % hg commit --config eol.only-consistent=False (should work) % hg commit of binary .txt file marked as native (binary files always okay) $ dotest CRLF % hg clone repo repo-CRLF 2 files updated, 0 files merged, 0 files removed, 0 files unresolved % a.txt first\r (esc) second\r (esc) third\r (esc) % hg cat a.txt first\r (esc) second\r (esc) third\r (esc) % a.txt first\r (esc) second\r (esc) third\r (esc) fourth\r (esc) diff --git a/a.txt b/a.txt --- a/a.txt +++ b/a.txt @@ -1,3 +1,4 @@ first\r (esc) second\r (esc) third\r (esc) +fourth\r (esc) % switching encoding from '\r\n' to '\n' % hg diff only reports a single changed line: diff --git a/a.txt b/a.txt --- a/a.txt +++ b/a.txt @@ -1,3 +1,4 @@ first\r (esc) second\r (esc) third\r (esc) +fourth\r (esc) % reverting back to CRLF format first\r (esc) second\r (esc) third\r (esc) % hg commit of inconsistent .txt file marked as binary (should work) % hg commit of inconsistent .txt file marked as native (should fail) abort: inconsistent newline style in a.txt % hg commit --config eol.only-consistent=False (should work) % hg commit of binary .txt file marked as native (binary files always okay) $ rm -r repo Mixed tests $ makemixedrepo LF # setup LF repository adding unix.txt adding win.txt # setting repository-native EOLs to LF adding .hgeol $ testmixed LF % hg clone mixed mixed-LF updating to branch default 3 files updated, 0 files merged, 0 files removed, 0 files unresolved % hg status (eol extension not yet activated) % hg status (eol activated) M win.txt % hg commit % hg status $ testmixed CRLF % hg clone mixed mixed-CRLF updating to branch default 3 files updated, 0 files merged, 0 files removed, 0 files unresolved % hg status (eol extension not yet activated) % hg status (eol activated) M win.txt % hg commit % hg status $ rm -r mixed $ makemixedrepo CRLF # setup CRLF repository adding unix.txt adding win.txt # setting repository-native EOLs to CRLF adding .hgeol $ testmixed LF % hg clone mixed mixed-LF updating to branch default 3 files updated, 0 files merged, 0 files removed, 0 files unresolved % hg status (eol extension not yet activated) % hg status (eol activated) M unix.txt % hg commit % hg status $ testmixed CRLF % hg clone mixed mixed-CRLF updating to branch default 3 files updated, 0 files merged, 0 files removed, 0 files unresolved % hg status (eol extension not yet activated) % hg status (eol activated) M unix.txt % hg commit % hg status $ rm -r mixed $ echo '[extensions]' >> $HGRCPATH $ echo 'eol =' >> $HGRCPATH #if unix-permissions Test issue2569 -- eol extension takes write lock on reading: $ hg init repo $ cd repo $ touch .hgeol $ hg status ? .hgeol $ chmod -R -w .hg $ sleep 1 $ touch .hgeol $ hg status --traceback ? .hgeol $ chmod -R u+w .hg $ cd .. #endif Test cleverencode: and cleverdecode: aliases for win32text extension $ cat <<EOF >> $HGRCPATH > [encode] > **.txt = cleverencode: > [decode] > **.txt = cleverdecode: > EOF $ hg init win32compat $ cd win32compat $ printf "foo\r\nbar\r\nbaz\r\n" > win.txt $ printf "foo\nbar\nbaz\n" > unix.txt $ hg add adding unix.txt adding win.txt $ hg commit -m checkin Check that both files have LF line-endings in the repository: $ hg cat win.txt foo bar baz $ hg cat unix.txt foo bar baz Test handling of a broken .hgeol file: $ touch .hgeol $ hg add .hgeol $ hg commit -m 'clean version' $ echo "bad" > .hgeol $ hg status warning: ignoring .hgeol file due to parse error at .hgeol:1: bad M .hgeol $ hg revert .hgeol warning: ignoring .hgeol file due to parse error at .hgeol:1: bad $ hg status ? .hgeol.orig Test eol.only-consistent can be specified in .hgeol $ cd $TESTTMP $ hg init only-consistent $ cd only-consistent $ printf "first\nsecond\r\n" > a.txt $ hg add a.txt $ cat > .hgeol << EOF > [eol] > only-consistent = True > EOF $ hg commit -m 'inconsistent' abort: inconsistent newline style in a.txt [255] $ cat > .hgeol << EOF > [eol] > only-consistent = False > EOF $ hg commit -m 'consistent' $ hg init subrepo $ hg -R subrepo pull -qu . $ echo "subrepo = subrepo" > .hgsub $ hg ci -Am "add subrepo" adding .hgeol adding .hgsub $ hg archive -S ../archive $ find ../archive/* | sort ../archive/a.txt ../archive/subrepo ../archive/subrepo/a.txt $ cat ../archive/a.txt ../archive/subrepo/a.txt first\r (esc) second\r (esc) first\r (esc) second\r (esc) Test trailing newline $ cat >> $HGRCPATH <<EOF > [extensions] > eol= > EOF setup repository $ cd $TESTTMP $ hg init trailing $ cd trailing $ cat > .hgeol <<EOF > [patterns] > **.txt = native > [eol] > fix-trailing-newline = False > EOF add text without trailing newline $ printf "first\nsecond" > a.txt $ hg commit --addremove -m 'checking in' adding .hgeol adding a.txt $ rm a.txt $ hg update -C -q $ cat a.txt first second (no-eol) $ cat > .hgeol <<EOF > [patterns] > **.txt = native > [eol] > fix-trailing-newline = True > EOF $ printf "third\nfourth" > a.txt $ hg commit -m 'checking in with newline fix' $ rm a.txt $ hg update -C -q $ cat a.txt third fourth append a line without trailing newline $ printf "fifth" >> a.txt $ hg commit -m 'adding another line line' $ rm a.txt $ hg update -C -q $ cat a.txt third fourth fifth amend of changesets with renamed/deleted files expose new code paths $ hg mv a.txt b.txt $ hg ci --amend -q $ hg diff -c. diff --git a/a.txt b/b.txt rename from a.txt rename to b.txt --- a/a.txt +++ b/b.txt @@ -1,2 +1,3 @@ third fourth +fifth $ cd ..