Re: [PATCH 4 of 4 v3] log: add -L/--line-range option to follow file history by line range

2017-10-17 Thread Augie Fackler

> On Oct 17, 2017, at 09:28, Yuya Nishihara  wrote:
> 
 If we take the latter, a plain file pattern can be used to select all 
 lines.
 
  hg log --follow -L file.c,13-23 main.c
>>> 
>>> I feel like the -L file.c,13-23 is the least ambiguous option in terms of 
>>> understanding what a command *does*, and probably also the easiest to 
>>> actually implement with our command line parser. The others feel clever in 
>>> a way that kind of makes me nervous.
> 
> FWIW, I lean towards (1) -L file.c,13-23 syntax, but taking -L as the flag
> to just attach linerange information, not the flag to enable followlines.
> 
>  hg log --follow -L FILE_A,LINERANGE_A FILE_B
> 
> will follow LINERANGE_A in FILE_A and the whole lines in FILE_B.

That looks reasonable to me. Why don't we add the -L flag as (EXPERIMENTAL) in 
4.4, and maybe that'll inspire some more feedback on the CLI UX?
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH 4 of 4 v3] log: add -L/--line-range option to follow file history by line range

2017-10-17 Thread Yuya Nishihara
On Mon, 16 Oct 2017 20:56:29 +0200, Denis Laxalde wrote:
> Augie Fackler a écrit :
> > (+jordigh, themystic, indygreg in case one of them has an opinion or 
> > inspiration)
> > 
> >> On Oct 13, 2017, at 10:24 AM, Yuya Nishihara  wrote:
> >>
> >> On Fri, 13 Oct 2017 10:02:07 +0200, Denis Laxalde wrote:
> >>> Yuya Nishihara a écrit :
>  On Tue, 10 Oct 2017 17:37:27 +0200, Denis Laxalde wrote:
> > # HG changeset patch
> > # User Denis Laxalde 
> > # Date 1507290475 -7200
> > #  Fri Oct 06 13:47:55 2017 +0200
> > # Node ID a05d3b45319a9ec28205f19dd7012b206a2b200f
> > # Parent  86a055d1c06f55daeb5d725187b61522974d24e3
> > # Available At http://hg.logilab.org/users/dlaxalde/hg
> > #  hg pull http://hg.logilab.org/users/dlaxalde/hg -r 
> > a05d3b45319a
> > # EXP-Topic followlines-cli/v2
> > log: add -L/--line-range option to follow file history by line range
> 
>  The series generally looks good to me in functionality POV. Some nits 
>  follow.
> >>>
> >>> Nits addressed in v4 which I can send now or once we agree on the UI.
> >>>
> 
>  So, do we really like this UI?
> >>>
> >>> For the record, there are currently two proposals:
> >>>
> >>> 1. The one implemented in this patch that adds a -L option to specify
> >>> both the file and its line range:
> >>>
> >>>   hg log -L file.c,13-23 -L main.c,2-6
> >>>
> >>> 2. The idea by Yuya to have pairs of -L FROMLINE-TOLINE options and
> >>> regular FILE arguments
> >>>
> >>>   hg log -L 13-23 file.c -L 2-6 main.c
> >>>
> >>> The issue with this one (as explained in [1]) is that option parsing
> >>> would not be strict, meaning that (IIUC) we would allow:
> >>>
> >>>   hg log -L 13-23 -L 2-6 file.c main.c
> >>>
> >>> to work the same as the previous example.
> >>
> >> Thanks for the great summary. Some other crazy proposals just came up:
> >>
> >> 3. Extend the fileset syntax
> >>
> >>   hg log set:file.c@13-23 set:main.c@2-6
> >>
> >> but we have to always type 'set:'
> >>
> >> 4. Reuse the matcher kind to carry linerange in pats
> >>
> >>   hg log file.c L:13-23 main.c L:2-6
> >>
> >> linerange: and L: are preprocessed to be paired with the previous file
> >> pattern.
> >>
> >> Another UI concern is whether --follow should be implied or required:
> >>
> >>   hg log -L file.c,13-23  (implies --follow)
> >>
> >> or
> >>
> >>   hg log -L file.c,13-23  (abort)
> >>   hg log --follow -L file.c,13-23
> >>
> >> If we take the latter, a plain file pattern can be used to select all 
> >> lines.
> >>
> >>   hg log --follow -L file.c,13-23 main.c
> > 
> > I feel like the -L file.c,13-23 is the least ambiguous option in terms of 
> > understanding what a command *does*, and probably also the easiest to 
> > actually implement with our command line parser. The others feel clever in 
> > a way that kind of makes me nervous.

