From 45b218d5c18c7ce61b1fcca8cc185b36ac34a1f6 Mon Sep 17 00:00:00 2001 From: Martin Zuther Date: Sun, 29 Dec 2019 15:21:55 +0100 Subject: [PATCH] support encrypted files in "yadm list" * export some helper functions to hooks --- contrib/hooks/post_list | 73 +++++++++++++++++++++++++++++++++++++++++ yadm | 6 ++++ 2 files changed, 79 insertions(+) create mode 100755 contrib/hooks/post_list diff --git a/contrib/hooks/post_list b/contrib/hooks/post_list new file mode 100755 index 0000000..6e0394e --- /dev/null +++ b/contrib/hooks/post_list @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +# yadm - Yet Another Dotfiles Manager +# Copyright (C) 2015-2019 Tim Byrne and Martin Zuther + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +YADM_CHECKSUMS="$YADM_HOOK_DIR/files.checksums" + + +# is current directory on yadm's work path? +# (adapted from https://unix.stackexchange.com/a/6438/122163) +if [ "${PWD##$YADM_HOOK_WORK}" != "$PWD" ]; then + ON_WORK_PATH=1 +else + ON_WORK_PATH=0 +fi + + +# list all files or only those in the subdirectories below? +OPTION_LIST_ALL=0 +for argument in ${YADM_HOOK_FULL_COMMAND[*]}; do + # mimick git ls-files by displaying all files when not on work + # path + if [ "$argument" = "-a" ] || [ $ON_WORK_PATH -eq 0 ]; then + OPTION_LIST_ALL=1 + break + fi +done + + +# if there is no checksum file, exit with original status of yadm +# command +if [ ! -f "$YADM_CHECKSUMS" ]; then + exit "$YADM_HOOK_EXIT" +fi + +# list encrypted files; try to satisfy the "list -a" argument and +# beautify the output (requires "grep" and "sed") +if command -v grep > /dev/null && command -v sed > /dev/null; then + # remove checksums from file names + while IFS= read -r filename; do + if [ $OPTION_LIST_ALL -eq 0 ]; then + # list only files in the subdirectories below (i.e. files + # whose relative path doesn't begin with "../") + REL_PATH=$(relative_path "$PWD" "$YADM_HOOK_WORK/$filename") + + if ! echo "$REL_PATH" | grep '^\.\./' > /dev/null; then + echo "$REL_PATH" + fi + else + # list all files + echo "$filename" + fi + done <<< "$(sed -r 's/^\S+\s+//' "$YADM_CHECKSUMS")" +else + # just display checksum file + cat "$YADM_CHECKSUMS" +fi + +# return original exit status of yadm command +exit "$YADM_HOOK_EXIT" diff --git a/yadm b/yadm index 312b70a..0a642aa 100755 --- a/yadm +++ b/yadm @@ -1591,6 +1591,12 @@ function invoke_hook() { export YADM_HOOK_WORK export YADM_ENCRYPT_INCLUDE_FILES + # export helper functions + export -f builtin_dirname + export -f relative_path + export -f unix_path + export -f mixed_path + "$hook_command" hook_status=$?