Git show:在我的常用编辑器Qt Creator中查看文件

huangapple go评论71阅读模式
英文:

Git show : view file in my usual editor, Qt Creator

问题

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.

英文:

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.

答案1

得分: 2

根据上面的评论,似乎指定GIT_PAGERPAGER环境变量可能是你想要的:

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

Pager预期从标准输入读取,这意味着你可能需要创建一个小包装脚本,它会从标准输入读取内容,然后将内容写入临时文件:

$ cat /path/to/my/script
#!/bin/bash
tmp=$(mktemp) # 你可能需要调整此变量以指示文件类型
cat - > "$tmp"
qtcreator -block "$tmp" # 根据你的需求更改此行
rm -- "$tmp"

然后在你的rc文件中指定GIT_PAGER

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

然后运行git show时,Pager将生效:

$ git show ...

然而,这种方法有一个巨大的缺点,即GIT_PAGER环境变量将被git loggit diff等命令捕获。

这意味着你可能希望将Pager指定为配置选项:

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

如配置名称所示,这会为“show”子命令设置Pager。

如果你不喜欢Bash,那么你可以轻松地将其重写为Python、Perl、JavaScript或其他语言。

英文:

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.

答案2

得分: 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 !"

英文:

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 !

huangapple
  • 本文由 发表于 2023年2月16日 17:30:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定