Reverting All Buffers

I have two machines on which I do most of my writing and development: a 27″ iMac and a 5 year old MacBook Pro. I use both of these computers everyday so I need to keep them in sync. I do that with Git repositories that live on a separate Linux server. One of those repositories contains all my org files. That includes blog posts, todo and task lists from my org agenda, and various other files that I maintain with org-mode. Those, along with all the other archived files means that I have a lot of files to keep up to date.

The first thing I do when I begin a session is a git pull to make sure I have the latest files from the other machine. Of course that means I have to be religious about pushing any changes to Git but that’s pretty much second nature now. The other thing you need to know is that when I’m finished with a session, I just put the machine in sleep mode. That means my Emacs still has all the open files from the last session when I get back to it. I also have desktop-save-mode on so this happens even if I turn the computer off or reboot it.

Here’s the problem. After the Git pull, those buffers and their underlying files are no longer in sync. I could set Emacs to automatically sync them but I don’t want to deal with the overhead. Thus I have to run revert-buffer by hand for each of the updated files. That’s a pain, of course, and cries out for automation so I wrote a quick bit of Elisp to do it for me. Here’s the code

(defun revert-all-buffers ()
  "Revert all non-modified buffers associated with a file.
This is to update existing buffers after a Git pull of their underlying files."
  (interactive)
  (save-current-buffer
    (mapc (lambda (b)
            (set-buffer b)
            (unless (or (null (buffer-file-name)) (buffer-modified-p))
              (revert-buffer t t)
              (message "Reverted %s\n" (buffer-file-name))))
          (buffer-list))))

The code is pretty simple. It goes through the buffer list looking for buffers with an underlying file. If the buffer is not dirty, I call revert-buffer to get the changes from the Git pull into the buffer. If I’ve made changes to the buffer, then I have to fix things up by hand so I don’t want to do the revert.

It seems to me that there’s nothing special about my work flow so others may have the same problem and find revert-all-buffers useful.

This entry was posted in Programming and tagged , . Bookmark the permalink.