4956: rbt patch fails with multiple commits in mercurial because mercurial won't patch a dirty working copy

grim

What version are you running?

2.0.1

What steps will reproduce the problem?

This came from a contributor, so it might be a bit hard to follow but I've ran into this myself.

  1. Post a review request with multiple commits
  2. Go to another machine and rbt patch XXXX
  3. Watch hg patch explode on the second commit

What is the expected output? What do you see instead?

Expected output is a successful patch. An error about failing to patch.

What operating system are you using?

Happens on linux and dinwo.

Attach the debug out from the command.

>>> RBTools 2.0.1
>>> Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0]
>>> Running on Linux-5.4.0-94-generic-x86_64-with-glibc2.29
>>> Home = /home/ivanhoe
>>> Current directory = /home/ivanhoe/devel/pidgin/pidgin/subprojects/gplugin
>>> Command line: rbt patch -d 1229
>>> Running: tf vc help
>>> Checking for a Mercurial repository...
>>> Running: hg showconfig
>>> Using candidate path 'default': 'https://keep.imfreedom.org/gplugin/gplugin'
>>> Repository info: Path: https://keep.imfreedom.org/gplugin/gplugin, Base path: , Supports changesets: False
>>> Making HTTP GET request to https://reviews.imfreedom.org/api/
>>> Running: hg status --modified --added --removed --deleted
WARNING: Working directory is not clean.
>>> Making HTTP GET request to https://reviews.imfreedom.org/api/review-requests/1229/diffs/?only-fields=&only-links=
>>> HTTP GET request to https://reviews.imfreedom.org/api/review-requests/1229/diffs/?only-fields=&only-links= cannot be cached
>>> Making HTTP GET request to https://reviews.imfreedom.org/api/review-requests/1229/diffs/3/?expand=commits
>>> HTTP GET request to https://reviews.imfreedom.org/api/review-requests/1229/diffs/3/?expand=commits cannot be cached
>>> Making HTTP GET request to https://reviews.imfreedom.org/api/review-requests/1229/diffs/3/commits/106bbe2696333f5af2a1e3d88798ecb4e4c7c993/
>>> HTTP GET request to https://reviews.imfreedom.org/api/review-requests/1229/diffs/3/commits/106bbe2696333f5af2a1e3d88798ecb4e4c7c993/ cannot be cached
>>> Making HTTP GET request to https://reviews.imfreedom.org/api/review-requests/1229/diffs/3/commits/8280699f8e74def7e6ca04a0d75f848b4b7a4f5b/
>>> HTTP GET request to https://reviews.imfreedom.org/api/review-requests/1229/diffs/3/commits/8280699f8e74def7e6ca04a0d75f848b4b7a4f5b/ cannot be cached
Applying 2 patches from review request 1229 (diff revision 3)
Applying patch 1/2...
>>> Running: hg parents --hidden -r 0
>>> Running: hg patch --no-commit /tmp/rbtools.621999nz --config extensions.rbtoolsnormalize=/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/helpers/hgext.py
>>> Command exited with rc 255: ['hg', 'patch', '--no-commit', '/tmp/rbtools.621999nz', '--config', 'extensions.rbtoolsnormalize=/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/helpers/hgext.py']
b'abort: uncommitted changes\n'---
Traceback (most recent call last):
  File "/home/ivanhoe/.local/bin/rbt", line 8, in <module>
    sys.exit(main())
  File "/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/commands/main.py", line 124, in main
    command.run_from_argv([RB_MAIN, command_name] + args)
  File "/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/commands/__init__.py", line 770, in run_from_argv
    exit_code = self.main(*args) or 0
  File "/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/commands/patch.py", line 519, in main
    self._apply_patches(patches)
  File "/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/commands/patch.py", line 614, in _apply_patches
    success = self.apply_patch(
  File "/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/commands/patch.py", line 345, in apply_patch
    result = self._tool.apply_patch(
  File "/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/clients/mercurial.py", line 1242, in apply_patch
    rc, data = self._execute(cmd, with_errors=True, return_error_code=True,
  File "/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/clients/mercurial.py", line 1191, in _execute
    return execute(cmd, *args, **kwargs)
  File "/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/utils/process.py", line 189, in execute
    raise Exception('Failed to execute command: %s' % command)
Exception: Failed to execute command: ['hg', 'patch', '--no-commit', '/tmp/rbtools.621999nz', '--config', 'extensions.rbtoolsnormalize=/home/ivanhoe/.local/lib/python3.8/site-packages/rbtools/helpers/hgext.py']

Please provide any additional information below.

As you can see in the above output, it's Mercurial that's aborting.

b'abort: uncommitted changes\n'---

We temporarily worked around the issue by updating rbtools/clients/mercurial.py to add the -f command line option in def apply_patch to this line cmd = [self._exe, 'patch', '--no-commit'].

At some point the -f argument to hg patch was deprecated. I'm not sure when or why and didn't ask the Mercurial team yet but this appears to work fine for now.