[pass] [PATCH] generate only first line

Facundo Tuesca facutuesca at gmail.com
Sun May 4 04:46:55 CEST 2014

Hey everyone, here's a patch for the feature requested last month (I just
suscribed to the list, so I can't send this as a reply to the original
The feature requested was:

> As someone who likes to change passwords quite often I really like the
generate command.
> But as I prefer to have metadata alongside the password it would be
really useful if pass would only replace the first line of a passfile if it
regenerates an already existing password.
> Otherwise when using generate everything else in the passfile would be

and one of the repiles was:

> I second that. I was thinking about it, but haven't got around coding
> anything yet.
> I would do it as an option to "generate" e.g., "-r/--replace".

So here's a little patch that implements the option "-r" for "generate",
allowing to replace the first line of the password file, and leaving the
rest untouched.

 src/password-store.sh | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index ad22833..d35765e 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -230,10 +230,11 @@ cmd_usage() {
             overwriting existing password unless forced.
         $PROGRAM edit pass-name
             Insert a new password or edit an existing password using
-        $PROGRAM generate [--no-symbols,-n] [--clip,-c] [--force,-f]
pass-name pass-length
+        $PROGRAM generate [--no-symbols,-n] [--clip,-c] [--force,-f]
[--replace,-r] pass-name pass-length
             Generate a new password of pass-length with optionally no
             Optionally put it on the clipboard and clear board after 45
             Prompt before overwriting existing password unless forced.
+            Only replace the first line, leaving the rest of the file
         $PROGRAM rm [--recursive,-r] [--force,-f] pass-name
             Remove existing password or directory, optionally forcefully.
         $PROGRAM mv [--force,-f] old-path new-path
@@ -430,18 +431,19 @@ cmd_edit() {

 cmd_generate() {
-    local opts clip=0 force=0 symbols="-y"
-    opts="$($GETOPT -o ncf -l no-symbols,clip,force -n "$PROGRAM" -- "$@")"
+    local opts clip=0 force=0 replace=0 symbols="-y"
+    opts="$($GETOPT -o ncfr -l no-symbols,clip,force,replace -n "$PROGRAM"
-- "$@")"
     local err=$?
     eval set -- "$opts"
     while true; do case $1 in
         -n|--no-symbols) symbols=""; shift ;;
         -c|--clip) clip=1; shift ;;
         -f|--force) force=1; shift ;;
+        -r|--replace) replace=1; shift;;
         --) shift; break ;;
     esac done

-    [[ $err -ne 0 || $# -ne 2 ]] && die "Usage: $PROGRAM $COMMAND
[--no-symbols,-n] [--clip,-c] [--force,-f] pass-name pass-length"
+    [[ $err -ne 0 || $# -ne 2 ]] && die "Usage: $PROGRAM $COMMAND
[--no-symbols,-n] [--clip,-c] [--force,-f] [--replace,-r]  pass-name
     local path="$1"
     local length="$2"
     check_sneaky_paths "$path"
@@ -450,11 +452,18 @@ cmd_generate() {
     set_gpg_recipients "$(dirname "$path")"
     local passfile="$PREFIX/$path.gpg"

-    [[ $force -eq 0 && -e $passfile ]] && yesno "An entry already exists
for $path. Overwrite it?"
+    [[ $force -eq 0 && $replace -eq 0 && -e $passfile ]] && yesno "An
entry already exists for $path. Overwrite it?"

     local pass="$(pwgen -s $symbols $length 1)"
     [[ -n $pass ]] || exit 1
-    $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}"
+    local output="$pass"
+    if [[ $replace -eq 1 ]]; then
+        local file_metadata="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail
-n +2)"
+        output+="$(echo -e "\n$file_metadata")"
+    fi
+    $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}"
     git_add_file "$passfile" "Add generated password for $path to store."

     if [[ $clip -eq 0 ]]; then
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.zx2c4.com/pipermail/password-store/attachments/20140503/c52f029a/attachment.html>

More information about the Password-Store mailing list