Acknowledged that it's not a good situation, but sometimes you discover that the same utility scripts (or things like Makefile
s) have been checked in to different directories within the same or different repositories for the same or different projects. They may have even been checked into inconsistently named directories or project directories resident at different levels of the source tree.
How can I find the files with the same name and (maybe) the same purpose that are living in different directories and easily discover which are the same, which are different and what the differences are?
For the Makefile
example, let's say that someone is working alone on a new and improved variant, projectX, and has updated the Makefile to automate a formerly manual task. You're responsible for making sure that the rest of the older projects catch up, but someone might have already done some of the updates.
You could use the script below to run diffTree.sh Makefile
and get output like this:
bash$ diffTree.sh Makefile
diff ./newprojects/projectX/Makefile ./projectB/Makefile
1,2d0
< CONFIGPARAM = "foo"
17c5
< echo "I did that thing for you"
---
> echo "You should do that thing"
diff ./projectB/Makefile ./projectC/Makefile
diff ./projectC/Makefile ./projectD/Makefile
end of files
bash$ _
This tells you that projectB and C and D are all identical, but newprojects/projectX/Makefile is different from B, and therefore also equally different from C and D.
If all the files in different locations with the same name are the same, you'll get something like this:
bash$ diffTree.sh sameFile.h
diff ./newprojects/projectX/src/headers/sameFile.h ./projectB/src/headers/sameFile.h
diff ./projectB/src/headers/sameFile.h ./projectC/src/headers/sameFile.h
diff ./projectC/src/headers/sameFile.h ./projectD/src/headers/sameFile.h
end of files
bash$ _
If you specify a filename that doesn't exist at all in the tree, you'll get:
bash$ diffTree.sh foo
end of files
bash$ _
Here's the script:
#!/usr/bin/env bash
set `find . -name $1 -print ; echo no files`
while [[ $# -gt 1 ]]
do
if [[ $2 = no && $3 = files ]]
then
shift
else
echo diff $1 $2
diff $1 $2
fi
shift
done
echo end of files
It appears to handle wildcards in the filename, but there's probably something to be done to make that safer. Likewise, allowing for diff options like 'ignore whitespace' and so on would be an improvement.
Obviously, it totally breaks if you happen to have a file called 'no' and a second file called 'files' come up in the middle of the 'find' command's output, but since all the filenames are prefixed by './' and 'no' comes after 'files' alphabetically, I think it's safe enough.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments