git 如何让 COMMIT_EDITMSG 从正确的位置打开?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14067841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:56:37  来源:igfitidea点击:

How do I get COMMIT_EDITMSG to open from the correct location?

gitmingw32

提问by toxalot

I'm new to GIT. I downloaded GIT for Windows from a GitHub link a few days ago. I'm using the command line tool MinGW32. I'm not comfortable with the default editor so I've been trying to set up my favourite editor.

我是 GIT 的新手。几天前,我从 GitHub 链接下载了 Windows 版 GIT。我正在使用命令行工具MinGW32。我对默认编辑器不满意,所以我一直在尝试设置我最喜欢的编辑器。

I followed the instructions hereto use EditPad Pro as my editor. But I keep getting the following message:

我按照此处的说明使用 EditPad Pro 作为我的编辑器。但我不断收到以下消息:

Aborting commit due to empty commit message.

Aborting commit due to empty commit message.

EditPad Pro opens a new instance. MinGW32 is waiting because I don't get the abort message until after I close EditPad Pro. When the editor opens, it opens with a blank file called COMMIT_EDITMSG. When I close the editor, the file saves to the main directory for the repo.

EditPad Pro 打开一个新实例。MinGW32 正在等待,因为在我关闭 EditPad Pro 之前我没有收到中止消息。当编辑器打开时,它会打开一个名为COMMIT_EDITMSG. 当我关闭编辑器时,文件会保存到 repo 的主目录中。

I found a clue in this answer, specifically this phrase:

我在这个答案中找到了一个线索,特别是这句话:

[Vim] saves the file to .git/COMMIT_EDITMSG by default

[Vim] 默认将文件保存到 .git/COMMIT_EDITMSG

If I do a Save Asto save the file to the .git directory before closing the editor, then it works. However, there are two problems with that:

如果我在关闭编辑器之前执行另存为将文件保存到 .git 目录,则它可以工作。但是,这样做有两个问题:

  1. I have to remember to Save As
  2. I don't get the helpful comments that Git adds by default to COMMIT_EDITMSG
  1. 我必须记得另存为
  2. 我没有得到 Git 默认添加到 COMMIT_EDITMSG 的有用评论

The current config setting for core.editoris:

当前的配置设置core.editor是:

"'D:\Program Files\JGsoft\EditPadPro5\EditPad Pro.exe' //newinstance"