FWIW, I lean towards (1) -L file.c,13-23 syntax, but taking -L as the flag
to just attach linerange information, not the flag to enable followlines.

  hg log --follow -L FILE_A,LINERANGE_A FILE_B

will follow LINERANGE_A in FILE_A and the whole lines in FILE_B.

> > I guess that’s to say, +0 on that version, and I think my second choice is 
> > the fileset syntax option? None of these enthuse me, but I’d be 
> > enthusiastic about landing the feature *somehow*. I’m hesitant to not ship 
> > this series with 4.4, as it seems like this is just complicated enough 
> > we’ll have to do something that’s a compromise no matter what to make it 
> > work on the command line.
> > 
> 
> So, what should I do to move forward? I'd really like to get this in for
> 4.4. I'm happy to change anything, but need something actionable :)

I honestly don't want this "in" 4.4 as a stable option because log is one of
the most important command. Maybe it could be hidden by (EXPERIMENTAL) flag
if we really need to ship it with 4.4.

It's sad no one seems to be interested in this UI.
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH 4 of 4 v3] log: add -L/--line-range option to follow file history by line range

2017-10-16 Thread Denis Laxalde

Augie Fackler a écrit :

(+jordigh, themystic, indygreg in case one of them has an opinion or 
inspiration)


On Oct 13, 2017, at 10:24 AM, Yuya Nishihara  wrote:

On Fri, 13 Oct 2017 10:02:07 +0200, Denis Laxalde wrote:

Yuya Nishihara a écrit :

On Tue, 10 Oct 2017 17:37:27 +0200, Denis Laxalde wrote:

# HG changeset patch
# User Denis Laxalde 
# Date 1507290475 -7200
#  Fri Oct 06 13:47:55 2017 +0200
# Node ID a05d3b45319a9ec28205f19dd7012b206a2b200f
# Parent  86a055d1c06f55daeb5d725187b61522974d24e3
# Available At http://hg.logilab.org/users/dlaxalde/hg
#  hg pull http://hg.logilab.org/users/dlaxalde/hg -r a05d3b45319a
# EXP-Topic followlines-cli/v2
log: add -L/--line-range option to follow file history by line range


The series generally looks good to me in functionality POV. Some nits follow.


Nits addressed in v4 which I can send now or once we agree on the UI.



So, do we really like this UI?


For the record, there are currently two proposals:

1. The one implemented in this patch that adds a -L option to specify
both the file and its line range:

  hg log -L file.c,13-23 -L main.c,2-6

2. The idea by Yuya to have pairs of -L FROMLINE-TOLINE options and
regular FILE arguments

  hg log -L 13-23 file.c -L 2-6 main.c

The issue with this one (as explained in [1]) is that option parsing
would not be strict, meaning that (IIUC) we would allow:

  hg log -L 13-23 -L 2-6 file.c main.c

to work the same as the previous example.


Thanks for the great summary. Some other crazy proposals just came up:

3. Extend the fileset syntax

  hg log set:file.c@13-23 set:main.c@2-6

but we have to always type 'set:'

4. Reuse the matcher kind to carry linerange in pats

  hg log file.c L:13-23 main.c L:2-6

linerange: and L: are preprocessed to be paired with the previous file
pattern.

Another UI concern is whether --follow should be implied or required:

  hg log -L file.c,13-23  (implies --follow)

or

  hg log -L file.c,13-23  (abort)
  hg log --follow -L file.c,13-23

If we take the latter, a plain file pattern can be used to select all lines.

  hg log --follow -L file.c,13-23 main.c


I feel like the -L file.c,13-23 is the least ambiguous option in terms of 
understanding what a command *does*, and probably also the easiest to actually 
implement with our command line parser. The others feel clever in a way that 
kind of makes me nervous.

I guess that’s to say, +0 on that version, and I think my second choice is the 
fileset syntax option? None of these enthuse me, but I’d be enthusiastic about 
landing the feature *somehow*. I’m hesitant to not ship this series with 4.4, 
as it seems like this is just complicated enough we’ll have to do something 
that’s a compromise no matter what to make it work on the command line.



So, what should I do to move forward? I'd really like to get this in for 
4.4. I'm happy to change anything, but need something actionable :)

___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH 4 of 4 v3] log: add -L/--line-range option to follow file history by line range

2017-10-13 Thread Augie Fackler
(+jordigh, themystic, indygreg in case one of them has an opinion or 
inspiration)

> On Oct 13, 2017, at 10:24 AM, Yuya Nishihara  wrote:
> 
> On Fri, 13 Oct 2017 10:02:07 +0200, Denis Laxalde wrote:
>> Yuya Nishihara a écrit :
>>> On Tue, 10 Oct 2017 17:37:27 +0200, Denis Laxalde wrote:
 # HG changeset patch
 # User Denis Laxalde 
 # Date 1507290475 -7200
 #  Fri Oct 06 13:47:55 2017 +0200
 # Node ID a05d3b45319a9ec28205f19dd7012b206a2b200f
 # Parent  86a055d1c06f55daeb5d725187b61522974d24e3
 # Available At http://hg.logilab.org/users/dlaxalde/hg
 #  hg pull http://hg.logilab.org/users/dlaxalde/hg -r 
 a05d3b45319a
 # EXP-Topic followlines-cli/v2
 log: add -L/--line-range option to follow file history by line range
>>> 
>>> The series generally looks good to me in functionality POV. Some nits 
>>> follow.
>> 
>> Nits addressed in v4 which I can send now or once we agree on the UI.
>> 
>>> 
>>> So, do we really like this UI?
>> 
>> For the record, there are currently two proposals:
>> 
>> 1. The one implemented in this patch that adds a -L option to specify
>>both the file and its line range:
>> 
>>  hg log -L file.c,13-23 -L main.c,2-6
>> 
>> 2. The idea by Yuya to have pairs of -L FROMLINE-TOLINE options and
>>regular FILE arguments
>> 
>>  hg log -L 13-23 file.c -L 2-6 main.c
>> 
>>The issue with this one (as explained in [1]) is that option parsing
>>would not be strict, meaning that (IIUC) we would allow:
>> 
>>  hg log -L 13-23 -L 2-6 file.c main.c
>> 
>>to work the same as the previous example.
> 
> Thanks for the great summary. Some other crazy proposals just came up:
> 
> 3. Extend the fileset syntax
> 
>  hg log set:file.c@13-23 set:main.c@2-6
> 
>but we have to always type 'set:'
> 
> 4. Reuse the matcher kind to carry linerange in pats
> 
>  hg log file.c L:13-23 main.c L:2-6
> 
>linerange: and L: are preprocessed to be paired with the previous file
>pattern.
> 
> Another UI concern is whether --follow should be implied or required:
> 
>  hg log -L file.c,13-23  (implies --follow)
> 
> or
> 
>  hg log -L file.c,13-23  (abort)
>  hg log --follow -L file.c,13-23
> 
> If we take the latter, a plain file pattern can be used to select all lines.
> 
>  hg log --follow -L file.c,13-23 main.c

I feel like the -L file.c,13-23 is the least ambiguous option in terms of 
understanding what a command *does*, and probably also the easiest to actually 
implement with our command line parser. The others feel clever in a way that 
kind of makes me nervous.

I guess that’s to say, +0 on that version, and I think my second choice is the 
fileset syntax option? None of these enthuse me, but I’d be enthusiastic about 
landing the feature *somehow*. I’m hesitant to not ship this series with 4.4, 
as it seems like this is just complicated enough we’ll have to do something 
that’s a compromise no matter what to make it work on the command line.



signature.asc
Description: Message signed with OpenPGP
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH 4 of 4 v3] log: add -L/--line-range option to follow file history by line range

2017-10-13 Thread Yuya Nishihara
On Fri, 13 Oct 2017 10:02:07 +0200, Denis Laxalde wrote:
> Yuya Nishihara a écrit :
> > On Tue, 10 Oct 2017 17:37:27 +0200, Denis Laxalde wrote:
> >> # HG changeset patch
> >> # User Denis Laxalde 
> >> # Date 1507290475 -7200
> >> #  Fri Oct 06 13:47:55 2017 +0200
> >> # Node ID a05d3b45319a9ec28205f19dd7012b206a2b200f
> >> # Parent  86a055d1c06f55daeb5d725187b61522974d24e3
> >> # Available At http://hg.logilab.org/users/dlaxalde/hg
> >> #  hg pull http://hg.logilab.org/users/dlaxalde/hg -r 
> >> a05d3b45319a
> >> # EXP-Topic followlines-cli/v2
> >> log: add -L/--line-range option to follow file history by line range
> > 
> > The series generally looks good to me in functionality POV. Some nits 
> > follow.
> 
> Nits addressed in v4 which I can send now or once we agree on the UI.
> 
> > 
> > So, do we really like this UI?
> 
> For the record, there are currently two proposals:
> 
> 1. The one implemented in this patch that adds a -L option to specify
> both the file and its line range:
> 
>   hg log -L file.c,13-23 -L main.c,2-6
> 
> 2. The idea by Yuya to have pairs of -L FROMLINE-TOLINE options and
> regular FILE arguments
> 
>   hg log -L 13-23 file.c -L 2-6 main.c
> 
> The issue with this one (as explained in [1]) is that option parsing
> would not be strict, meaning that (IIUC) we would allow:
> 
>   hg log -L 13-23 -L 2-6 file.c main.c
> 
> to work the same as the previous example.

Thanks for the great summary. Some other crazy proposals just came up:

 3. Extend the fileset syntax

  hg log set:file.c@13-23 set:main.c@2-6

but we have to always type 'set:'

 4. Reuse the matcher kind to carry linerange in pats

  hg log file.c L:13-23 main.c L:2-6

linerange: and L: are preprocessed to be paired with the previous file
pattern.

Another UI concern is whether --follow should be implied or required:

  hg log -L file.c,13-23  (implies --follow)

or

  hg log -L file.c,13-23  (abort)
  hg log --follow -L file.c,13-23

If we take the latter, a plain file pattern can be used to select all lines.

  hg log --follow -L file.c,13-23 main.c
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH 4 of 4 v3] log: add -L/--line-range option to follow file history by line range

2017-10-13 Thread Denis Laxalde

Yuya Nishihara a écrit :

On Tue, 10 Oct 2017 17:37:27 +0200, Denis Laxalde wrote:

# HG changeset patch
# User Denis Laxalde 
# Date 1507290475 -7200
#  Fri Oct 06 13:47:55 2017 +0200
# Node ID a05d3b45319a9ec28205f19dd7012b206a2b200f
# Parent  86a055d1c06f55daeb5d725187b61522974d24e3
# Available At http://hg.logilab.org/users/dlaxalde/hg
#  hg pull http://hg.logilab.org/users/dlaxalde/hg -r a05d3b45319a
# EXP-Topic followlines-cli/v2
log: add -L/--line-range option to follow file history by line range


The series generally looks good to me in functionality POV. Some nits follow.


Nits addressed in v4 which I can send now or once we agree on the UI.



So, do we really like this UI?


For the record, there are currently two proposals:

1. The one implemented in this patch that adds a -L option to specify
   both the file and its line range:

 hg log -L file.c,13-23 -L main.c,2-6

2. The idea by Yuya to have pairs of -L FROMLINE-TOLINE options and
   regular FILE arguments

 hg log -L 13-23 file.c -L 2-6 main.c

   The issue with this one (as explained in [1]) is that option parsing
   would not be strict, meaning that (IIUC) we would allow:

 hg log -L 13-23 -L 2-6 file.c main.c

   to work the same as the previous example.

[1] 
https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-October/105858.html



Then, there's the question of using "-" to specify the line range as
"FROMLINE-TOLINE". As said in the commit message quoted below, I'm not
at all attached to this syntax and would be fine with using ":" which is
more consistent with the followlines() revset syntax. Later on, I think
I'd like to add support for an offset mechanism like FROMLINE+OFFSET or
FROMLINE-OFFSET, so perhaps FROMLINE:TOLINE would be better. The reason
I did not pick this in the first place is because I thought it would be
too much ":" when used in combination with file patterns (e.g.
relpath:file.c,12:23) but maybe it's acceptable.


What do others think?



We add a -L/--line-range option to 'hg log' taking file patterns along with a
line range using the (new) FILE,FROMLINE-TOLINE syntax where FILE may be a
pattern (matching exactly one file). The resulting history is similar to what
the "followlines" revset except that, if --patch is specified, only diff hunks
within specified line range are shown (it's also more convenient to type).

Basically, this brings the CLI on par with what currently only exists in hgweb
through line selection in "file" and "annotate" views resulting in a file log
with filtered patch to only display followed line range.

The option may be specified multiple times and can be combined with --rev to
futher restrict revisions. Revisions are shown in descending order and
renames are followed (sort of implying --follow).
Only the --graph option is currently not supported.

Some debatable UI choices (I did not think too much about this for now).

*   "," as a separator between the FILE and line range information; the idea
 is to avoid confusion with file pattern syntax which uses ":".

*   "-" in the line range information may not be the best choice; in
 particular, we might want to add support for an offset +/- syntax.


Perhaps ":" would be better for consistency with the followlines() revset.


+def getloglinerangerevs(repo, userrevs, opts):
+"""Return (revs, filematcher, hunksfilter).
+
+"revs" are revisions obtained by processing "line-range" log options and
+walking block ancestors of each specified file/line-range.
+
+"filematcher(rev) -> match" is a factory function returning a match object
+for a given revision for file patterns specified in --line-range option.
+If neither --stat nor --patch options are passed, "filematcher" is None.
+
+"hunksfilter(rev) -> filterfn(fctx, hunks)" is a factory function
+returning a hunks filtering function.
+If neither --stat nor --patch options are passed, "filterhunks" is None.
+"""
+wctx = repo[None]
+
+# Two-levels map of "rev -> file ctx -> [line range]".
+linerangesbyrev = {}
+for fname, (fromline, toline) in _parselinerangelogopt(repo, opts):
+fctx = wctx.filectx(fname)
+for fctx, linerange in dagop.blockancestors(fctx, fromline, toline):
+if fctx.rev() not in userrevs:
+continue
+linerangesbyrev.setdefault(
+fctx.rev(), {}).setdefault(


I'm not sure, but it might be fctx.introrev() since this function seems quite
similar to _makefollowlogfilematcher().


+fctx, []).append(linerange)


Perhaps it's better to not cache fctx for long. IIUC, we only need
(rev or node, path).



___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH 4 of 4 v3] log: add -L/--line-range option to follow file history by line range

