Sublime Forum

View In Editor to view a specific revision (menu items)

#1

Wrote up a basic View In Editor menu item that will extract a read-only version of a file at a specific commit as an alternative to Open In Editor that only opens the HEAD version.

The menu item is implemented using a git alias which also allows command line usage if wanted:

  • git smerge-view-in-editor path/to/file commit-ish
  • git smerge-view-in-editor path/to/file commit-ish 10 (at line 10)

Notes/Caveats:

  • always creates a read-only temporary file, regardless of if it’s on the current branch or not
  • viewing a file from Commit Changes will always open a blank file due an invalid/non-existent commit

Steps:

  1. add this git alias:

    git config --global alias.smerge-view-in-editor '!f() {
        local file="${1#"$PWD"}"
        local commit="${2:-HEAD}"
        local line="${3:-1}"
        
        local temp_file="$(mktemp -d)"
        temp_file="$temp_file/$(basename "$temp_file").$(basename "$file")"
        git show "$commit:$file" > "$temp_file"
        chmod a-wx "$temp_file"
        
        subl "$temp_file:$line"
    }; f'
    
  2. add this menu item to File.sublime-menu:

    [
        {
            "caption": "View in Editor…",
            "command": "git",
            "args": { "argv": ["smerge-view-in-editor", "$path", "$commit", "1"] }
        }
    ]
    
  3. add this menu item to Diff Context.sublime-menu:

    [
        {
            "caption": "View in Editor…",
            "command": "git",
            "args": { "argv": ["smerge-view-in-editor", "$path", "$commit", "$line"] }
        }
    ]
    
2 Likes

Opening file version at commit
Create tag and push to remote from single command
#2

unfortunately this won’t work out of the box on windows. Using the git(.exe) command to add that alias gives an error. In your windows user home .gitconfig you can add it like this:

[alias]
smerge-view-in-editor = “!f() {\n local file=”${1#"$PWD"}"\n local commit="${2:-HEAD}"\n local line="${3:-1}"\n \n local temp_file="$(mktemp -d)"\n temp_file="$temp_file/$(basename “$temp_file”).$(basename “$file”)"\n git show “$commit:$file” > “$temp_file”\n chmod a-wx “$temp_file”\n \n subl “$temp_file:$line”\n}; f"

It will be interpreted at least but broken. I tried fiddling around, for example passing $working_dir into it because $PWD would be broken but since my repo was in a $wsl path this proved impossible for me (apparently you can’t go to a $wsl dir in cmd). There’s more stuff to fix but I gave up at that point. Please if anyone knows how to tame this cursed Linux Windows bastard dimension please help… This feature is needed direly

0 Likes