Search code examples
gitgit-diffmonaco-editor

Get file before and after git diff


I need do develop an UI like this in javascript

enter image description here

I think I will use Monaco Diff editor. What I'm looking for is a way to get a file before the changes and after the changes. Actually if I exec git diff my file.ext the output is not what I'm looking for. What I want is a command to get the file before and an other command to get the file after.

With these commands I'll be able to bind the "beforeFile" to the original model and the "afterFile" to the modifiedModel.

If there is no command to do so, is there a way to transform the output of git diff in two files ?


Solution

  • You can get an arbitrary version of any file in the repository using, for example, git cat-file, e.g. for commit foo:

    git cat-file blob foo:path/to/file
    

    The file path is relative to the root of the repository. The file's contents will go to stdout.

    Full documentation: https://git-scm.com/docs/git-cat-file

    PS. also look at git difftool which is a way to create the base files for a tool like you envision and then pass them to a program. It may not fit your use case but might provide some inspiration.