Commit Graph

506 Commits

Author SHA1 Message Date
Siddharth Agarwal
53fe8ecbd4 patch.diff: restrict matcher to relative root in certain cases
Previously we'd request all results, then filter by relative root. This is
clearly inefficient, so we now restrict the matcher to the relative root for
certain easy cases.

The particular case here is when the matcher matches all files. In that case we
can simply create a matcher by the relative root.

This is purely an optimization and has no impact on correctness.
2015-03-17 15:46:36 -07:00
Siddharth Agarwal
633aa5fbea patch.diff: add support for diffs relative to a subdirectory
For now this implementation is pretty naive -- it filters out files right
before passing them into trydiff. In upcoming patches we'll add some more
smarts.
2015-03-17 13:41:24 -07:00
Siddharth Agarwal
1ba05f7f8a patch.trydiff: add support for stripping a relative root
This assumes that if relroot is not None, all the files in modified, added and
removed start with it. In upcoming patches we'll follow that.
2015-03-17 12:59:41 -07:00
Siddharth Agarwal
4a073b9113 patch._applydiff: resolve prefix with respect to the cwd
This has several advantages compared to resolving it relative to the root:

- '--prefix .' works as expected.
- consistent with upcoming 'hg diff' option to produce relative patches

(I made sure to put in the (glob) annotations this time!)
2015-03-19 10:18:05 -07:00
Siddharth Agarwal
400fb488cd patch.pathtransform: prepend prefix even if strip is 0
Silly oversight by me.
2015-03-18 20:59:06 -07:00
Siddharth Agarwal
598b5c20d8 patch.trydiff: add a docstring
It took me a bit to figure out what this function actually does.
2015-03-17 13:06:15 -07:00
Laurent Charignon
2716ef87d7 record: add comparison methods for recordhunk class 2015-03-13 14:08:30 -07:00
Laurent Charignon
62f1f68490 record: change interface of the filtering function
This way filtering functions accept chunks and return chunks
2015-03-12 17:51:37 -07:00
Jordi Gutiérrez Hermoso
8eb132f5ea style: kill ersatz if-else ternary operators
Although Python supports `X = Y if COND else Z`, this was only
introduced in Python 2.5. Since we have to support Python 2.4, it was
a very common thing to write instead `X = COND and Y or Z`, which is a
bit obscure at a glance. It requires some intricate knowledge of
Python to understand how to parse these one-liners.

We change instead all of these one-liners to 4-liners. This was
executed with the following perlism:

    find -name "*.py" -exec perl -pi -e 's,(\s*)([\.\w]+) = \(?(\S+)\s+and\s+(\S*)\)?\s+or\s+(\S*)$,$1if $3:\n$1    $2 = $4\n$1else:\n$1    $2 = $5,' {} \;

I tweaked the following cases from the automatic Perl output:

    prev = (parents and parents[0]) or nullid
    port = (use_ssl and 443 or 80)
    cwd = (pats and repo.getcwd()) or ''
    rename = fctx and webutil.renamelink(fctx) or []
    ctx = fctx and fctx or ctx
    self.base = (mapfile and os.path.dirname(mapfile)) or ''

I also added some newlines wherever they seemd appropriate for readability

There are probably a few ersatz ternary operators still in the code
somewhere, lurking away from the power of a simple regex.
2015-03-13 17:00:06 -04:00
Laurent Charignon
742d78e9af record: move filterpatch from record to patch
Part of a series of patches to move record from hgext to core
2015-03-10 14:42:07 -07:00
Siddharth Agarwal
8e295dc1a9 patch.internalpatch: add a default value for prefix
Rev cb4d72125aae adding a parameter here. This breaks third-party extensions
like crecord and also makes the issue fairly hard to fix on the extension's
side if it wants to retain compatibility across Mercurial versions -- in old
versions, the positional argument will be passed into the next unknown
argument, which is 'files'.

The patch also undoes a change to the record extension that is no longer
necessary.
2015-03-10 17:34:42 -07:00
Laurent Charignon
39d2901c91 record: move parsepatch from record to patch
Part of a series of patches to move record from hgext to core
2015-03-09 13:16:25 -07:00
Laurent Charignon
18e7b1d4da record: move scanpatch from record to patch
Part of a series of patches to move record from hgext to core
2015-03-09 13:14:31 -07:00
Laurent Charignon
b8126743d5 record: move hunk class from record to patch
Part of a series of patches to move record from hgext to core
2015-03-09 13:09:15 -07:00
Laurent Charignon
3b17682474 record: move header class from record to patch
Part of a series of patches to move record from hgext to core
2015-03-09 13:04:50 -07:00
Siddharth Agarwal
eb2ddaf931 cmdutil.tryimportone: allow importing relative patches with --bypass 2015-03-10 13:06:38 -07:00
Siddharth Agarwal
3f34a651ac cmdutil.tryimportone: allow importing relative patches into the working dir
This makes hg import --prefix dir/ (without bypass) work.
2015-03-09 18:22:24 -07:00
Siddharth Agarwal
f5f1d70d92 patch.internalpatch: accept a prefix parameter 2015-03-06 22:43:47 -08:00
Siddharth Agarwal
1cc108f3fe patch.patchbackend: accept a prefix parameter 2015-03-06 22:27:41 -08:00
Siddharth Agarwal
7ebeb5b176 patch.applydiff: accept a prefix parameter
This is preparation for upcoming patches that will add support for applying a
patch within a subdirectory.
2015-03-06 22:24:49 -08:00
Siddharth Agarwal
3ad52d447e patch._applydiff: accept a prefix parameter
This is preparation for upcoming patches that will add support for applying a
patch within a subdirectory.

We normalize the prefix here because this is the main driver -- all code to
apply patches is expected to go through here.
2015-03-06 22:22:14 -08:00
Siddharth Agarwal
2adfba3ca4 patch.makepatchmeta: accept a prefix parameter
This is preparation for upcoming patches that will add support for applying a
patch within a subdirectory.
2015-03-06 22:19:26 -08:00
Siddharth Agarwal
1f6f9200c0 patch.pathtransform: add a prefix parameter
This is preparation for upcoming patches that will add support for applying a
patch within a subdirectory.

The prefix is applied after path components are stripped.
2015-03-06 22:17:24 -08:00
Siddharth Agarwal
3aa3a9554f patch.pathtransform: add doctests
In upcoming patches we're going to make this function more complex, so add some
unit tests for it.
2015-03-06 21:48:40 -08:00
Siddharth Agarwal
b529f117b7 patch: rename pathstrip to pathtransform
In upcoming patches we'll not just strip path components but also add new ones.
2015-03-06 21:12:30 -08:00
Martin von Zweigbergk
c2aa9ed527 trydiff: transpose 'if opts.git or losedatafn' with 'if f[12]'
Perhaps it's more readable this way...
2015-02-06 16:09:43 -08:00
Martin von Zweigbergk
f2001609be trydiff: extract function that generates filename pairs
The code that identifies copies/renames, as well as the filenames
before and after, is now isolated and we can extract it to a function
so it can be overridden by extensions (in particular the narrow clone
extension).
2015-01-22 23:29:00 -08:00
Martin von Zweigbergk
8e7eeafaf5 trydiff: read file data in only one place
This moves getfilectx() out of the initial block in the loop, leaving
that block to be only about finding pairs of filenames in ctx1 and
ctx2 to diff.
2015-01-22 23:18:43 -08:00
Martin von Zweigbergk
c5d80876e7 trydiff: set filename variables to None even when not opts.git or losedatafn
There is not much left of the first block "if opts.git or losedatafn"
block now. The next patch will move the call to getfilectx() out of
that block. We will then be using the defined-ness of 'f1' to tell
whether the file existed in ctx1 (and under what name). We will need
this information whether or not opts.git or losedatafn was set, so
just remove that guard. The only operation in the block that is not
cheap is the call to getfilectx(), but that has an extra 'if opts.git'
guard already.

--ignore-space-change proves that only 'if opts.git or losedatafn:'
  was removed.
2015-01-22 23:13:48 -08:00
Martin von Zweigbergk
ff03ae13d3 trydiff: read flags in one place
Now that we can trust f1/f2 to indicate whether that side of the diff
exists, we can move the calls to ctx.flag() to a single place.
2015-01-22 23:01:00 -08:00
Martin von Zweigbergk
5cac0e0289 trydiff: break 'if opts.git or losedatafn' into two
This moves the initialization 'binary' closer to its other assignment,
but exists mostly to simplify future patches.
2015-02-06 15:21:56 -08:00
Martin von Zweigbergk
03828692db trydiff: make filenames None when they don't exist
f1 and f2 are currently set always set to some filename, even for
added or deleted files. Let's instead set them to None to indicate
that one side of the diff doesn't exist. This lets us use the filename
variables instead of the content variables and simplify a bit since
the empty string is not a valid filename. More importantly, it paves
the way for further simplifications.
2015-01-22 22:42:35 -08:00
Martin von Zweigbergk
d1957c5720 trydiff: call util.binary in only one place
It's practically free to call util.binary on empty or None content. By
relying on that, we can replace the current four call sites by one.
2015-01-22 22:22:09 -08:00
Martin von Zweigbergk
24758b719a trydiff: collect all lossiness checks in one place
By having all the checks for lossiness in one place, it becomes much
easier to get an overview of the conditions that lead to losedatafn()
being called. It also makes it obvious that it can not be called
multiple times for a single time (something that was rather tricky to
determine before).
2015-01-22 21:35:57 -08:00
Martin von Zweigbergk
67c419e3d3 trydiff: replace 'binarydiff' variable by 'binary' variable
It's not obvious, but every path in the 'if opts.git or losedatafn:'
block will have checked whether the file is binary [1]. Let's assign
the result of this check to a variable so we can simplify by checking
'binary and opts.git' in only one place instead of every place we
currently assign to 'binarydiff'.

 [1] Except when deleting an empty file, but checking whether an empty
     string is binary is very cheap anyway.
2015-01-22 21:03:57 -08:00
Martin von Zweigbergk
b7bbf4a4e7 trydiff: rename 'op' to make it more specific
Rename the 'op' variable that can take values None/'copy'/'rename' to
'copyop' to make it a little more specific.
2015-01-16 15:09:21 -08:00
Martin von Zweigbergk
1473f44d5f trydiff: join elements in 'header' list by '\n'
It seems natural that each element in the list corresponds to one line
of output. That is currently true, but only because each element in
the list has a trailing newline. Let's drop those newlines and instead
add them when we print the headers.
2015-01-16 15:34:03 -08:00
Martin von Zweigbergk
5210611f2a trydiff: move check for quietness out of diffline()
By moving the condition out of diffline(), the call site becomes
clearer and diffline() no longer closes on any variables.

Note that this changes the value of the header variable from [''] to
[], but there is no difference in how these two are treated by the
following code. The new value seems more natural anyway.
2015-01-16 15:27:04 -08:00
Martin von Zweigbergk
d00ce0b1fe trydiff: remove dead branch in diffline()
Since diffline() is never called when 'revs' is empty, it doesn't need
to handle that case.
2015-01-16 15:22:29 -08:00
Martin von Zweigbergk
a904d02975 trydiff: make 'revs' ignored if opts.git is set
Instead of setting revs=None to prevent the call to diffline() when
opts.git is set, explicitly do not call the function in the git case.
2015-01-16 15:19:57 -08:00
Martin von Zweigbergk
06e8c1de95 trydiff: remove unused argument to diffline()
Now that diffline no longer knows about copies/renames, it only needs
one argument for the path.
2015-01-16 14:58:51 -08:00
Martin von Zweigbergk
2a62729249 trydiff: move git-header code out of diffline function
This collects more of the code for writing git headers in a single
place and makes diffline() close on a few variables less.
2015-01-16 14:58:06 -08:00
Martin von Zweigbergk
13e9d82723 trydiff: inline indexmeta()
The function is trivial and is only called in one place.
2015-01-16 15:40:59 -08:00
Martin von Zweigbergk
18e0f2df1e trydiff: order header-writing code in same order as output
Instead of inserting a line before the others header lines, move the
code that writes that line before the code that writes the other
lines.
2015-01-16 14:49:08 -08:00
Martin von Zweigbergk
e67ec1dc7b trydiff: inline sole addmodehdr() call
Now that there is only a single call to addmodehdr() left, and there
is other similar code (for new/deleted files) around that call site,
let's inline the function there. That also makes it clearer under what
circumstances the header is actually written (when modes differ).
2015-01-16 14:53:37 -08:00
Martin von Zweigbergk
26f9ad4736 trydiff: join filename with prefix only once 2015-01-16 14:46:03 -08:00
Martin von Zweigbergk
fa3ad9c66e trydiff: collect header-writing in one place
This is the first step towards simplifying the big loop in
trydiff(). This will make both the header code and the non-header code
clearer, and it prepares for further simplification of the many nested
if-statements in the body of the loop.
2015-01-22 17:00:54 -08:00
Martin von Zweigbergk
2cda80f871 trydiff: make variable names more consistent
Use '1' and '2' as suffix for names just like in the parameters
'ctx[12]':

to,tn -> content1,content2
a,b -> f1, f2
omode,mode -> mode1,mode2
omode,nmode -> mode1,mode2
onode,nnode -> node1,node2
oflag,nflag -> flag1,flag2
oindex,nindex -> index1,index2
2015-01-16 10:57:13 -08:00
Martin von Zweigbergk
20500e2442 trydiff: check only if added file is a copy target, not source
When creating a diff with copy/rename enabled, we consider added files
and check if they are either copy sources or targets. However, an
added file should never be a copy source. The test suite seems to
agree with this: all tests pass if we raise an exception when an added
file is a copy source. So, let's simplify the code by dropping the
conditions that are never true.

For those interested in the historical reasons:

Before commit c15c00e7afba (patch: separate reverse copy data
(issue1959), 2010-02-11), 'copy' seems to have been a bidirectional
map. Then that commit split it up into two unidirectional maps and
duplicated the logic to look in both maps. It was still needed at that
point to look in both maps, as the copy detection was poor and could
sometimes be reported in reverse.

A little later came 5a644704d5eb (copies: rewrite copy detection for
non-merge users, 2012-01-04). That commit fixed the copy detection to
be backwards when it should, and made the hacks in trydiff
unnecessary.
2015-01-16 17:01:58 -08:00
Martin von Zweigbergk
74a4151049 trydiff: remove unused variable 'modifiedset'
We started updating 'modifiedset' in e85e48ffb83e (trydiff: simplify
checking for additions, 2014-12-23) but in the same commit, we removed
the last use of the variable. Clean it up.
2015-01-29 21:12:35 -08:00