Fossil Forum

Wishlist: Allow --context 0 (again?)
Login

Wishlist: Allow --context 0 (again?)

Wishlist: Allow --context 0 (again?)

(1) By Bernd Feige (berndfeige) on 2024-02-04 10:30:07 [source]

I started using https://github.com/mhinz/vim-signify with fossil - Signify is one of few VCS vim bindings including fossil support.

To correctly show changed lines, Signify uses 'fossil diff --unified -c 0 -- %f'. However, the result is wrong as "-c 0" is just ignored.

Looking at the code (src/diff.c), at line 3152, "if( (z = find_option("context","c",1))!=0 && (f = atoi(z))!=0 )" causes the option with argument 0 to be ignored altogether.

The following code sets the flag DIFF_CONTEXT_EX which would make the function diff_context_lines() *not* ignore nContext==0 - however, this code is not reached in this exact case. So DIFF_CONTEXT_EX, as is, has no function.

This is the small patch to make diff -c 0 work again:

Index: fossil-src-2.23/src/diff.c
===================================================================
--- fossil-src-2.23.orig/src/diff.c
+++ fossil-src-2.23/src/diff.c
@@ -3149,8 +3149,8 @@ void diff_options(DiffConfig *pCfg, int
     if( find_option("debug",0,0)!=0 ) diffFlags |= DIFF_DEBUG;
     if( find_option("raw",0,0)!=0 )   diffFlags |= DIFF_RAW;
   }
-  if( (z = find_option("context","c",1))!=0 && (f = atoi(z))!=0 ){
-    pCfg->nContext = f;
+  if( (z = find_option("context","c",1))!=0 ){
+    pCfg->nContext = atoi(z);
     diffFlags |= DIFF_CONTEXT_EX;
   }
   if( (z = find_option("width","W",1))!=0 && (f = atoi(z))>0 ){

(2) By Stephan Beal (stephan) on 2024-02-04 10:44:27 in reply to 1 [link] [source]

To correctly show changed lines, Signify uses 'fossil diff --unified -c 0 -- %f'. However, the result is wrong as "-c 0" is just ignored.

If your intent is to show an infinite number of context lines, use a negative value. If that's now your intent, what is 0 supposed to do?

Zero currently has the meaning "use the default," and changing it might break things for people.

(3) By Bernd Feige (berndfeige) on 2024-02-04 11:55:45 in reply to 2 [link] [source]

Thanks for the reply!

The intent is to show zero context lines, very simple.

I know that this would be suboptimal if the diff was used as a patch, however it is a perfectly valid request. Looking through fossil's changelog, I have seen a change like "make --context 0 work" a couple of years ago, so I am sure that it was supported at some point.

It's not clear to me why a value of 0 should be assigned the meaning "use the default" if 0 has a valid logical meaning. If it is really necessary to have an option with a value that has the same effect as if the option was not set at all (which does not need to be so since the absence of an option has informative value), then it might be better to use value -1 for that special purpose, and any other negative value meaning "infinite".

(4) By Stephan Beal (stephan) on 2024-02-04 12:14:57 in reply to 3 [link] [source]

It's not clear to me why a value of 0 should be assigned the meaning "use the default" if 0 has a valid logical meaning.

That's a fair point, but i'd argue that 0 is not a valid context value for a diff. Technically, perhaps, but not for practical purposes. Diffs are largely for human consumption, and a diff without context is extremely difficult to read (as is a diff with too much context).

If it is really necessary to have an option with a value that has the same effect as if the option was not set at all (which does not need to be so since the absence of an option has informative value), then it might be better to use value -1 for that special purpose, and any other negative value meaning "infinite".

That's not wrong, but the fact is that changing the current semantics may disrupt other users and, so far, there's no compelling reason to permit diffs with 0 context lines. If you can present a compelling reason to change it, such that 0 actually means 0, we could certainly consider changing it.

(5) By Bernd Feige (berndfeige) on 2024-02-04 12:33:01 in reply to 4 [link] [source]

Well, compelling is in the eye of the beholder :-)
It seems that a spectrum of other VCS systems does allow 0 context lines, as per this list defined in vim-signify:

let s:default_vcs_cmds = {
      \ 'git':      'git diff --no-color --no-ext-diff -U0 -- %f',
      \ 'yadm':     'yadm diff --no-color --no-ext-diff -U0 -- %f',
      \ 'hg':       'hg diff --color=never --config aliases.diff= --nodates -U0 -- %f',
      \ 'svn':      'svn diff --diff-cmd %d -x -U0 -- %f',
      \ 'bzr':      'bzr diff --using %d --diff-options=-U0 -- %f',
      \ 'darcs':    'darcs diff --no-pause-for-gui --no-unified --diff-opts=-U0 -- %f',
      \ 'fossil':   'fossil diff --unified -c 0 -- %f',
      \ 'cvs':      'cvs diff -U0 -- %f',
      \ 'rcs':      'rcsdiff -U0 %f 2>%n',
      \ 'accurev':  'accurev diff %f -- -U0',
      \ 'perforce': 'p4 info '. sy#util#shell_redirect('%n') . (has('win32') ? ' &&' : ' && env P4DIFF= P4COLORS=') .' p4 diff -du0 %f',
      \ 'tfs':      'tf diff -version:W -noprompt -format:Unified %f'
      \ }

Here, the fossil line does not work as expected. I did not check all of them but it seems that -U0 is valid in most of them.

It is true that the parser of vim-signify could be changed to deal with context lines just for fossil...

(6) By Daniel Dumitriu (danield) on 2024-02-04 12:44:00 in reply to 5 [link] [source]

FWIW, I also believe 0 has its use, as the other VCS show - I myself asked for such things earlier with hg.

I also don't think there are that many legacy scripts counting on the current behaviour, so I vote for it.

(7) By Stephan Beal (stephan) on 2024-02-04 14:15:44 in reply to 5 [link] [source]

It seems that a spectrum of other VCS systems does allow 0 context lines, as per this list defined in vim-signify:

That's a pretty compelling argument :). This change is now on the short-term TODO-list.

@All: if there are any objections to a change in behavior for (diff -c 0), please voice them. The current behavior is that 0 uses the default value (hard-coded at 5). The new behavior would be to elide all context lines.

(8) By Larry Brasfield (larrybr) on 2024-02-04 20:39:44 in reply to 7 [link] [source]

I, personally, like the logical consistency of giving the same numerical meaning to all non-negative values.

However, I doubt that anybody cares much who looks at diff outputs.

Also, as I read the allegedly compelling rationale, I was unimpressed by the argument, "Everybody else does it." I can still remember my father's stern voice when he told me, in no uncertain terms, that such justification was a poor and pathetic excuse, and why it was so poor.

That said, I support the change for a further reason: Why have a special value convention to say "take the default" for a parameter which, if left unspecified, takes a default? I can imagine this happening as an accident or quirk of coding, but I cannot fathom it as a well considered choice. (I intend no offense to whoever might have considered it so.)

(10) By Stephan Beal (stephan) on 2024-02-04 22:21:58 in reply to 8 [link] [source]

Why have a special value convention to say "take the default" for a parameter which, if left unspecified, takes a default?

Indeed.

I can imagine this happening as an accident or quirk of coding, but I cannot fathom it as a well considered choice. (I intend no offense to whoever might have considered it so.)

IIRC, when adding support for negative values the question came up of how to handle zero and literal zero made (still makes!) little sense to me in the context of a diff, so it was simply treated as the default. We can chalk it up to a lack of imagination on my part.

@Daniel: thank you for the fix.

(11) By Larry Brasfield (larrybr) on 2024-02-04 22:33:20 in reply to 10 [link] [source]

I mean this is no criticism. Saying "I cannot fathom" is what I call "a statement of ignorance".

Often, when considering details of how some user-facing feature is to work, I think about how complex it will be to describe and comprehend. This usually works to keep things as logically consistent as I can manage.

(12) By Stephan Beal (stephan) on 2024-02-04 22:46:11 in reply to 11 [link] [source]

I mean this is no criticism.

Considered criticism is always welcome.

Saying "I cannot fathom" is what I call "a statement of ignorance".

Absolutely! i've no shortage of those ;).

Often, when considering details of how some user-facing feature is to work, I think about how complex it will be to describe and comprehend.

An excellent tip, thank you.

(13) By Warren Young (wyoung) on 2024-02-04 23:15:22 in reply to 11 [link] [source]

More than once, I’ve abandoned a commit on finding myself writing a novelette to describe it. :)

(9) By Daniel Dumitriu (danield) on 2024-02-04 21:30:46 in reply to 5 [link] [source]

This is now changed on trunk.