2017-10-11 Thread Yuya Nishihara
On Tue, 10 Oct 2017 17:37:27 +0200, Denis Laxalde wrote:
> # HG changeset patch
> # User Denis Laxalde 
> # Date 1507290475 -7200
> #  Fri Oct 06 13:47:55 2017 +0200
> # Node ID a05d3b45319a9ec28205f19dd7012b206a2b200f
> # Parent  86a055d1c06f55daeb5d725187b61522974d24e3
> # Available At http://hg.logilab.org/users/dlaxalde/hg
> #  hg pull http://hg.logilab.org/users/dlaxalde/hg -r a05d3b45319a
> # EXP-Topic followlines-cli/v2
> log: add -L/--line-range option to follow file history by line range

The series generally looks good to me in functionality POV. Some nits follow.

So, do we really like this UI?

> We add a -L/--line-range option to 'hg log' taking file patterns along with a
> line range using the (new) FILE,FROMLINE-TOLINE syntax where FILE may be a
> pattern (matching exactly one file). The resulting history is similar to what
> the "followlines" revset except that, if --patch is specified, only diff hunks
> within specified line range are shown (it's also more convenient to type).
> 
> Basically, this brings the CLI on par with what currently only exists in hgweb
> through line selection in "file" and "annotate" views resulting in a file log
> with filtered patch to only display followed line range.
> 
> The option may be specified multiple times and can be combined with --rev to
> futher restrict revisions. Revisions are shown in descending order and
> renames are followed (sort of implying --follow).
> Only the --graph option is currently not supported.
> 
> Some debatable UI choices (I did not think too much about this for now).
> 
> *   "," as a separator between the FILE and line range information; the idea
> is to avoid confusion with file pattern syntax which uses ":".
> 
> *   "-" in the line range information may not be the best choice; in
> particular, we might want to add support for an offset +/- syntax.

Perhaps ":" would be better for consistency with the followlines() revset.

> +def getloglinerangerevs(repo, userrevs, opts):
> +"""Return (revs, filematcher, hunksfilter).
> +
> +"revs" are revisions obtained by processing "line-range" log options and
> +walking block ancestors of each specified file/line-range.
> +
> +"filematcher(rev) -> match" is a factory function returning a match 
> object
> +for a given revision for file patterns specified in --line-range option.
> +If neither --stat nor --patch options are passed, "filematcher" is None.
> +
> +"hunksfilter(rev) -> filterfn(fctx, hunks)" is a factory function
> +returning a hunks filtering function.
> +If neither --stat nor --patch options are passed, "filterhunks" is None.
> +"""
> +wctx = repo[None]
> +
> +# Two-levels map of "rev -> file ctx -> [line range]".
> +linerangesbyrev = {}
> +for fname, (fromline, toline) in _parselinerangelogopt(repo, opts):
> +fctx = wctx.filectx(fname)
> +for fctx, linerange in dagop.blockancestors(fctx, fromline, toline):
> +if fctx.rev() not in userrevs:
> +continue
> +linerangesbyrev.setdefault(
> +fctx.rev(), {}).setdefault(

I'm not sure, but it might be fctx.introrev() since this function seems quite
similar to _makefollowlogfilematcher().

> +fctx, []).append(linerange)

Perhaps it's better to not cache fctx for long. IIUC, we only need
(rev or node, path).
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


[PATCH 4 of 4 v3] log: add -L/--line-range option to follow file history by line range

2017-10-10 Thread Denis Laxalde
# HG changeset patch
# User Denis Laxalde 
# Date 1507290475 -7200
#  Fri Oct 06 13:47:55 2017 +0200
# Node ID a05d3b45319a9ec28205f19dd7012b206a2b200f
# Parent  86a055d1c06f55daeb5d725187b61522974d24e3
# Available At http://hg.logilab.org/users/dlaxalde/hg
#  hg pull http://hg.logilab.org/users/dlaxalde/hg -r a05d3b45319a
# EXP-Topic followlines-cli/v2
log: add -L/--line-range option to follow file history by line range

We add a -L/--line-range option to 'hg log' taking file patterns along with a
line range using the (new) FILE,FROMLINE-TOLINE syntax where FILE may be a
pattern (matching exactly one file). The resulting history is similar to what
the "followlines" revset except that, if --patch is specified, only diff hunks
within specified line range are shown (it's also more convenient to type).

Basically, this brings the CLI on par with what currently only exists in hgweb
through line selection in "file" and "annotate" views resulting in a file log
with filtered patch to only display followed line range.

The option may be specified multiple times and can be combined with --rev to
futher restrict revisions. Revisions are shown in descending order and
renames are followed (sort of implying --follow).
Only the --graph option is currently not supported.

Some debatable UI choices (I did not think too much about this for now).

*   "," as a separator between the FILE and line range information; the idea
is to avoid confusion with file pattern syntax which uses ":".

*   "-" in the line range information may not be the best choice; in
particular, we might want to add support for an offset +/- syntax.


The implementation spreads between commands.log() and cmdutil module.
In commands.log(), the main loop may now use a "hunksfilter" factory (similar
to "filematcher") that, for a given "rev", produces a filtering function for
diff hunks for a given file context object.
The logic to build revisions from -L/--line-range options lives in
cmdutil.getloglinerangerevs() which produces "revs", "filematcher" and
"hunksfilter" information. In commands.log(), "revs" resulting from
cmdutil.getloglinerangerevs() are filtered out of revisions that do not
match the revset specified by --rev option.

.. feature::

   Add a -L/--line-range FILE,FROMLINE-TOLINE option to 'hg log' command to
   follow the history of files by line range. In combination with -p/--patch
   option, only diff hunks within specified line range will be displayed.

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -26,12 +26,14 @@ from . import (
 changelog,
 copies,
 crecord as crecordmod,
+dagop,
 dirstateguard,
 encoding,
 error,
 formatter,
 graphmod,
 match as matchmod,
+mdiff,
 obsolete,
 patch,
 pathutil,
@@ -2592,6 +2594,86 @@ def getlogrevs(repo, pats, opts):
 
 return revs, expr, filematcher
 
+def _parselinerangelogopt(repo, opts):
+"""Parse --line-range log option and return a list of tuples (filename,
+(fromline, toline)).
+"""
+linerangebyfname = []
+for pat in opts.get('line_range', []):
+try:
+pat, linerange = pat.rsplit(',', 1)
+except ValueError:
+raise error.Abort(_('malformatted line-range pattern %s') % pat)
+try:
+fromline, toline = map(int, linerange.split('-'))
+except ValueError:
+raise error.Abort(_("invalid line range for %s") % pat)
+msg = _("line range pattern '%s' must match exactly one file") % pat
+fname = scmutil.parsefollowlinespattern(repo, None, pat, msg)
+linerangebyfname.append(
+(fname, util.processlinerange(fromline, toline)))
+return linerangebyfname
+
+def getloglinerangerevs(repo, userrevs, opts):
+"""Return (revs, filematcher, hunksfilter).
+
+"revs" are revisions obtained by processing "line-range" log options and
+walking block ancestors of each specified file/line-range.
+
+"filematcher(rev) -> match" is a factory function returning a match object
+for a given revision for file patterns specified in --line-range option.
+If neither --stat nor --patch options are passed, "filematcher" is None.
+
+"hunksfilter(rev) -> filterfn(fctx, hunks)" is a factory function
+returning a hunks filtering function.
+If neither --stat nor --patch options are passed, "filterhunks" is None.
+"""
+wctx = repo[None]
+
+# Two-levels map of "rev -> file ctx -> [line range]".
+linerangesbyrev = {}
+for fname, (fromline, toline) in _parselinerangelogopt(repo, opts):
+fctx = wctx.filectx(fname)
+for fctx, linerange in dagop.blockancestors(fctx, fromline, toline):
+if fctx.rev() not in userrevs:
+continue
+linerangesbyrev.setdefault(
+fctx.rev(), {}).setdefault(
+fctx,