Git diff as a virtual page

Firstly, what an awesome tool! Many thanks for this Zef!

I made this space-lua script to view git diffs in a “virtual page”. Two commands

  • diff currently active file only
  • diff entire space
virtualPage.define {
  pattern = "gitdiff:(.+)",
  run = function(filename)
    local filepath
    if filename == "." then
      filepath = filename
    else
      filepath = filename .. ".md"
    end
    
    local result = {}
    table.insert(result, "````diff")
    table.insert(
      result,
      shell.run(
        "git",
        {"--no-pager", "diff", filepath}
      ).stdout
    )
    table.insert(result, "````")
    return table.concat(result, "\n")
  end
}

command.define {
  name = 'Git: Diff (this file)',
  run = function()
    editor.navigate("gitdiff:" .. editor.getCurrentPath())
  end
}

command.define {
  name = 'Git: Diff (entire space)',
  run = function()
    editor.navigate("gitdiff:.")
  end
}

Ah cool, just installed this. This is the start of something. I may integrate it into my Git library later if that’s ok with you?

One thing that should be tackled at some point is better version control integration, either backed by git, or different backends. Inevitably, part of that would some sort of diff view. The next obvious ask would then of course be some sort of UI around it to selective apply or revert patches. This is probably trickier part. If there’s any takers of such a project out there, btw, I’d be very willing to take contributions :slight_smile:

yes absolutely!

@ChenZhu-Xie could be awesome to integrate It in your git plugin

might be more accurate to consolidate this script with Version history (git based) and Using `git log` to see what you've worked on , i suppose :wink:

1 Like

Version history is incompatible with v2. No fix

I added the script but the diff code block is just empty for me. I have git setup though, with commands like commit and push working fine.

Hmm, the diff will show nothing if your files are already staged.
Also it won’t show untracked files.

If you change the code to

      shell.run(
        "git",
        {"--no-pager", "diff", "HEAD", filepath}
      ).stdout

does it work?

Also, I’m using the vanilla docker image. IDK if it matters

That didn’t make a difference, and I am also using the vanilla docker image. I may have to wait till I get home and can try the git commands on the vps directly, see if its outputting things correctly there.

1 Like