1

I use Git (2.39.2.windows.1) and Qt Creator 7.0.2 on Windows 10. I would like Git to show me files at specific revisions (as in git show <revision>:<relative/path/to/file>) in the instance of Qt Creator that is currently open.

Alternatively, Qt Creator has some Git integration, but I do no see this possiblity under Extras > Git. Is there one, actually ?

The -block option of Qt Creator offers me pretty much that. If I run

qtcreator -block path/to/file.cpp

Then I get the file opened in my Qt Creator instance until I close it there. But if I set the pager for git show to be qtcreator -block then nothing happens when I run git show <file>.

I also tried the syntax qtcreator -block $1 to specify that one argument is expected fropm the caller (that would be the copy of the file at the specified revision, retrieved by Git who calls the pager), but that does nothing more, so still nothing.

Charles
  • 988
  • 1
  • 11
  • 28
  • I assume that you can simply specific a `PAGER` or `GIT_PAGER` environment variable: `GIT_PAGER='vim -' git show --color=never` will open in vim. – Andreas Louv Feb 16 '23 at 09:34
  • See my edits about that. – Charles Feb 16 '23 at 09:46
  • 1
    The problem is that the provided PAGER reads the content from stdin, you might be able to use `/dev/stdin`: `GIT_PAGER='vim /dev/stdin' git show ...` otherwise you can create a small wrapper script that reads the content from stdin stores it in a temporary file, opens your editor, and removes the temporary file. – Andreas Louv Feb 16 '23 at 10:30
  • 1
    @Charles Can you do `cat path/to/file.cpp | qtcreator -block` or `cat path/to/file.cpp | qtcreator -block -` ? – phd Feb 16 '23 at 10:45
  • But how to retrieve `path/fo/tile` from `git`'s calling of that command as `show`'s pager ? – Charles Feb 16 '23 at 10:54
  • 1
    @Charles Can you do this from the command line, just to experiment? Any text file. PS. `git` does the `cat file` path, but I wanna see how to put the stream to QtC. – phd Feb 16 '23 at 10:55
  • Ah ok. So no, this does nothing. Qt Creator flashes in the taskbar, meaning it knows it was called, but it doesn't do anything beyond that. This isn't surprising to be honest, Qt Creator is really a full GUI app, so it will at best receive files, but it isnot mean for command line, so it will not received piped text. – Charles Feb 16 '23 at 10:57
  • 1
    @Charles The last experiment: `cat path/to/file.cpp | qtcreator -block /dev/stdin` — does this work? – phd Feb 16 '23 at 10:59
  • "The binary editor cannot open empty files" – Charles Feb 16 '23 at 11:03
  • 1
    @AndreasLouv I don't want to steal your comment. Would you mind writing said wrapper script as an answer? – phd Feb 16 '23 at 11:05
  • Yeah I was thinking about a script with a temp file. Actually it would be possible to name the file in a meaningful way, with the filename, the hash under scrutiny, and the correct extension so that Qt Creator can do some parsing and coloring and whatnot. – Charles Feb 16 '23 at 12:06
  • 1
    @phd did an attempt. – Andreas Louv Feb 16 '23 at 14:30

2 Answers2

2

Based off the comments above it sounds like specifying either the GIT_PAGER or PAGER environment variable might be what you want:

$ export GIT_PAGER='whatever'
$ git show ...

The pager is expected to read from stdin, which means that you might need to create a small wrapper script that does that and then writes the content to a temporary file:

$ cat /path/to/my/script
#!/bin/bash
tmp=$(mktemp) # You might want to tinker with this variable to indicate the filetype
cat - > "$tmp"
qtcreator -block "$tmp" # change this line to your needs
rm -- "$tmp"

Then specify the GIT_PAGER in your rc file:

export GIT_PAGER='/path/to/my/script'

Then when running git show the pager will be picked up.

$ git show ...

This however have one huge downside which is that the GIT_PAGER environment variable will be picked up by git log, git diff, et al.

Which means that you properly want to specify the pager as a config option:

$ git config --global pager.show '/path/to/my/script'

As the config name indicates this sets the pager for the "show" sub-command.

If you don't fancy bash, then I'm sure it easy to rewrite in Python, Perl, JavaScript or whatever.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • 1
    Oh I fancy bash just as much as the next guy in this computer world. I will try this out. My plan was to set a specific pager for various commands in gitconfig, as you re-explained - this wasn't so much my question. – Charles Feb 16 '23 at 16:26
  • That basically works ! Thank you ! To be fair I should submit this to Qt themselves, this is a missing functionality of their integration of Git in Qt Creator. But anyways. – Charles Feb 17 '23 at 09:22
  • If any of you feels generous : does `git show` pass any arguments to the pager ? Like, filename, hash, etc ... ? I'd like my temp file to be named after all this. – Charles Feb 17 '23 at 09:25
  • Here is my small command to do that : `showq = "!f() { tmp=$(echo $(git rev-parse $1) | cut -c1-7)_$(basename $2) ; git show $1:$2 > "$tmp" ; qtcreator -block "$tmp" ; rm "$tmp";}; f"` – Charles Feb 17 '23 at 09:45
1

Spinning off of the knowledge, insights and ideas gained from @Andreas Louv, you can make a nice name for your file this way .

Here is my small command to do that, to put in your .gitconfig :

showq = "!f() { tmp=$(echo $(git rev-parse $1) | cut -c1-7)_$(basename $2) ; git show $1:$2 > "$tmp" ; qtcreator -block "$tmp" ; rm "$tmp";}; f"

OR

showq = "!f() { tmp=$(echo "$1")_$(basename $2) ; git show $1:$2 > "$tmp" ; qtcreator -block "$tmp" ; rm "$tmp";}; f"

Explaination :

suppose I want to type :

git showq HEAD^^^^^ path/to/file.c

We can either

  • Parse HEAD^^^^^ out using git parse $1 where $1 is the first argument passed to this new function showq. This outputs the full hash, say a1b2c3d4e5f67890a1b2c3d4e5f67890a1b2c3d4. Then we echo/pipe that into cut to only take the first 7 characters, as is common with hashes. This yields a1b2c3d

OR

  • Put double quotes like this

    $(echo "$1")
    

Then we put a _.

Then for the filename (argument $2), we only take the base name.

Then we run the original git show with the proper arguments and ask it to output to a file named as constructed.

Then we call Qt Creator on that file. Qt Creator shows us we are currently looking at <7 char hash OR rev-syntax>_<basename> e.g. in our example a1b2c3d_file.c OR HEAD^^^^^_file.c. Since this has the same extension, it is parsed and colored by Qt Creator as usual.

Then we remove it.

Bingo !

Charles
  • 988
  • 1
  • 11
  • 28
  • This has 2 drawbacks : sometimes running Qt Creator instance isn't found (but then another one opens pretty quickly and comes in focus). But if it is found, then it doesn#t come in focus and only blinks in the taskbar, to show something happened ... can I ping the app from the git script so it comes in focus ? – Charles Feb 20 '23 at 14:41