Skip to content

Instantly share code, notes, and snippets.

@Pagliacii
Last active February 21, 2021 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pagliacii/841f9f2a9eeb754f84f215f5f38664ca to your computer and use it in GitHub Desktop.
Save Pagliacii/841f9f2a9eeb754f84f215f5f38664ca to your computer and use it in GitHub Desktop.
Quick insert file content into .org file. You can specify a history version of the file!
#+name: show-file-content
#+header: :exports none :tangle no :results value raw
#+header: :var path="./README.org" :var revision="HEAD"
#+header: :var start=1 :var end=-1
#+begin_src emacs-lisp
(substring
  (shell-command-to-string
    (format "git show %s:%s | sed -n '%d,%sp;d'"
            revision
            path
            start
            (cond ((integerp end)
                   (if (= end -1)
                       "$"
                     end))
                  ((char-or-string-p end)
                   (if (string-match "\\+\\([0-9]+\\)$" end)
                       (+ (string-to-number (match-string 1 end)) start)
                     "$"))
                  (t "$"))))
  0
  -1)
#+end_src

#+call: show-file-content(path="./src-file.c", revision="<Commit ID>", start=1, end="+10") :wrap src c

Use C-c C-c to insert results after #+call: line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment