Generate diff with prefix - diff

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?

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}
}

Related

GitHub - Remove a indexed file from "Languages" on first page

How can I remove this indexed HTML page, that are a documentation to one of the external librarys I use on my GitHub blob?
I have tried alot of diffrent commands, but don't find a way to remove this file from the GitHub Linguist indexer...
Here are the "Languages" that are indexed on the startpage:
[image] Languages on the startpage
The file that I want to exclude:
[image] HTML file that needs to be excluded
TestProject/wwwroot/lib/bootstrap-icons/docs/index.html
Code that I've tried to get it removed via ".attributes"-file in root-folder (the vendored, works... But not getting rid of this HTML-file... from the GitHub-Languages) :
### vendored:
TestProject/wwwroot/lib/* linguist-vendored
### documentations:
TestProject/wwwroot/lib/bootstrap-icons/* linguist-documentation
and tried:
TestProject/wwwroot/lib/bootstrap-icons/* -linguist-documentation
and this:
TestProject/wwwroot/lib/bootstrap-icons/docs/* linguist-documentation
and this:
TestProject/wwwroot/lib/bootstrap-icons/docs/* -linguist-documentation
and this:
TestProject/wwwroot/lib/* linguist-documentation
and this:
TestProject/wwwroot/lib/* -linguist-documentation
But I can't figure it out how to remove this file:
TestProject/wwwroot/lib/bootstrap-icons/docs/index.html
Please help me with the correct syntax to remove the file from being indexed as a Language in my GitHub repository, main branch. 🙂
You've got the right idea and the right Linguist overrides (either will do the trick). The problem is your path matching isn't quite right.
From the .gitattributes docs
The rules by which the pattern matches paths are the same as in .gitignore files (see gitignore[5]), with a few exceptions:
[...]
If we look in the .gitignore docs (emphasis is mine):
An asterisk "*" matches anything except a slash. The character "?" matches any one character except "/". The range notation, e.g. [a-zA-Z], can be used to match one of the characters in a range. See fnmatch(3) and the FNM_PATHNAME flag for a more detailed description.
Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:
[...]
A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.
The files you're trying to ignore are in sub-directories of the paths you've specified so you need to either:
use TestProject/wwwroot/lib/** linguist-vendored to recurse, or
use TestProject/wwwroot/lib/bootstrap-icons/docs/* linguist-vendored to limit to this directory.
We can demonstrate this without even using Linguist thanks to git check-attr:
$ # Create a repo with just the one file
$ git init -q Test-Project
$ cd Test-Project
$ mkdir -p TestProject/wwwroot/lib/bootstrap-icons/docs/
$ echo "<html>" > TestProject/wwwroot/lib/bootstrap-icons/docs/index.html
$ git add -A
$ git commit -m 'Add file'
[main (root-commit) bed71b5] Add file
1 file changed, 1 insertion(+)
create mode 100644 TestProject/wwwroot/lib/bootstrap-icons/docs/index.html
$
$ # Add your initial override
$ git add -A && git commit -m 'attribs'
[main 7d0a0cf] attribs
1 file changed, 1 insertion(+)
create mode 100644 .gitattributes
$
$ # Check the attributes
$ git check-attr linguist-vendored TestProject/wwwroot/lib/bootstrap-icons/docs/index.html
TestProject/wwwroot/lib/bootstrap-icons/docs/index.html: linguist-vendored: unspecified
$ # So it doesn't have any effect.
$ # Now lets recurse
$ echo "TestProject/wwwroot/lib/** linguist-vendored" > .gitattributes
$ git add -A && git commit -m 'attribs'
[main 9007c34] attribs
1 file changed, 1 insertion(+), 1 deletion(-)
$ git check-attr linguist-vendored TestProject/wwwroot/lib/bootstrap-icons/docs/index.html
TestProject/wwwroot/lib/bootstrap-icons/docs/index.html: linguist-vendored: set
$ # Woohoo!!! It's work.
$ # Lets be specific to the docs dir
$ echo "TestProject/wwwroot/lib/bootstrap-icons/docs/* linguist-vendored" > .gitattributes
$ git add -A && git commit -m 'attribs'
[main a46f416] attribs
1 file changed, 1 insertion(+), 1 deletion(-)
$ git check-attr linguist-vendored TestProject/wwwroot/lib/bootstrap-icons/docs/index.html
TestProject/wwwroot/lib/bootstrap-icons/docs/index.html: linguist-vendored: set
$ # Woohoo!!! It's worked too
Some good troubleshooting from #lildude, shown that:
All the files was ignored correctly.
I had alot of CSHTML-files under my repository that was grouped as HTML+Razor (see this post on GitHub: GitHub linguist discussion ) .
When I clicked the "HTML"-link on startpage under language, it took me to: https://github.com/pownas/Test-Project/search?l=html
But the startpage under language was telling me that I had around 40% html from the HTML+Razor search: https://github.com/pownas/Test-Project/search?l=HTML%2BRazor

Is there a command to diff all the kept files in accurev?

I am new to accurev, used to use SVN earlier. I want to a get diff file consisting of all the changes in kept files in a given directory. I know ac diff -b <file>
gives diff in a file, but if I have many files and I want the diff of all the kept files in a given directory, is there a straight forward command to do this like svn diff?
You are going to need to create a script if you only want to diff kept files in a given directory. Basically you will run an 'accurev stat -k' -> parse output for given directory -> 'accurev diff -b'
On a *NIX machine the commands below work nicely.
The -k option to AccuRev's stat command says find the file with "(kept)" status. Using the -fal options to stat provides just the Depot relative pathway to the file. No addition filtering needed. So the command line would be:
accurev stat -k -fal | xargs accurev diff -b
Produces output like:
accurev stat -k -fal | xargs accurev diff -b
diffing element /./COPYING
341a342
> Tue Mar 18 08:38:39 EDT 2014
> Change for demo purposes.
diffing element /./INSTALL
3a4,7
> New Change
>
> Another Change
>
Dave

diff ignore blank likes

How can I get GNU diff ignore the blank lines in the following example?
File a:
x
do
done
File b:
x
do
done
Neither file has trailing white spaces in any line.
Using GNU diff 3.1 on Mac OS X I get:
diff -w a b
2d1
< do
3a3
> do
Same when I add various promising looking options:
diff --suppress-blank-empty -E -b -w -B -I '^[[:space:]]*$' --strip-trailing-cr -i a b
2d1
< do
3a3
> do
What am I missing here?
diff --version
diff (GNU diffutils) 3.1
I think the problem here is that diff is seeing do as being removed from the first file, and added to the second, maybe because there isn't enough context around the change.
If you reverse the order of the files as arguments, diff reports that the space is added and removed, and will then ignore it with --ignore-blanks-lines.
Looking at it as a unified diff, this is a little more clear:
$ diff test.txt test2.txt -u
--- test.txt 2015-10-20 10:50:52.585167600 -0700
+++ test2.txt 2015-10-20 10:51:01.042167600 -0700
## -1,4 +1,4 ##
x
-do
+do
done
prp#QW7PRP09-14 ~/temp
$ diff test2.txt test.txt -u
--- test2.txt 2015-10-20 10:51:01.042167600 -0700
+++ test.txt 2015-10-20 10:50:52.585167600 -0700
## -1,4 +1,4 ##
x
-
do
+
done
And the result with the --ignore-blank-lines, and the order switched:
prp#QW7PRP09-14 ~/temp
$ diff test2.txt test.txt -B -u

diff for patching without renaming target file

how do i create a standard patch using diff -u without using a different name for the "new" file?
when i submitted a patch for an Apache project, the committer advised that i don't need to rename the file when submitting patches. i can somewhat understand how this breaks patching since the name of the "new" file should somehow match the name of the patch target - however they can't be in the same directory with the same name.
is it okay (for ease of patching) to rename the "old" file, such that i should have used:
diff -u Source-old.java Source.java
instead of:
diff -u Source.java Source-new.java
?
Given an existing project 'a', copy whole project to 'b', make changes in 'b'. Generate diff between original directory and your copied directory.
E.g., checkout or download project to directory 'a', copy to 'b':
$ tree a
a
`-- dir
|-- Bar.java
`-- Foo.java
$ cp -r a b
$ tree b
b
`-- dir
|-- Bar.java
`-- Foo.java
Make changes to 'b' (and only 'b'):
$ diff -r -s a b
Files a/dir/Bar.java and b/dir/Bar.java are identical
Files a/dir/Foo.java and b/dir/Foo.java are identical
$ sed -i 's/Foo.*$/& \/* Change...*\//' b/dir/Foo.java
$ diff -ruN a b | tee a.patch
diff -ruN a/dir/Foo.java b/dir/Foo.java
--- a/dir/Foo.java 2012-08-02 18:41:39.444720785 -0700
+++ b/dir/Foo.java 2012-08-02 18:46:45.319932802 -0700
## -1,2 +1,2 ##
package dir;
-public class Foo {}
+public class Foo {} /* Change...*/
$ gzip a.patch
Another alternative is to store the original source in a temporary, local git repository, then use git's built-in diff to generate the patch. Or, better, if the original source is using git, then just clone the repo and work directly in the source tree itself, and (still) using git to generate the patch.

