Last active
September 19, 2025 03:18
-
-
Save KuanYuChang/bdae6e23af3b2cfc0d2554c5d611cfa9 to your computer and use it in GitHub Desktop.
A script that uses vimdiff to review differences in the working file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| vimdiff="vimdiff" | |
| cvs_rev="HEAD" | |
| ################################################################################ | |
| # Process input arguments | |
| ################################################################################ | |
| if [ "$1" == "-h" ]; then | |
| echo "CVSVIMDIFF - A script that uses vimdiff to review differences in the working file" | |
| echo | |
| echo "Usage: cvsvimdiff [-g] [-r TAG] FILE_NAME" | |
| echo | |
| echo "Arguments: | |
| -g Start vimdiff in GUI mode | |
| -r <tag> Compare the working file with the specified revision tag. If no -r option is provided, the working file will be compared with the most recent version available in the repository." | |
| exit 0 | |
| fi | |
| if [ "$1" == "-g" ]; then | |
| vimdiff="gvimdiff" | |
| shift 1 | |
| fi | |
| if [ "$1" == "-r" ]; then | |
| cvs_rev="$2" | |
| shift 2 | |
| fi | |
| if [ -z "$1" ]; then | |
| echo "Insufficient input arguments" | |
| exit 1 | |
| else | |
| file_path="$1" | |
| shift 1 | |
| fi | |
| if [ ! -z "$1" ]; then | |
| echo "The script $(basename $0) does not support diffing multiple files" | |
| exit 1 | |
| fi | |
| ################################################################################ | |
| # Check the availability of the working file | |
| ################################################################################ | |
| file_dir="$(dirname ${file_path})" | |
| file_name="$(basename ${file_path})" | |
| if [ ! -f "${file_path}" ]; then | |
| echo "${file_path} does not exist" | |
| exit 1 | |
| fi | |
| if [ ! -d "${file_dir}/CVS" ]; then | |
| echo "There is no version in ${file_dir}, i.e., ${file_dir}/CVS does not exist" | |
| exit 1 | |
| fi | |
| ################################################################################ | |
| # Check the availability of the corresponding CVS repository | |
| ################################################################################ | |
| cvs="cvs -q -d $(cat ${file_dir}/CVS/Root)" | |
| cvs_cmd_co="${cvs} checkout -p -r ${cvs_rev}" | |
| cvs_file="$(cat ${file_dir}/CVS/Repository)/${file_name}" | |
| ${cvs_cmd_co} ${cvs_file} > /dev/null | |
| if [ ! "$?" -eq 0 ]; then | |
| echo "Failed to checkout ${cvs_file}" | |
| exit 1 | |
| fi | |
| ################################################################################ | |
| # Diff the working file | |
| ################################################################################ | |
| cvs_cmd_diff="${cvs} diff -r ${cvs_rev}" | |
| ${cvs} status ${file_path} | |
| ${cvs_cmd_diff} ${file_path} > /dev/null | |
| if [ "$?" -ne 0 ]; then | |
| read -p "Press ENTER to continue" | |
| ${vimdiff} -c "windo let &ft = getwinvar(2, '&ft')" <(${cvs_cmd_co} ${cvs_file}) ${file_path} | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment