Sublime Forum

Manual Fetch All (command)

#1

Every once in a while, I forget which tabs have or haven’t been updated recently. I decided to find a workaround that could hold until native support or the Plugin API is released.

The two commands added are Fetch in all known repositories & Fetch in all open repositories. the former fetches in all repositories known to Sublime Merge & the latter fetches only those that are open. Due to the periodic nature of the session auto-save process, the list of open repositories may not always be complete/accurate.

Both commands are implemented using a git alias which also allows command line usage if wanted:

  • fetch all: git smerge-fetch all
  • fetch open: git smerge-fetch open

Both commands support specifying a second argument for settings directory (for those using portable installs, etc):

  • fetch all: git smerge-fetch all /path/to/merge/Data
  • fetch open: git smerge-fetch open /path/to/merge/Data

Notes/Caveats:

  • when used within Sublime Merge, it passes in the settings directory to support portable installs
  • command line support doesn’t work on Windows without the second parameter
    • if anyone wants to implement it, let me know how & I’ll update it (keep in mind git-bash, msysgit, WSL, WSL2, etc)
  • the repository/tab in which the Fetch in all .. command is run in will not auto-update or be usable until the operation is completed
    • depending on repository count & update size, it could take a while
    • this is due to Sublime Merge expecting the command to operate solely on that tab’s repository

Steps:

  1. install jq (https://github.com/stedolan/jq)

  2. add this git alias:

    git config --global alias.smerge-fetch '!f() {
        local merge_dir="$2"
        
        # determine default-install data directory if not specified
        if [ "$merge_dir" == "" ]; then
            case "$(uname -a | tr "[:upper:]" "[:lower:]")" in
                darwin*) 
                    merge_dir="$HOME/Library/Application Support/Sublime Merge"
                    ;;
                mingw* | msys* | windows* | *microsoft*)
                    echo "Windows is not supported" >&2
                    exit 1
                    ;;
                *)
                    merge_dir="$HOME/.config/sublime_merge"
                    ;;
            esac
        fi
        
        local session_file="$(ls -Art "$merge_dir"/Local/*.sublime_session | tail -n 1)"
        
        # fetch all by default
        local jq_command=".recent[].path"
        if [ "$1" == "open" ]; then
            jq_command=".windows[].tabs[].path"
        fi
        
        jq -r "$jq_command" < "$session_file" |
            while IFS= read -r repo; do
                printf "\n\nFetching $repo\n"
                git -C "$repo" fetch
            done
        
        echo "Done"
    }; f'
    
  3. add these commands to Default.sublime-commands:

    [
        {
            "caption": "Fetch in all known repositories",
            "command": "git",
            "args": { "argv": ["smerge-fetch", "all", "$packages/.."] }
        },
        {
            "caption": "Fetch in all open repositories",
            "command": "git",
            "args": { "argv": ["smerge-fetch", "open", "$packages/.."] }
        }
    ]
    
2 Likes