zsh-users
 help / color / mirror / Atom feed
* git diff HEAD - only show modified files in completion
@ 2020-10-15 22:48 Ahmad Ismail
  2020-10-16 22:16 ` Ahmad Ismail
  0 siblings, 1 reply; 8+ messages in thread
From: Ahmad Ismail @ 2020-10-15 22:48 UTC (permalink / raw)
  To: Zsh Users


[-- Attachment #1.1: Type: text/plain, Size: 361 bytes --]

Hi All,

You can see from the following screenshot that my `git ls-files -m` output
is only `book-urls.txt`. I mean it is the only modified file.
*[image: Screenshot-2020-10-16-04:40:30.png]*
Now on top of `Completing tree file` (which list all the files) I want to
place `Completing modified files`.
How can I do that?


*Thanks and Best Regards,Ahmad Ismail*

[-- Attachment #1.2: Type: text/html, Size: 1817 bytes --]

[-- Attachment #2: Screenshot-2020-10-16-04:40:30.png --]
[-- Type: image/png, Size: 87938 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: git diff HEAD - only show modified files in completion
  2020-10-15 22:48 git diff HEAD - only show modified files in completion Ahmad Ismail
@ 2020-10-16 22:16 ` Ahmad Ismail
  2020-10-17  8:33   ` Oliver Kiddle
  2020-10-17 12:28   ` Daniel Shahaf
  0 siblings, 2 replies; 8+ messages in thread
From: Ahmad Ismail @ 2020-10-16 22:16 UTC (permalink / raw)
  To: Zsh Users


[-- Attachment #1.1: Type: text/plain, Size: 1066 bytes --]

Hi All,

I think it is not showing the screenshot or something. So, I asked the
question in stackoverflow.
https://stackoverflow.com/questions/64381142/append-the-output-of-a-command-as-suggestion-to-existing-competion

I think I have to use:

_git-diff 2>/dev/null
functions[_git-diff-orig]=$functions[_git-diff]

_git-diff() {
    _git-diff-orig "$@"
    ...
}

To extend the _git-diff. However, I am not understanding what to write
there so that it shows *Completing Modified Files *which I expect to be the
output of git ls-files -m

Please help me out here.

*Thanks and Best Regards,Ahmad Ismail*


On Fri, Oct 16, 2020 at 4:48 AM Ahmad Ismail <ismail783@gmail.com> wrote:

> Hi All,
>
> You can see from the following screenshot that my `git ls-files -m` output
> is only `book-urls.txt`. I mean it is the only modified file.
> *[image: Screenshot-2020-10-16-04:40:30.png]*
> Now on top of `Completing tree file` (which list all the files) I want to
> place `Completing modified files`.
> How can I do that?
>
>
> *Thanks and Best Regards,Ahmad Ismail*
>
>
>

[-- Attachment #1.2: Type: text/html, Size: 3797 bytes --]

[-- Attachment #2: Screenshot-2020-10-16-04:40:30.png --]
[-- Type: image/png, Size: 87938 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: git diff HEAD - only show modified files in completion
  2020-10-16 22:16 ` Ahmad Ismail
@ 2020-10-17  8:33   ` Oliver Kiddle
  2020-10-17 15:24     ` Bart Schaefer
  2020-10-17 12:28   ` Daniel Shahaf
  1 sibling, 1 reply; 8+ messages in thread
From: Oliver Kiddle @ 2020-10-17  8:33 UTC (permalink / raw)
  To: Ahmad Ismail; +Cc: Zsh Users

Ahmad Ismail wrote:
> I think I have to use:

> _git-diff 2>/dev/null
> functions[_git-diff-orig]=$functions[_git-diff]
>
> _git-diff() {
>     _git-diff-orig "$@"
>     ...
> }

It looks like you've removed the stack overflow question already so
I'm somewhat guessing at what your issue is. Mostly, you need to move
your own code up before calling _git-diff-orig. The tag-ordering styles
can only work when there is a single tag loop in which completions are
added.

In this case, it's probably easier to tweak the original _git-diff
function than to modify it from a wrapper. And using git diff HEAD or
git diff @ to get both staged and unstaged changes is a common enough
case to warrant being special-cased. So any thoughts on the following
patch for _git. Ideally it ought to be possible to correctly handle any
arbitrary base branch name but we have existing functions for taking
care of files modified relative to the HEAD.

Admittedly, __git_changed_files itself could be improved but that's a
separate issue.

Oliver

diff --git a/Completion/Unix/Command/_git b/Completion/Unix/Command/_git
index 05e2a2361..81a060e4d 100644
--- a/Completion/Unix/Command/_git
+++ b/Completion/Unix/Command/_git
@@ -806,11 +806,14 @@ _git-diff () {
             # Example: git diff branch1..branch2 <tab>
             __git_tree_files ${PREFIX:-.} $(__git_committish_range_last $line[1]) && ret=0
           elif __git_is_committish $line[1] || __git_is_treeish $line[1]; then
+            local files_alt='files::__git_tree_files ${PREFIX:-.} HEAD'
+            [[ $line[1] = (HEAD|@) ]] &&
+                files_alt='files::__git_changed_files'
             # Example: git diff branch1 <tab>
             _alternative \
               'commits::__git_commits' \
               'blobs-and-trees-in-treeish::__git_blobs_and_trees_in_treeish' \
-              'files::__git_tree_files ${PREFIX:-.} HEAD' && ret=0
+              $files_alt && ret=0
           elif __git_is_blob $line[1]; then
             _alternative \
               'files::__git_cached_files' \
@@ -831,6 +834,10 @@ _git-diff () {
               __git_is_treeish $line[2]; then
             # Example: git diff branch1 branch2 <tab>
             __git_tree_files ${PREFIX:-.} $line[2] && ret=0
+          elif [[ $line[1] = (HEAD|@) ]]; then
+            # Example: git diff @ file1 <tab>
+            # Example: git diff HEAD -- <tab>
+            __git_ignore_line __git_changed_files && ret=0
           elif __git_is_committish $line[1] || __git_is_treeish $line[1]; then
             # Example: git diff branch file1 <tab>
             # Example: git diff branch -- f<tab>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: git diff HEAD - only show modified files in completion
  2020-10-16 22:16 ` Ahmad Ismail
  2020-10-17  8:33   ` Oliver Kiddle
@ 2020-10-17 12:28   ` Daniel Shahaf
  1 sibling, 0 replies; 8+ messages in thread
From: Daniel Shahaf @ 2020-10-17 12:28 UTC (permalink / raw)
  To: Ahmad Ismail; +Cc: Zsh Users

Ahmad Ismail wrote on Sat, 17 Oct 2020 04:16 +0600:
> I think it is not showing the screenshot or something.

The screenshot made it through both times.

Furthermore, you referred to the screenshot in the body, so if it
_hadn't_ gone through, someone would have pointed that out.

In any case, copy-pasting the terminal contents as text (as opposed to
image) is preferable when possible, as it's easier to read, quote,
grep, etc..



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: git diff HEAD - only show modified files in completion
  2020-10-17  8:33   ` Oliver Kiddle
@ 2020-10-17 15:24     ` Bart Schaefer
  2020-10-17 16:25       ` Ahmad Ismail
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2020-10-17 15:24 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Ahmad Ismail, Zsh Users

On Sat, Oct 17, 2020 at 1:33 AM Oliver Kiddle <opk@zsh.org> wrote:
>
> In this case, it's probably easier to tweak the original _git-diff
> function than to modify it from a wrapper.

I was thinking there should be some way to implement this with a
zstyle by changing the program invoked by _call_program for the
"files" style, but I didn't have time to work out the incantation for
doing that in the nested subcommand context.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: git diff HEAD - only show modified files in completion
  2020-10-17 15:24     ` Bart Schaefer
@ 2020-10-17 16:25       ` Ahmad Ismail
  2020-10-17 22:15         ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Ahmad Ismail @ 2020-10-17 16:25 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Oliver Kiddle, Zsh Users

[-- Attachment #1: Type: text/plain, Size: 772 bytes --]

I have asked this question on reddit afterwards.
https://www.reddit.com/r/zsh/comments/jcju5h/extend_git_completion_function/

I got a solution from there. However, I am always looking for betterment.

*Thanks and Best Regards,Ahmad Ismail*


On Sat, Oct 17, 2020 at 9:24 PM Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Sat, Oct 17, 2020 at 1:33 AM Oliver Kiddle <opk@zsh.org> wrote:
> >
> > In this case, it's probably easier to tweak the original _git-diff
> > function than to modify it from a wrapper.
>
> I was thinking there should be some way to implement this with a
> zstyle by changing the program invoked by _call_program for the
> "files" style, but I didn't have time to work out the incantation for
> doing that in the nested subcommand context.
>

[-- Attachment #2: Type: text/html, Size: 2055 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: git diff HEAD - only show modified files in completion
  2020-10-17 16:25       ` Ahmad Ismail
@ 2020-10-17 22:15         ` Bart Schaefer
  2020-10-18 18:23           ` Ahmad Ismail
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2020-10-17 22:15 UTC (permalink / raw)
  To: Ahmad Ismail; +Cc: Oliver Kiddle, Zsh Users

On Sat, Oct 17, 2020 at 8:24 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> I was thinking there should be some way to implement this with a
> zstyle by changing the program invoked by _call_program

Finally had a chance to poke at this a bit more ...

On Sat, Oct 17, 2020 at 9:25 AM Ahmad Ismail <ismail783@gmail.com> wrote:
>
> I have asked this question on reddit afterwards.

In your post there, you said:

> I also tried zstyle :completion::complete:git-difftool:argument-rest:' command 'git ls-files -m'. Apparently it is doing nothing.

So very close.

zstyle ':completion::complete:git-diff*:argument-rest:tree-files'
command 'git ls-files -m'

One question is why ^Xh (list-tags) shows the "files" tag for that
context but not the "tree-files" tag.  If you invoke completion
immediately after "git diff " (instead of after "git diff HEAD ") then
the context is "changed-in-working-tree-files" and THAT gets shown
correctly by ^Xh.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: git diff HEAD - only show modified files in completion
  2020-10-17 22:15         ` Bart Schaefer
@ 2020-10-18 18:23           ` Ahmad Ismail
  0 siblings, 0 replies; 8+ messages in thread
From: Ahmad Ismail @ 2020-10-18 18:23 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Oliver Kiddle, Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1152 bytes --]

Hi Bart Schaefer,

Thank you very much.

*Best Regards,Ahmad Ismail*


On Sun, Oct 18, 2020 at 4:15 AM Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Sat, Oct 17, 2020 at 8:24 AM Bart Schaefer <schaefer@brasslantern.com>
> wrote:
> >
> > I was thinking there should be some way to implement this with a
> > zstyle by changing the program invoked by _call_program
>
> Finally had a chance to poke at this a bit more ...
>
> On Sat, Oct 17, 2020 at 9:25 AM Ahmad Ismail <ismail783@gmail.com> wrote:
> >
> > I have asked this question on reddit afterwards.
>
> In your post there, you said:
>
> > I also tried zstyle :completion::complete:git-difftool:argument-rest:'
> command 'git ls-files -m'. Apparently it is doing nothing.
>
> So very close.
>
> zstyle ':completion::complete:git-diff*:argument-rest:tree-files'
> command 'git ls-files -m'
>
> One question is why ^Xh (list-tags) shows the "files" tag for that
> context but not the "tree-files" tag.  If you invoke completion
> immediately after "git diff " (instead of after "git diff HEAD ") then
> the context is "changed-in-working-tree-files" and THAT gets shown
> correctly by ^Xh.
>

[-- Attachment #2: Type: text/html, Size: 2540 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-10-18 18:24 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-15 22:48 git diff HEAD - only show modified files in completion Ahmad Ismail
2020-10-16 22:16 ` Ahmad Ismail
2020-10-17  8:33   ` Oliver Kiddle
2020-10-17 15:24     ` Bart Schaefer
2020-10-17 16:25       ` Ahmad Ismail
2020-10-17 22:15         ` Bart Schaefer
2020-10-18 18:23           ` Ahmad Ismail
2020-10-17 12:28   ` Daniel Shahaf

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git