【发布时间】:2021-04-21 16:41:12
【问题描述】:
我经常遇到这种情况:我有一个使用git format-patch 生成的补丁,我正在使用git apply 应用它,但它失败并显示“补丁不适用”消息。
然后我很惊讶,尝试用patch -p1 -i … 应用它,它成功了,没有任何问题,只有几个大块偏移量(但大到例如:300 行)。
我想知道为什么会这样?我已经将--recount 选项与apply 一起使用,所以它也应该接受大块偏移。我什至使用-3 进行三路合并,这对 50% 的情况有帮助。
我还想说明这个补丁是多么简单(附后)——这是它的摘要:
lib/keybind.c | 1 +
lib/keybind.h | 1 +
misc/mc.default.keymap | 1 +
misc/mc.emacs.keymap | 1 +
src/editor/edit-impl.h | 1 +
src/editor/edit.c | 21 +++++++++++++++++++++
src/keybind-defaults.c | 1 +
我正在应用它与neo-mc 的当前 HEAD。补丁已经在那里合并了,但是首先我正在应用反向补丁(不足为奇:使用patch),然后尝试从文件中应用它(附在下面)。
那么为什么git apply 不能正确处理这个简单的补丁?不能 git 程序员编码或至少复制一个像样的工具(即:patch)?还是我错过了什么?
我也遇到过类似的重要问题:使用format-patch 生成的补丁即使使用patch 也无法应用,所以我使用了git diff master..BRANCH > adhoc.patch 并且它起作用了!我能够使用/usr/bin/patch 应用补丁,我什至没有看format-patch/apply 组合。
这是git apply -3 --recount patches/CenterView_v3.patch的输出:
error: patch failed: src/editor/edit-impl.h:233
Falling back to three-way merge...
Applied patch to 'src/editor/edit-impl.h' cleanly.
error: patch failed: src/editor/edit.c:3766
error: repository lacks the necessary blob to fall back on 3-way merge.
error: src/editor/edit.c: patch does not apply
这里来自patch -p1 -i …:
patching file lib/keybind.c
Reversed (or previously applied) patch detected! Assume -R? [n] y
Hunk #1 succeeded at 69 (offset 5 lines).
patching file lib/keybind.h
Reversed (or previously applied) patch detected! Assume -R? [n] y
Hunk #1 succeeded at 57 (offset 2 lines).
patching file misc/mc.default.keymap
Reversed (or previously applied) patch detected! Assume -R? [n] y
Hunk #1 succeeded at 278 (offset 3 lines).
patching file misc/mc.emacs.keymap
Reversed (or previously applied) patch detected! Assume -R? [n] y
Hunk #1 succeeded at 277 (offset 2 lines).
patching file src/editor/edit-impl.h
Reversed (or previously applied) patch detected! Assume -R? [n] y
Hunk #1 succeeded at 280 with fuzz 1 (offset 47 lines).
patching file src/editor/edit.c
Reversed (or previously applied) patch detected! Assume -R? [n] y
Hunk #1 succeeded at 3281 (offset 258 lines).
Hunk #2 succeeded at 4085 with fuzz 1 (offset 319 lines).
patching file src/keybind-defaults.c
Reversed (or previously applied) patch detected! Assume -R? [n] y
Hunk #1 succeeded at 431 (offset 11 lines).
补丁内容:
From b7f010902585692326161e6e4b16fcd9a9378854 Mon Sep 17 00:00:00 2001
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Date: Sat, 23 Jan 2021 23:16:37 -0600
Subject: CenterView action
---
lib/keybind.c | 1 +
lib/keybind.h | 1 +
misc/mc.default.keymap | 1 +
misc/mc.emacs.keymap | 1 +
src/editor/edit-impl.h | 1 +
src/editor/edit.c | 21 +++++++++++++++++++++
src/keybind-defaults.c | 1 +
7 files changed, 27 insertions(+)
diff --git a/lib/keybind.c b/lib/keybind.c
index abd44d3e2..9ab8dd5a6 100644
--- a/lib/keybind.c
+++ b/lib/keybind.c
@@ -64,6 +64,7 @@ static name_keymap_t command_names[] = {
ADD_KEYMAP_NAME (PageDown),
ADD_KEYMAP_NAME (HalfPageUp),
ADD_KEYMAP_NAME (HalfPageDown),
+ ADD_KEYMAP_NAME (CenterView),
ADD_KEYMAP_NAME (Top),
ADD_KEYMAP_NAME (Bottom),
ADD_KEYMAP_NAME (TopOnScreen),
diff --git a/lib/keybind.h b/lib/keybind.h
index af019df09..1cef8138d 100644
--- a/lib/keybind.h
+++ b/lib/keybind.h
@@ -55,6 +55,7 @@ enum
CK_PageDown,
CK_HalfPageUp,
CK_HalfPageDown,
+ CK_CenterView,
CK_Top,
CK_Bottom,
CK_TopOnScreen,
diff --git a/misc/mc.default.keymap b/misc/mc.default.keymap
index 2931ddd0a..fe9b5adb1 100644
--- a/misc/mc.default.keymap
+++ b/misc/mc.default.keymap
@@ -275,6 +275,7 @@ End = end
Tab = tab; shift-tab; ctrl-tab; ctrl-shift-tab
Undo = ctrl-u
Redo = alt-r
+CenterView = alt-c
Top = ctrl-home; alt-lt
Bottom = ctrl-end; alt-gt
ScrollUp = ctrl-up
diff --git a/misc/mc.emacs.keymap b/misc/mc.emacs.keymap
index 7cc305db7..9d8ee0fd8 100644
--- a/misc/mc.emacs.keymap
+++ b/misc/mc.emacs.keymap
@@ -275,6 +275,7 @@ End = end; ctrl-e
Tab = tab; shift-tab; ctrl-tab; ctrl-shift-tab
Undo = ctrl-u
# Redo =
+CenterView = alt-c
Top = ctrl-home; alt-lt
Bottom = ctrl-end; alt-gt
ScrollUp = ctrl-up
diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h
index 3ad04dbea..69f8c1683 100644
--- a/src/editor/edit-impl.h
+++ b/src/editor/edit-impl.h
@@ -233,6 +233,7 @@ void edit_save_size (WEdit * edit);
gboolean edit_handle_move_resize (WEdit * edit, long command);
void edit_toggle_fullscreen (WEdit * edit);
void edit_move_to_line (WEdit * e, long line);
+void edit_center_display (WEdit * e, long diff);
void edit_move_display (WEdit * e, long line);
void edit_word_wrap (WEdit * edit);
int edit_sort_cmd (WEdit * edit);
diff --git a/src/editor/edit.c b/src/editor/edit.c
index 50879cee2..9146069a2 100644
--- a/src/editor/edit.c
+++ b/src/editor/edit.c
@@ -3023,6 +3023,22 @@ edit_move_to_line (WEdit * e, long line)
edit_scroll_screen_over_cursor (e);
}
+/* --------------------------------------------------------------------------------------------- */
+/** scroll window so that current line is in center; the diff is a relative offset from that
+ * position */
+
+void
+edit_center_display (WEdit * e, long diff)
+{
+ int center_line_diff = WIDGET (e)->lines / 2 + diff;
+ int current_line = e->curs_row;
+
+ if (current_line < center_line_diff)
+ edit_scroll_upward (e, center_line_diff - current_line);
+ else
+ edit_scroll_downward (e, current_line - center_line_diff);
+}
+
/* --------------------------------------------------------------------------------------------- */
/** scroll window so that first visible line is 'line' */
@@ -3766,6 +3782,11 @@ edit_execute_cmd (WEdit * edit, long command, int char_for_insertion)
}
break;
+ case CK_CenterView:
+ /* Center view at cursor line. */
+ edit_center_display (edit, 0);
+ break;
+
case CK_Top:
case CK_MarkToFileBegin:
edit_move_to_top (edit);
diff --git a/src/keybind-defaults.c b/src/keybind-defaults.c
index 7b87c2f5a..ad4b59780 100644
--- a/src/keybind-defaults.c
+++ b/src/keybind-defaults.c
@@ -420,6 +420,7 @@ static const global_keymap_ini_t default_editor_keymap[] = {
{"Goto", "alt-l; alt-shift-l"},
{"Refresh", "ctrl-l"},
{"Shell", "ctrl-o"},
+ {"CenterView", "alt-c"},
{"Top", "ctrl-home; ctrl-pgup; alt-lt"},
{"Bottom", "ctrl-end; ctrl-pgdn; alt-gt"},
{"TopOnScreen", "ctrl-pgup"},
--
2.28.0
【问题讨论】: