user2988167 user2988167 - 1 month ago 14
Git Question

How do I change paths in a unified patch?

I have a Git repository in which I made some changes to the folder structure. I moved everything that was in

./_test_html
directory to the root
./
of the repository and continued working.

Now I wanted to revert some changes I had made before the directory change, but realized I could not do this with a normal revert because the files affected were no longer present in my current branch, so I created a patch file and changed all paths in the file from
./subdir
to
./
to make the references correct again, but git apply gives me error:


patch failed, patch does not apply errors.


See below for (parts) of the old and adjusted patch file and the Git output. What am I doing wrong and is there a command/tool to use instead of manually editing patch file?

Old file



---
_test_html/mobile/css/sf.css | 53 ++++++++++++++--
_test_html/mobile/images/logo_temp_x.png | Bin 17636 -> 19362 bytes
_test_html/mobile/js/secretflirt.js | 16 ++++-
_test_html/mobile/templates/footer.tpl.php | 7 ++-
_test_html/mobile/templates/header.tpl.php | 90 ++++++++++++++++++++++++++-
_test_html/mobile/templates/ingelogd.tpl.php | 40 +-----------
6 files changed, 157 insertions(+), 49 deletions(-)

diff --git a/_test_html/mobile/css/sf.css b/_test_html/mobile/css/sf.css
index 0d5f317..63a3b9d 100644
--- a/_test_html/mobile/css/sf.css
+++ b/_test_html/mobile/css/sf.css
@@ -66,7 +66,7 @@ h2, .ui-collapsible-heading{margin:0;font-size:16px;}

.ui-collapsible-heading a span.ui-btn{right:6px;}

-.ui-field-contain{ border-bottom-width: 0px!important; 0px; padding: 0px;
+.ui-field-contain{ border-bottom-width: 0px!important; padding: 0px;
margin: 3px 3px 0px 0px; width: 100%;}
input.ui-input-text, .ui-input-search{background:#fff;color:#333;margin: 2px 2px 2px 0px; width: 100%;height: 2.1em;}
textarea.ui-input-text {background:#fff;color:#333;margin-bottom: 5px; margin-left: 0px;margin-right: 5px;width: 100%; min-height:150px;}
@@ -648,7 +648,7 @@ label.ui-input-text, label.ui-select{color:#333;font-size:100%;font-weight:norma
.ui-body-c .ui-footer .ui-link{color:#c8c8c8;font-size:110%;font-weight:bold;}
.ui-body-c .ui-footer .ui-link.last{color:#2489CE;}
.ui-body-c .ui-footer .ui-link:hover{color:#fff;}
-label.error, {display:block;margin-left: 10px;margin-top: 5px;text-shadow: 1px 1px 5px white;
+label.error {display:block;margin-left: 10px;margin-top: 5px;text-shadow: 1px 1px 5px white;
color: red;
font-size: 1.1em;}


new file

---
mobile/css/sf.css | 53 ++++++++++++++--
mobile/images/logo_temp_x.png | Bin 17636 -> 19362 bytes
mobile/js/secretflirt.js | 16 ++++-
mobile/templates/footer.tpl.php | 7 ++-
mobile/templates/header.tpl.php | 90 ++++++++++++++++++++++++++-
mobile/templates/ingelogd.tpl.php | 40 +-----------
6 files changed, 157 insertions(+), 49 deletions(-)

diff --git a/mobile/css/sf.css b/mobile/css/sf.css
index 0d5f317..63a3b9d 100644
--- a/mobile/css/sf.css
+++ b/mobile/css/sf.css
@@ -66,7 +66,7 @@ h2, .ui-collapsible-heading{margin:0;font-size:16px;}

.ui-collapsible-heading a span.ui-btn{right:6px;}

-.ui-field-contain{ border-bottom-width: 0px!important; 0px; padding: 0px;
+.ui-field-contain{ border-bottom-width: 0px!important; padding: 0px;
margin: 3px 3px 0px 0px; width: 100%;}
input.ui-input-text, .ui-input-search{background:#fff;color:#333;margin: 2px 2px 2px 0px; width: 100%;height: 2.1em;}
textarea.ui-input-text {background:#fff;color:#333;margin-bottom: 5px; margin-left: 0px;margin-right: 5px;width: 100%; min-height:150px;}
@@ -648,7 +648,7 @@ label.ui-input-text, label.ui-select{color:#333;font-size:100%;font-weight:norma
.ui-body-c .ui-footer .ui-link{color:#c8c8c8;font-size:110%;font-weight:bold;}
.ui-body-c .ui-footer .ui-link.last{color:#2489CE;}
.ui-body-c .ui-footer .ui-link:hover{color:#fff;}
-label.error, {display:block;margin-left: 10px;margin-top: 5px;text-shadow: 1px 1px 5px white;
+label.error {display:block;margin-left: 10px;margin-top: 5px;text-shadow: 1px 1px 5px white;
color: red;
font-size: 1.1em;}


git apply output

$ git apply patch2.diff
patch2.diff:101: trailing whitespace.
min-height:400px
patch2.diff:843: trailing whitespace.
}
patch2.diff:846: trailing whitespace.
});
patch2.diff:873: trailing whitespace.
<div class="panel-content">
patch2.diff:874: trailing whitespace.

error: patch failed: mobile/css/sf.css:66
error: mobile/css/sf.css: patch does not apply
error: the patch applies to 'mobile/images/logo_temp_x.png' (ebe43e4cce1686add4f
ad9bf64f0fc261010a6b5), which does not match the current contents.
error: mobile/images/logo_temp_x.png: patch does not apply
error: patch failed: mobile/js/secretflirt.js:1518
error: mobile/js/secretflirt.js: patch does not apply
error: patch failed: mobile/templates/footer.tpl.php:1
error: mobile/templates/footer.tpl.php: patch does not apply
error: patch failed: mobile/templates/header.tpl.php:1
error: mobile/templates/header.tpl.php: patch does not apply
error: patch failed: mobile/templates/ingelogd.tpl.php:28
error: mobile/templates/ingelogd.tpl.php: patch does not apply

Warning: Your console font probably doesn't support Unicode. If you experience s
trange characters in the output, consider switching to a TrueType font such as L
ucida Console!

Answer

You can try to apply the original patch using patch in dry mode:

patch -p2 --dry-run -i patch2.diff 

-p2 means that first two components will be stripped, that is a/_test_html/.

If no errors occurred then run again without --dry-run .

Comments