[pass] [PATCH] Added 'rename' command

Matthieu Weber mweber at free.fr
Fri Mar 28 18:21:54 CET 2014


---
 man/pass.1                          |    7 +++++
 src/completion/pass.bash-completion |    4 +++
 src/completion/pass.fish-completion |    4 +++
 src/completion/pass.zsh-completion  |    7 +++++
 src/password-store.sh               |   49 ++++++++++++++++++++++++++++++++++-
 5 files changed, 70 insertions(+), 1 deletion(-)

diff --git a/man/pass.1 b/man/pass.1
index 668206d..bb7b654 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -114,6 +114,13 @@ alternatively named \fBremove\fP or \fBdelete\fP. If \fI--recursive\fP or \fI-r\
 is specified, delete pass-name recursively if it is a directory. If \fI--force\fP
 or \fI-f\fP is specified, do not interactively prompt before removal.
 .TP
+\fBmv\fP [ \fI--force\fP, \fI-f\fP ] \fIold-pass-name\fP \fInew-pass-name\fP
+Renames the password named \fIold-pass-name\fP to \fInew-pass-name\fP. This
+command is alternatively named \fBrename\fP. If \fI--force\fP is specified,
+silently overwrite \fInew-pass-name\fP if it exists. If \fInew-pass-name\fP is
+a directory, the filename part of \fIold-pass-name\fP is used to construct the
+full path for the new password.
+.TP
 \fBgit\fP \fIgit-command-args\fP...
 If the password store is a git repository, pass \fIgit-command-args\fP as arguments to
 .BR git (1)
diff --git a/src/completion/pass.bash-completion b/src/completion/pass.bash-completion
index d0ef012..26cde8d 100644
--- a/src/completion/pass.bash-completion
+++ b/src/completion/pass.bash-completion
@@ -70,6 +70,10 @@ _pass()
 				COMPREPLY+=($(compgen -W "-n --no-symbols -c --clip -f --force" -- ${cur}))
 				_pass_complete_entries
 				;;
+			mv|rename)
+				COMPREPLY+=($(compgen -W "-f --force" -- ${cur}))
+				_pass_complete_entries
+				;;
 			rm|remove|delete)
 				COMPREPLY+=($(compgen -W "-r --recursive -f --force" -- ${cur}))
 				_pass_complete_entries
diff --git a/src/completion/pass.fish-completion b/src/completion/pass.fish-completion
index 5c52ecc..f140fcd 100644
--- a/src/completion/pass.fish-completion
+++ b/src/completion/pass.fish-completion
@@ -81,6 +81,10 @@ complete -c $PROG -f -A -n '__fish_pass_uses_command generate' -s c -l clip -d '
 complete -c $PROG -f -A -n '__fish_pass_uses_command generate' -s f -l force -d 'Do not prompt before overwritting'
 complete -c $PROG -f -A -n '__fish_pass_uses_command generate' -a "(__fish_pass_print_entry_dirs)"
 
+complete -c $PROG -f -A -n '__fish_pass_needs_command' -a mv -d 'Command: rename existing password'
+complete -c $PROG -f -A -n '__fish_pass_uses_command mv' -s f -l force -d 'Force rename'
+complete -c $PROG -f -A -n '__fish_pass_uses_command mv' -a "(__fish_pass_print_entries_and_dirs)"
+
 complete -c $PROG -f -A -n '__fish_pass_needs_command' -a rm -d 'Command: remove existing password'
 complete -c $PROG -f -A -n '__fish_pass_uses_command rm' -s r -l recursive -d 'Remove password groups recursively'
 complete -c $PROG -f -A -n '__fish_pass_uses_command rm' -s f -l force -d 'Force removal'
diff --git a/src/completion/pass.zsh-completion b/src/completion/pass.zsh-completion
index 7590322..4601e8e 100644
--- a/src/completion/pass.zsh-completion
+++ b/src/completion/pass.zsh-completion
@@ -48,6 +48,12 @@ _pass () {
 					"--clip[copy password to the clipboard]"
 				_pass_complete_entries_with_subdirs
 				;;
+			mv|rename)
+				_arguments : \
+					"-f[force rename]" \
+					"--force[force rename]" \
+					_pass_complete_entries_with_subdirs
+				;;
 			rm)
 				_arguments : \
 					"-f[force deletion]" \
@@ -81,6 +87,7 @@ _pass () {
 			"insert:Insert a new password"
 			"generate:Generate a new password using pwgen"
 			"edit:Edit a password with \$EDITOR"
+			"mv:Rename the password"
 			"rm:Remove the password"
 			"git:Call git on the password store"
 			"version:Output version information"
diff --git a/src/password-store.sh b/src/password-store.sh
index c576844..1100134 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -55,6 +55,9 @@ usage() {
 		Generate a new password of pass-length with optionally no symbols.
 		Optionally put it on the clipboard and clear board after 45 seconds.
 		Prompt before overwriting existing password unless forced.
+	    $program mv [--force,-f] old-pass-name new-pass-name
+		Rename old-pass-name to new-pass-name.
+		Prompt before renaming unless forced.
 	    $program rm [--recursive,-r] [--force,-f] pass-name
 		Remove existing password or directory, optionally forcefully.
 	    $program git git-command-args...
@@ -70,7 +73,7 @@ usage() {
 }
 is_command() {
 	case "$1" in
-		init|ls|list|show|insert|edit|generate|remove|rm|delete|git|help|--help|version|--version) return 0 ;;
+		init|ls|list|show|insert|edit|generate|rename|mv|remove|rm|delete|git|help|--help|version|--version) return 0 ;;
 		*) return 1 ;;
 	esac
 }
@@ -388,6 +391,50 @@ case "$command" in
 			clip "$pass" "$path"
 		fi
 		;;
+	rename|mv)
+		force=0
+		opts="$($GETOPT -o f -l force -n "$program" -- "$@")"
+		err=$?
+		eval set -- "$opts"
+		while true; do case $1 in
+			-f|--force) force=1; shift ;;
+			--) shift; break ;;
+		esac done
+		if [[ $# -ne 2 ]]; then
+			echo "Usage: $program $command [--force,-f] old-pass-name new-pass-name"
+			exit 1
+		fi
+		old_path="$1"
+		new_path="$2"
+		old_passfile="$PREFIX/${old_path%/}"
+		if [[ ! -d $old_passfile ]]; then
+			old_passfile="$PREFIX/$old_path.gpg"
+			if [[ ! -f $old_passfile ]]; then
+				echo "$old_path is not in the password store."
+				exit 1
+			fi
+		fi
+		new_passfile="$PREFIX/${new_path%/}"
+		if [[ -d $new_passfile ]]; then
+			new_path="$new_path/$(basename $old_path)"
+		fi
+		new_passfile="$PREFIX/$new_path.gpg"
+		if [[ -e $new_passfile ]]; then
+			echo "$new_path is already in the password store."
+			exit 1
+		fi
+		[[ $force -eq 1 ]] || yesno "Are you sure you would like to rename $old_path to $new_path?"
+		mkdir -p -v "$PREFIX/$(dirname "$new_path")"
+
+		if [[ -e $old_passfile && ! -e $new_passfile ]]; then
+			if [[ -d $GIT_DIR ]]; then
+				git mv "$old_passfile" "$new_passfile"
+				git commit -m "Renamed $old_path to $new_path."
+			else
+				mv -v "$old_passfile" "$new_passfile"
+			fi
+		fi
+		;;
 	delete|rm|remove)
 		recursive=""
 		force=0
-- 
1.7.10.4



More information about the Password-Store mailing list