Using diff and patch to force one local code base to look like another

I've noticed this strange behavior of diff and patch when I've used them to force one code base to be identical to another. Let's say I want to update update_me to look identical to leave_unchanged. I go to update_me. I run a diff from leave_unchanged to update_me. Then I patch the diff into update_me. If there are new files in leave_unchanged, patch asks me if my patch was reversed! If I answer yes, it deletes the new files in leave_unchanged. Then, if I simply re-run the patch, it correctly patches update_me.
Why does patch try to modify both leave_unchanged and update_me?
What's the proper way to do this? I found a hacky way which is to replace all +++ lines with nonsense paths so patch can't find leave_unchanged. Then it works fine. It's such an ugly solution though.
$ mkdir copyfrom
$ mkdir copyto
$ echo "Hello world" > copyfrom/myFile.txt
$ cd copyto
$ diff -Naur . ../copyfrom > my.diff
$ less my.diff
diff -Naur ./myFile.txt ../copyfrom/myFile.txt
--- ./myFile.txt 1969-12-31 19:00:00.000000000 -0500
+++ ../copyfrom/myFile.txt 2010-03-15 17:21:22.000000000 -0400
## -0,0 +1 ##
+Hello world
$ patch -p0 < my.diff
The next patch would create the file ../copyfrom/myFile.txt,
which already exists! Assume -R? [n] yes
patching file ../copyfrom/myFile.txt
$ patch -p0 < my.diff
patching file ./myFile.txt
Edit
I noticed that Mercurial avoids this problem by pre-pending "a" and "b" directories.
$ hg diff
--- a/crowdsourcing/models.py Mon Jun 14 17:18:46 2010 -0400
+++ b/crowdsourcing/models.py Thu Jun 17 11:08:42 2010 -0400
...
I believe the answer here is to execute your diff at the parent directory. Then use patch -p1 to strip this first segment. I believe this is why the strip option of patch actually defaults to 1 rather than 0. E.g. to use your example from above
$ mkdir copyfrom
$ mkdir copyto
$ echo "Hello world" > copyfrom/myFile.txt
$ diff -Naur copyto copyfrom > my.diff
$ less my.diff
diff -Naur copyto/myFile.txt copyfrom/myFile.txt
--- copyto/myFile.txt 1970-01-01 12:00:00.000000000 +1200
+++ copyfrom/myFile.txt 2010-10-19 10:03:43.000000000 +1300
## -0,0 +1 ##
+Hello world
$ cd copyto
$ patch -p1 < ../my.diff
The only difference from your example is that I've executed the diff from the parent directory so that the directories being compared are at the same level.