I'm not sure what the $*mentioned in the instructions is for, but I tried it with and without that and also assorted variations with and without single/double quotes. I tried setting the value in a shell script as well. At worst, it doesn't work at all (e.g. won't even open the editor) and at best it opens a blank file.

我不确定说明中$*提到的内容是什么,但我尝试了有没有那个的情况,以及有和没有单/双引号的各种变体。我也尝试在 shell 脚本中设置该值。最坏的情况是,它根本不起作用(例如,甚至无法打开编辑器),最多只能打开一个空白文件。

How do I get my editor to open with the file that Git created in the .git directory?

如何让我的编辑器使用 Git 在 .git 目录中创建的文件打开?

EDIT:I get the exact same results whether I use $*or not, and this answersays it's not needed. This Git Propagemakes mention of it when explaining how to set up external merge and diff tools, but makes no mention of it when explaining the core.editorconfig setting. Note: I also tried %*.

编辑:无论我是否使用$*,我都会得到完全相同的结果,这个答案说不需要。这个Git Pro页面在解释如何设置外部合并和差异工具时提到了它,但在解释core.editor配置设置时没有提到它。注意:我也试过%*.

If the $*variable was needed (and missing), I would think that EditPad Pro would open with a blank Untitledfile rather than a blank COMMIT_EDITMSG file in the current directory. The problem seems to be the path.

如果$*需要(并且缺少)该变量,我认为 EditPad Pro 将打开一个空白的Untitled文件,而不是当前目录中的空白 COMMIT_EDITMSG 文件。问题似乎是路径。

EDIT:I've done more experimenting. I have spaces in my file path and I thought that might be causing a problem. I cloned my repo into a new directory with no spaces in the name and fixed my config variables. It didn't solve the problem. But I noticed another problem. In some of my tests, the blank file that was loaded into the editor was named $@.

编辑:我做了更多的实验。我的文件路径中有空格,我认为这可能会导致问题。我将我的 repo 克隆到一个名称中没有空格的新目录中,并修复了我的配置变量。它没有解决问题。但我注意到另一个问题。在我的一些测试中,加载到编辑器中的空白文件名为$@.

采纳答案by toxalot

There are several issues that can be causing confusion and problems.

有几个问题可能会导致混淆和问题。

  1. The Shell Special Variable

    If core.editoris set to the editorpath and filename, then the $*variable is redundant and notneeded. However, if the core.editoris set to a shell script, then the $*variable must be passed along to the editor.

    This is valid:

    $ git config --global core.editor "'D:/Path To/EditPadPro.exe' //newinstance"
    

    This is also valid:

    $ git config --global core.editor "'E:/Path To/editor.sh'"
    

    when editor.sh contains:

    #!/bin/sh
    "D:/Path To/EditPadPro.exe" //newinstance "$*"
    
  2. Spaces in File Names

    Filenames with spaces can be a pain. If the path/filename is quoted, then it isn't usually a problem. But when setting the value of core.editoryou have to either

    escape the spaces like this:

    "E:/Path\ To/editor.sh"
    

    or quote it twice like this:

    "'E:/Path To/editor.sh'"
    

    Without the extra quotes (or backslash escapes), you will have no problem setting the value, but it will fail when it's used because the outer quotes are not part of the value.

    EDIT:Latter method (quoting twice) seems safer. See the edit at the bottom for more explanation.

  3. Windows Path Separator

    The filename that is being passed to the editor can be a relative path (i.e. .git/COMMIT_EDITMSG) or absolute path (i.e. e:/path to/.git/rebase-merge/git-rebase-todo), but in both cases it is using forward slashes as a path separator. Windows can usually accept forward slashes as a path separator, especially if the path is quoted. Perhaps the older version of EditPad Prois unable to accept the forward slashes in combination with the hidden directory. A little bit of preprocessing can fix this.

    Note:Hardcoded paths with forward slashes and nohidden directory seem to work fine. Hardcoded paths with hidden directories andbackslashes seem to work fine.

  1. Shell 特殊变量

    如果core.editor设置为编辑路径和文件名,那么$*变量是多余的,没有必要的。但是,如果将core.editor设置为 shell 脚本,则$*必须将该变量传递给编辑器。

    这是有效的:

    $ git config --global core.editor "'D:/Path To/EditPadPro.exe' //newinstance"
    

    这也是有效的:

    $ git config --global core.editor "'E:/Path To/editor.sh'"
    

    当 editor.sh 包含:

    #!/bin/sh
    "D:/Path To/EditPadPro.exe" //newinstance "$*"
    
  2. 文件名中的空格

    带空格的文件名可能会很痛苦。如果路径/文件名被引用,那么它通常不是问题。但是在设置值时,core.editor您必须要么

    像这样逃避空间:

    "E:/Path\ To/editor.sh"
    

    或者像这样引用两次:

    "'E:/Path To/editor.sh'"
    

    如果没有额外的引号(或反斜杠转义),设置值就没有问题,但在使用时会失败,因为外部引号不是值的一部分。

    编辑:后一种方法(引用两次)似乎更安全。有关更多解释,请参阅底部的编辑。

  3. Windows 路径分隔符

    传递给编辑器的文件名可以是相对路径(即 .git/COMMIT_EDITMSG)或绝对路径(即 e:/path to/.git/rebase-merge/git-rebase-todo),但在这两种情况下它使用正斜杠作为路径分隔符。Windows 通常可以接受正斜杠作为路径分隔符,尤其是在路径被引用的情况下。可能旧版本的EditPad Pro无法接受正斜杠与隐藏目录结合使用。一些预处理可以解决这个问题。

    注意:带有正斜杠且没有隐藏目录的硬编码路径似乎工作正常。带有隐藏目录反斜杠的硬编码路径似乎工作正常。

Final Solution

最终解决方案

I'm not very experienced with shell scripting, but the following is now working for me.

我对 shell 脚本编写不是很有经验,但以下内容现在对我有用。

The editor.sh file contains:

editor.sh 文件包含:

#!/bin/sh
fullpath=`echo "$*" | tr '/' '\\'`
"D:/Program Files/JGsoft/EditPadPro5/EditPadPro.exe" //newinstance "$fullpath"

and the config is set like this:

并且配置是这样设置的:

$ git config --global core.editor "'E:/Path To/editor.sh'"

My copy of EditPad Pro 5.3.2 is now opening with the correct files already loaded, regardless of what git command launches the editor.

我的 EditPad Pro 5.3.2 副本现在以已加载的正确文件打开,无论 git 命令启动编辑器。

EDIT:I had to change the value of core.editor. I had used backslashes to escape spaces in the path and this opened the editor properly. However, when a GIt command passed a fielname with a relative path (beginning with a dot) to my shell script, the value of $*was $@rather than the filename, which caused the editor to open with a blank file named $@. I thought I tested that combination, but apparently not. Using the quote twicemethod works.

编辑:我不得不改变core.editor. 我使用反斜杠来转义路径中的空格,这正确打开了编辑器。但是,当 GIt 命令将带有相对路径(以点开头)的文件名传递给我的 shell 脚本时,它的值$*$@而不是文件名,这导致编辑器打开一个名为$@的空白文件。我以为我测试了这种组合,但显然没有。使用引用两次方法有效。

回答by VonC

$*is for "all other parameters": see "what does $*mean in a shell script"

$*用于“所有其他参数”:请参阅“ shell 脚本中的$*含义

If you forget $*in:

如果你忘记$*了:

 "C:/Program Files/JGsoft/EditPadPro6/EditPadPro.exe" //newinstance "$*"

, you won't open your editor with the final parameter which is .git/COMMIT_EDITMSG.
that means you won't save by dfault your commit message where it should be saved (for git to use it).

,您将不会使用最终参数打开编辑器,即.git/COMMIT_EDITMSG.
这意味着您不会将提交消息保存在应该保存的位置(以便 git 使用它)。

回答by toxalot

I'm not sure whether the problem lies with EditPad Pro or MinGW32, but I found a workaround. If I pass in the path and file name, it works.

我不确定问题出在 EditPad Pro 还是 MinGW32 上,但我找到了一个解决方法。如果我传入路径和文件名,它就可以工作。

For example:

例如:

$ git config --global core.editor "'D:/Program Files/JGsoft/EditPadPro5/EditPadPro.exe' //newinstance '.git\COMMIT_EDITMSG'"

EditPad Pro will open with two files: a blank COMMIT_EDITMSG in the current directory and the COMMIT_EDITMSG from the .git directory. I can edit the .git one and save it. When I close the editor, the .git one is used as the commit message. The blank one is not saved anywhere and is completely ignored.

EditPad Pro 将打开两个文件:当前目录中的空白 COMMIT_EDITMSG 和 .git 目录中的 COMMIT_EDITMSG。我可以编辑 .git 并保存它。当我关闭编辑器时,.git 文件被用作提交消息。空白的没有保存在任何地方,完全被忽略。

The backslash and single quotes in '.git\COMMIT_EDITMSG'are important. It won't work any other way. At first I thought maybe my old version of EditPad Pro didn't like the forward slashes, but I can pass in other file names using forward slashes in the path (with or without the quotes) and it works for them. I can only surmise that any other variation of COMMIT_EDITMSG is conflicting with what MinGW32 is already passing in.

中的反斜杠和单引号'.git\COMMIT_EDITMSG'很重要。它不会以任何其他方式工作。起初我想也许我的旧版本 EditPad Pro 不喜欢正斜杠,但我可以在路径中使用正斜杠(带或不带引号)传递其他文件名,并且它适用于它们。我只能推测 COMMIT_EDITMSG 的任何其他变体都与 MinGW32 已经传入的内容相冲突。

Since the path is relative to the current directory, it should work no matter which repo I am committing.

由于路径是相对于当前目录的,所以无论我提交哪个 repo,它都应该可以工作。

EDIT: The above solution will only work for commits.

编辑:上述解决方案仅适用于提交。

It doesn't help when the editor is used for other purposes such as an interactive rebase. See my other answerfor the final solution.

当编辑器用于其他目的(例如交互式变基)时,它无济于事。有关最终解决方案,请参阅我的其他答案

回答by Tad Guski

The following works for me so far for commits:

到目前为止,以下适用于我的提交:

$ git config --global core.editor "'C:/Program Files/Just Great Software/EditPad Pro 7/EditPadPro7.exe' '//wait'" --replace-all

This opens a new tab pointing to the correct file and waits for that tab to close before continuing. The --replace-allwas added to clear stuff I had stuck in there from previous attempts to set it.

这将打开一个指向正确文件的新选项卡,并在继续之前等待该选项卡关闭。该--replace-都加入到清晰的东西,我就堵在那里,从以前的尝试来设置。

回答by gerrant

The solution in the chosen answer was very helpful.

所选答案中的解决方案非常有帮助。

I want to share a one-liner version based on it:

我想分享一个基于它的单行版本:

git config --global core.editor "callEditor(){ filepath=\`echo \"$*\" | tr '/' '\' 2> /dev/null\`; '**PathToYourEditorHere**' \"$filepath\"; }; callEditor"