Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace grep in filter_ignore_files() with git ls-files --ignored #636

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 27 additions & 29 deletions git-ftp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ get_keychain_password () {
fi

[ -z "$KEYCHAIN_USER" ] && print_error_and_die "Missing keychain account." "$ERROR_MISSING_ARGUMENTS"

local KEYCHAIN_ARGS=(-a "$KEYCHAIN_USER")
[ -n "$KEYCHAIN_HOST" ] && KEYCHAIN_ARGS+=(-s "$KEYCHAIN_HOST")

Expand Down Expand Up @@ -787,29 +787,27 @@ add_include_file() {
}

filter_ignore_files() {
[ -f '.git-ftp-ignore' ] || return
local patterns="$TMP_DIR/ignore_tmp"
grep -v '^#.*$\|^\s*$' '.git-ftp-ignore' | tr -d '\r' > "$patterns"
filter_file "$patterns" "$1"
filter_file "$patterns" "$2"
rm -f "$patterns"
}

filter_file() {
glob_filter "$1" < "$2" > "$TMP_DIR/filtered_tmp"
mv "$TMP_DIR/filtered_tmp" "$2"
}

# Original implementation http://stackoverflow.com/a/27718468/3377535
glob_filter() {
local patterns="$1"
while IFS= read -r -d '' filename; do
local hasmatch=0
while IFS= read -r pattern; do
case $filename in ($pattern) hasmatch=1; break ;; esac
done < "$patterns"
test $hasmatch = 1 || printf '%s\0' "$filename"
done
[ -f '.git-ftp-ignore' ] || return
local ignored="$TMP_DIR/ignore_tmp"

# Get list of ignored files from git.
git ls-files --ignored --cached --exclude-from='.git-ftp-ignore' | \
# Make sure line endings are \n only (remove \r from windows).
tr -d '\r' > "$ignored"

# Filter out ignored files via grep for each input file.
for file in "$@"
do
# Use grep to filter out unwanted data, relevant flags:
# -f get pattern list from file
# -z the input lines are separated by null bytes
# -v inverse match: output contains what is _not_ matched in pattern list
# -Z ouput lines are separated by with null bytes
grep -f "$ignored" -FwzvZ "$file" > "$TMP_DIR/filtered_tmp"
mv "$TMP_DIR/filtered_tmp" "$file"
done

rm -f "$ignored" # cleanup
}

handle_file_sync() {
Expand Down Expand Up @@ -1117,7 +1115,7 @@ set_remotes() {

set_insecure
write_log "Insecure is '$INSECURE'."

set_curl_disable_epsv
[ $CURL_DISABLE_EPSV -eq 1 ] && write_log "Disable EPSV is '$CURL_DISABLE_EPSV'."

Expand Down Expand Up @@ -1171,7 +1169,7 @@ set_curl_proxy() {
set_merge_args() {
local config="$(get_config no-commit)"
[ -n "$config" ] && NO_COMMIT=1

if [ $NO_COMMIT -eq 1 ]; then
MERGE_ARGS="$MERGE_ARGS --no-commit --no-ff"
fi
Expand Down Expand Up @@ -1210,12 +1208,12 @@ download_remote_updates () {
ignore+="--exclude=^\.git/ --exclude=^\.git-ftp\.log --exclude=^\.git-ftp-ignore"

handle_lftp_settings

local lftp_cd=""
[ -n $REMOTE_PATH ] && lftp_cd="cd ${REMOTE_PATH} &&"
local lftp_action="mirror $mirror_options $delete $ignoreall $include $ignore . $SYNCROOT &&"
local lftp_exit="wait all && exit"

lftp_command="$LFTP_COMMAND_SETTINGS $lftp_cd $lftp_action $lftp_exit"
out="$(lftp $LFTP_OPTIONS -e "$lftp_command" -u "${REMOTE_USER},${REMOTE_PASSWD}" "${LFTP_PROTOCOL}://${REMOTE_HOST}/" 2>&1)"
print_info "$out"
Expand Down Expand Up @@ -1444,7 +1442,7 @@ check_remote_access() {
CURL_ARGS+=(--ftp-create-dirs)
CURL_ARGS+=("$REMOTE_BASE_URL/$REMOTE_PATH")
curl "${CURL_ARGS[@]}" > /dev/null

local EXIT_CODE=$?
if [ "$REMOTE_PROTOCOL" == "sftp" ] && [ $EXIT_CODE -eq 78 ]; then
write_log "Create $REMOTE_PATH"
Expand Down