git cherry-pick --continue,“--no-edit”选项?

git cherry-pick --continue, '--no-edit' option?

我正在编写用于迁移 git 存储库的脚本。关于择优选择冲突,我 运行

git add .
git cherry-pick --continue

这会出现 vim,提示我保存提交消息并冻结脚本。我正在寻找像 --no-edit--porcelain 这样的命令行选项来解决这个问题。

丑陋的终端黑客也可能受到欢迎 ;)

您可以使用:

git cherry-pick <sha1> --no-commit

解决冲突后,您可以从脚本中提交它。

当然你可以设置 cherry-pick strategy-options 通过接受来自 ours/theirs

的代码来自动解决冲突

否则你会得到标准的git冲突标记

+<<<<<<< HEAD
         some code
+||||||| parent of 4d64ec6... test commit
+        first version code
+=======
+        second version code
+>>>>>>> 4d64ec6... test commit

作为,您需要解决所有冲突,然后再执行git add。因此,您不应该使其完全自动化。

就是说,要跳过编辑提交 消息,您只需将编辑器设置为不执行任何操作并报告成功的命令即可。在类 Unix 系统上最理想的是 true 命令。因此:

git -c core.editor=true cherry-pick --continue

会成功的。 (您还可以使用任何环境变量 GIT_EDITORVISUALEDITOR;事实上,如果设置了其中任何一个,您 必须 使用它们而不是 core.editor,因为顺序是:如果已设置,则使用 $GIT_EDITOR;否则,如果已设置,则使用 $VISUAL;否则,如果已设置,则使用 $EDITOR ; 否则使用 core.editor 如果已设置;否则使用此版本 Git 中内置的任何内容。)

来自 torek's :

git -c core.editor=true cherry-pick --continue

随着 Git 2.32(2021 年第 2 季度),不再需要 tihs。

git cherry-pick/revert 有或没有 --[no-]edit 没有按预期生成编辑器(例如,冲突后“revert --no-edit”仍然要求编辑消息) ,已更正 Git 2.32(2021 年第 2 季度)。

参见 commit 39edfd5 (31 Mar 2021) by Elijah Newren (newren)
(由 Junio C Hamano -- gitster -- in commit 82fd285 合并,2021 年 4 月 8 日)

sequencer: fix edit handling for cherry-pick and revert messages

Reported-by: Renato Botelho
Signed-off-by: Elijah Newren
Reviewed-by: Johannes Schindelin

save_opts() should save any non-default values.
It was intended to do this, but since most options in struct replay_opts default to 0, it only saved non-zero values.
Unfortunately, this does not always work for options.edit.
Roughly speaking, options.edit had a default value of 0 for cherry-pick but a default value of 1 for revert.
Make save_opts() record a value whenever it differs from the default.

options.edit was also overly simplistic; we had more than two cases.
The behavior that previously existed was as follows:

Non-conflict commits    Right after Conflict
Edit iff isatty(0)      Edit (ignore isatty(0))
No edit                 See above
Edit (ignore isatty(0)) See above
(*)                     See above

(*) Before stopping for conflicts, No edit is the behavior.  After
    stopping for conflicts, the --no-edit flag is not saved so see
    the first two rows.

However, the expected behavior is:

Non-conflict commits    Right after Conflict
Edit iff isatty(0)      Edit iff isatty(0)
No edit                 Edit iff isatty(0)
Edit (ignore isatty(0)) Edit (ignore isatty(0))
No edit                 No edit

In order to get the expected behavior, we need to change options.edit to a tri-state: unspecified, false, or true.

  • When specified, we follow what it says.
  • When unspecified, we need to check whether the current commit being created is resolving a conflict as well as consulting options.action and isatty(0).
    While at it, add a should_edit() utility function that compresses options.edit down to a boolean based on the additional information for the non-conflict case.

continue_single_pick() is the function responsible for resuming after conflict cases, regardless of whether there is one commit being picked or many.
Make this function stop assuming edit behavior in all cases, so that it can correctly handle !isatty(0) and specific requests to not edit the commit message.