From 66b5992547b391c93fc45fd37e01b66c96a7a2b3 Mon Sep 17 00:00:00 2001 From: Leons Aleksandrovs Date: Thu, 26 Mar 2026 09:32:50 +0200 Subject: [PATCH] add collect_new_files --- scripts/collect_new_files.sh | 92 ++++++++++++++++++ sync-project.sh | 177 ++++++++++++++++++----------------- 2 files changed, 181 insertions(+), 88 deletions(-) create mode 100755 scripts/collect_new_files.sh diff --git a/scripts/collect_new_files.sh b/scripts/collect_new_files.sh new file mode 100755 index 0000000..930a244 --- /dev/null +++ b/scripts/collect_new_files.sh @@ -0,0 +1,92 @@ +#!/bin/bash + +# Collect newly created files from a range of commits into new_files.zip + +set -e + +# Load recent commits into an array +mapfile -t COMMITS < <(git log --oneline -30) + +echo "Recent commits:" +echo "─────────────────────────────────────────────────" +for i in "${!COMMITS[@]}"; do + printf " %2d) %s\n" "$((i + 1))" "${COMMITS[$i]}" +done +echo "─────────────────────────────────────────────────" +echo "" + +read -rp "Select START commit # (older): " START_NUM +read -rp "Select END commit # (newer): " END_NUM + +# Validate input +if ! [[ "$START_NUM" =~ ^[0-9]+$ ]] || ! [[ "$END_NUM" =~ ^[0-9]+$ ]]; then + echo "Error: please enter valid numbers" + exit 1 +fi + +if [ "$START_NUM" -lt 1 ] || [ "$START_NUM" -gt ${#COMMITS[@]} ] || \ + [ "$END_NUM" -lt 1 ] || [ "$END_NUM" -gt ${#COMMITS[@]} ]; then + echo "Error: number out of range (1-${#COMMITS[@]})" + exit 1 +fi + +if [ "$END_NUM" -ge "$START_NUM" ]; then + echo "Error: END commit must be newer (lower number) than START commit" + exit 1 +fi + +# Extract commit hashes (first word of each line) +COMMIT_FROM=$(echo "${COMMITS[$((START_NUM - 1))]}" | awk '{print $1}') +COMMIT_TO=$(echo "${COMMITS[$((END_NUM - 1))]}" | awk '{print $1}') + +echo "" +echo "Scanning commits from $COMMIT_FROM to $COMMIT_TO..." +echo "" + +# Collect all newly added files across the commit range +# --diff-filter=A means only "Added" files +# Use COMMIT_FROM~1 so the range includes the start commit itself +NEW_FILES=$(git diff --diff-filter=A --name-only "$COMMIT_FROM~1".."$COMMIT_TO") + +if [ -z "$NEW_FILES" ]; then + echo "No new files found in the given commit range." + exit 0 +fi + +echo "New files found:" +echo "─────────────────────────────────────────────────" +echo "$NEW_FILES" +echo "─────────────────────────────────────────────────" +echo "" + +# Filter to only files that still exist on disk +EXISTING_FILES=() +MISSING_FILES=() +while IFS= read -r file; do + if [ -f "$file" ]; then + EXISTING_FILES+=("$file") + else + MISSING_FILES+=("$file") + fi +done <<< "$NEW_FILES" + +if [ ${#MISSING_FILES[@]} -gt 0 ]; then + echo "Skipping ${#MISSING_FILES[@]} file(s) no longer on disk:" + for f in "${MISSING_FILES[@]}"; do + echo " - $f" + done + echo "" +fi + +if [ ${#EXISTING_FILES[@]} -eq 0 ]; then + echo "No files to zip (all were deleted since)." + exit 0 +fi + +# Remove old zip if it exists +rm -f new_files.zip + +zip -j new_files.zip "${EXISTING_FILES[@]}" + +echo "" +echo "Done! ${#EXISTING_FILES[@]} file(s) added to new_files.zip" diff --git a/sync-project.sh b/sync-project.sh index 78e83f4..c9064ea 100755 --- a/sync-project.sh +++ b/sync-project.sh @@ -18,24 +18,24 @@ FILES_ONLY=false GIT_ONLY=false for arg in "$@"; do - case "$arg" in - -h | --help) - cat "$SCRIPT_DIR/MANUAL" - exit 0 - ;; - --sftp-only) - SFTP_ONLY=true - echo -e "${YELLOW}SFTP only mode enabled${NC}" - ;; - --files-only) - FILES_ONLY=true - echo -e "${YELLOW}Files only mode enabled${NC}" - ;; - --git) - GIT_ONLY=true - echo -e "${YELLOW}Git clone mode enabled${NC}" - ;; - esac + case "$arg" in + -h | --help) + cat "$SCRIPT_DIR/MANUAL" + exit 0 + ;; + --sftp-only) + SFTP_ONLY=true + echo -e "${YELLOW}SFTP only mode enabled${NC}" + ;; + --files-only) + FILES_ONLY=true + echo -e "${YELLOW}Files only mode enabled${NC}" + ;; + --git) + GIT_ONLY=true + echo -e "${YELLOW}Git clone mode enabled${NC}" + ;; + esac done echo -e "${BLUE}=== Project Sync Configuration ===${NC}" @@ -43,77 +43,77 @@ echo "" # Check if config is predefined (for testing usually) if [ -f ".sync-credentials" ]; then - source ".sync-credentials" + source ".sync-credentials" else - # Prompt for configuration (password before path for SSH resolution) - read -p "Server user@host (e.g., leo@server.com): " SERVER_HOST - read -sp "SSH Password: " SSH_PASSWORD - echo "" - read -p "Server project path (default: ~/public_leo): " SERVER_PROJECT_PATH - read -p "Local folder path (e.g., Delta Pharmacy): " LOCAL_FOLDER - echo "" + # Prompt for configuration (password before path for SSH resolution) + read -p "Server user@host (e.g., leo@server.com): " SERVER_HOST + read -sp "SSH Password: " SSH_PASSWORD + echo "" + read -p "Server project path (default: ~/public_leo): " SERVER_PROJECT_PATH + read -p "Local folder path (e.g., Delta Pharmacy): " LOCAL_FOLDER + echo "" fi # Default path to ~/public_leo if empty if [ -z "$SERVER_PROJECT_PATH" ]; then - SERVER_PROJECT_PATH="~/public_leo" + SERVER_PROJECT_PATH="~/public_leo" fi # Resolve ~ to full path via SSH if [[ "$SERVER_PROJECT_PATH" == ~* ]]; then - echo "Resolving path..." - SERVER_PROJECT_PATH=$(sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "cd ${SERVER_PROJECT_PATH} && pwd") - if [ $? -ne 0 ]; then - echo -e "${RED}Failed to resolve path${NC}" - exit 1 - fi - echo -e "${GREEN}Resolved to: ${SERVER_PROJECT_PATH}${NC}" + echo "Resolving path..." + SERVER_PROJECT_PATH=$(sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "cd ${SERVER_PROJECT_PATH} && pwd") + if [ $? -ne 0 ]; then + echo -e "${RED}Failed to resolve path${NC}" + exit 1 + fi + echo -e "${GREEN}Resolved to: ${SERVER_PROJECT_PATH}${NC}" fi # Validate required variables (all except password) if [ -z "$SERVER_HOST" ]; then - echo -e "${RED}Error: SERVER_HOST is not set${NC}" - exit 1 + echo -e "${RED}Error: SERVER_HOST is not set${NC}" + exit 1 fi if [ -z "$SERVER_PROJECT_PATH" ]; then - echo -e "${RED}Error: SERVER_PROJECT_PATH is not set${NC}" - exit 1 + echo -e "${RED}Error: SERVER_PROJECT_PATH is not set${NC}" + exit 1 fi if [ -z "$LOCAL_FOLDER" ]; then - echo -e "${RED}Error: LOCAL_FOLDER is not set${NC}" - exit 1 + echo -e "${RED}Error: LOCAL_FOLDER is not set${NC}" + exit 1 fi # Git clone mode - delegate to sync-git-project.sh if [ "$GIT_ONLY" = true ]; then - "$SCRIPT_DIR/sync-git-project.sh" \ - "$SERVER_HOST" \ - "$SSH_PASSWORD" \ - "$SERVER_PROJECT_PATH" \ - "$LOCAL_FOLDER" - exit $? + "$SCRIPT_DIR/sync-git-project.sh" \ + "$SERVER_HOST" \ + "$SSH_PASSWORD" \ + "$SERVER_PROJECT_PATH" \ + "$LOCAL_FOLDER" + exit $? fi # Sync terminal info on full sync (no flags) if [ "$SFTP_ONLY" = false ] && [ "$FILES_ONLY" = false ]; then - # Detect terminal type - if [[ "$TERM" == *"ghostty"* ]]; then - TERM_TYPE="xterm-ghostty" - elif [[ "$TERM" == *"kitty"* ]]; then - TERM_TYPE="xterm-kitty" - else - TERM_TYPE="" - fi + # Detect terminal type + if [[ "$TERM" == *"ghostty"* ]]; then + TERM_TYPE="xterm-ghostty" + elif [[ "$TERM" == *"kitty"* ]]; then + TERM_TYPE="xterm-kitty" + else + TERM_TYPE="" + fi - if [ -n "$TERM_TYPE" ]; then - echo "Syncing terminal info ($TERM_TYPE)..." - infocmp -x "$TERM_TYPE" | sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} -- tic -x - - echo -e "${GREEN}Terminal info synced${NC}" - else - echo -e "${YELLOW}Warning: Unknown terminal ($TERM), skipping terminfo sync${NC}" - fi + if [ -n "$TERM_TYPE" ]; then + echo "Syncing terminal info ($TERM_TYPE)..." + infocmp -x "$TERM_TYPE" | sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} -- tic -x - + echo -e "${GREEN}Terminal info synced${NC}" + else + echo -e "${YELLOW}Warning: Unknown terminal ($TERM), skipping terminfo sync${NC}" + fi fi # Start sync process @@ -122,14 +122,14 @@ echo "Starting project sync..." # Step 1: SSH into server and create tar archive echo "Creating archive on server..." if [ "$FILES_ONLY" = true ]; then - sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "cd ${SERVER_PROJECT_PATH} && tar -czf ../${ARCHIVE_NAME} --exclude='node_modules' --exclude='vendor' --exclude='uploads' ." + sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "cd ${SERVER_PROJECT_PATH} && tar -czf ../${ARCHIVE_NAME} --exclude='node_modules' --exclude='vendor' --exclude='uploads' ." else - sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "cd ${SERVER_PROJECT_PATH} && tar -czf ../${ARCHIVE_NAME} --exclude='uploads' ." + sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "cd ${SERVER_PROJECT_PATH} && tar -czf ../${ARCHIVE_NAME} --exclude='uploads' ." fi if [ $? -ne 0 ]; then - echo -e "${RED}Failed to create archive on server${NC}" - exit 1 + echo -e "${RED}Failed to create archive on server${NC}" + exit 1 fi echo -e "${GREEN}Archive created successfully${NC}" @@ -142,8 +142,8 @@ echo "Copying archive to local machine..." sshpass -p "${SSH_PASSWORD}" rsync -az --info=progress2 -e "ssh $SSH_OPTS" ${SERVER_HOST}:~/${ARCHIVE_NAME} "${LOCAL_FOLDER}/" if [ $? -ne 0 ]; then - echo -e "${RED}Failed to copy archive${NC}" - exit 1 + echo -e "${RED}Failed to copy archive${NC}" + exit 1 fi echo -e "${GREEN}Archive copied successfully${NC}" @@ -154,8 +154,8 @@ cd "${LOCAL_FOLDER}" tar -xzf ${ARCHIVE_NAME} if [ $? -ne 0 ]; then - echo -e "${RED}Failed to extract archive${NC}" - exit 1 + echo -e "${RED}Failed to extract archive${NC}" + exit 1 fi echo -e "${GREEN}Archive extracted successfully${NC}" @@ -172,7 +172,7 @@ echo -e "${GREEN}File sync complete!${NC}" # Skip helper scripts and config generation in files-only mode if [ "$FILES_ONLY" = true ]; then - exit 0 + exit 0 fi # Create .vscode sftp.json file @@ -218,6 +218,7 @@ echo "{ \"authType\": \"password\", \"ignore\": [ \".git\", + \".env\", \".gosync.json\", \".vscode\", \".DS_Store\", @@ -257,16 +258,16 @@ add_to_gitignore ".gosync.json" # Go through scripts, add variables and add them to project folder if [ "$SFTP_ONLY" = true ]; then - # Sftp only scripts - scripts=("local-remote.sh" "remote-local.sh" "sftp-watch.sh" "ssh.sh") + # Sftp only scripts + scripts=("local-remote.sh" "remote-local.sh" "sftp-watch.sh" "ssh.sh" "collect_new_files.sh") else - # All scripts - scripts=("watch-build.sh" "local-remote.sh" "remote-local.sh" "sftp-watch.sh" "ssh.sh" "split-conflicts.sh") + # All scripts + scripts=("watch-build.sh" "local-remote.sh" "remote-local.sh" "sftp-watch.sh" "ssh.sh" "split-conflicts.sh", "collect_new_files.sh") fi for script in "${scripts[@]}"; do - # Add variables to script - echo " + # Add variables to script + echo " #!/bin/bash set -e @@ -278,23 +279,23 @@ for script in "${scripts[@]}"; do REMOTE_PROGRESS=\".remote-in-progress\" " >"${script}" - # Append the rest of the upload core build script - cat "${SCRIPT_DIR}/scripts/${script}" >>"${script}" - echo "${script} created successfully" + # Append the rest of the upload core build script + cat "${SCRIPT_DIR}/scripts/${script}" >>"${script}" + echo "${script} created successfully" - # Skip .gitignore updates when --sftp-only flag is present - if [ "$SFTP_ONLY" = false ]; then - add_to_gitignore "${script}" - fi + # Skip .gitignore updates when --sftp-only flag is present + if [ "$SFTP_ONLY" = false ]; then + add_to_gitignore "${script}" + fi done # Upload lazygit to server (skip when --sftp-only flag is present) if [ "$SFTP_ONLY" = false ]; then - echo "Uploading lazygit to server..." + echo "Uploading lazygit to server..." - sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS "${SCRIPT_DIR}/lazygit" ${SERVER_HOST}:${SERVER_PROJECT_PATH} - echo -e "${GREEN}lazygit uploaded successfully${NC}" + sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS "${SCRIPT_DIR}/lazygit" ${SERVER_HOST}:${SERVER_PROJECT_PATH} + echo -e "${GREEN}lazygit uploaded successfully${NC}" - # Add lazygit to .gitignore - add_to_gitignore "lazygit" + # Add lazygit to .gitignore + add_to_gitignore "lazygit" fi