Reputation: 81
I have a file which is not under git control and I need to generate a patch for it. I know I can use diff -Naur file file_new > diff.patch
but it will produce something like:
--- file <timestamp>
+++ file_new <timestamp>
<diff content>
But I want to get something like git diff does:
--- a/file <timestamp>
+++ b/file <timestamp>
<diff content>
Is there a way to generate this type of patch without using git diff and editing it manually?
Upvotes: 1
Views: 96
Reputation: 1
The git diff --no-index
command provides the capabilities of git diff
without the need to have your files/directories added to the git system (e.g. using git add
). git diff
will by default add a/ and b/ to the source and destination files respectively, hence
git diff --noindex file file_new > diff.patch
should give the requested result.
In addition, the options --src-prefix=
and --dst-prefix=
allow to change /a
and /b
in whatever desired. As with regular diff
, to create new content from (files in) a directory, you can not use /dev/null
as the source, but need an empty directory instead.
mkdir /tmp/empty
git diff --no-index --dst-prefix="b/drivers/net/ethernet/" /tmp/empty mydriver/ >mydriver.patch
will create a patch file that, when executed, generates a new Linux kernel ethernet network driver in the proper drivers/net/ethernet/mydriver
directory from the content of the local (development) folder mydriver
Upvotes: 0
Reputation: 81
Ok, I created a simple function in my ~/.zshrc file that does it. Hope this would be helpful for someone.
diffgit () {
local dir_a=a/${$(dirname $1)#pwd}
local dir_b=b/${$(dirname $1)#pwd}
mkdir -p ./${dir_a} ./${dir_b}
cp $1 ./${dir_a}
cp $2 ./${dir_b}/$(basename $1)
diff -Naur ${dir_a} ${dir_b}
rm -rf ./{a,b}
}
Upvotes: 2