Skip to content

Instantly share code, notes, and snippets.

@mdb
Created December 7, 2010 20:33
Show Gist options
  • Save mdb/732362 to your computer and use it in GitHub Desktop.
Save mdb/732362 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Running this script via ./smartcommit.sh provides a fast way
# to selectively include files in an 'svn commit.'
# The script can be used as follows:
#
# 1. Enter ./smartcommit.sh at the command line from within the project's directory
# 1. A temporary file containing an 'svn status' list is opened in Vim
# 2. Remove any lines specifiying files you DO NOT want to commit
# 3. Enter :wq in command mode to save and quit the temporary file
# 4. Proceed with the commit as normal, noting that only those files left
# listed in in the aforementioned temporary file will be committed
#
# Alternatively, you can copy and paste the following function into your
# ~/.bash_profile, but remember to remove the # at each line's beginning.
#
# Once this function lives in your .bash_profile, the function can be run
# with the command 'smartcommit.'
#
# function smartcommit() {
# svn stat > /tmp/svn_commits.tmp
# vim /tmp/svn_commits.tmp
# svn commit `cat /tmp/svn_commits.tmp | cut -d' ' -f2- | xargs`
# rm /tmp/svn_commits.tmp
# }
svn stat > /tmp/svn_commits.tmp
vim /tmp/svn_commits.tmp
svn commit `cat /tmp/svn_commits.tmp | cut -d' ' -f2- | xargs`
rm /tmp/svn_commits.tmp
@fakeh
Copy link

fakeh commented Dec 30, 2012

Hi, I liked your idea but had some additional criteria:

  • cope with more complex statuses (your cut failed when there were statuses in more than the first column)
  • svn add, where necessary
  • exit early if there's nothing to commit, and allow the user to quit the script after exiting vim, if nothing in the file was changed.

function cleanup {
rm /tmp/svn_commits.tmp /tmp/svn_commits.md5 2> /dev/null #cleanup, redirect stops it complaining when the files aren't there.
}
trap cleanup EXIT #always cleanup when the script exits.

svn stat > /tmp/svn_commits.tmp
[ ! -s /tmp/svn_commits.tmp ] && echo "Nothing to commit, quitting." && exit

md5 /tmp/svn_commits.tmp > /tmp/svn_commits.md5 #Used later to detect if the file changes
vim /tmp/svn_commits.tmp #User can now edit the commit list
md5 /tmp/svn_commits.tmp | diff /tmp/svn_commits.md5 - > /dev/null && read -p "No changes were made, did you want to quit? [CTRL+C quit | ENTER proceed]
" #newline

svn add grep -e "^\?" /tmp/svn_commits.tmp | cut -c9- | xargs #the cut -c9- removes the first 9 columns, which represent svn statuses, see svn help st
svn commit cut -c9- /tmp/svn_commits.tmp | xargs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment