Created
December 10, 2018 03:33
-
-
Save Peregring-lk/3062060d909ea8283c87dc1b2bd086ce to your computer and use it in GitHub Desktop.
Emacs function to update emacs conf from git repo at startup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; This code assume that your emacs conf (.emacs.d) is itself a git repo. | |
;; This function check at startup if there's updates available, applying | |
;; `git pull` in that case, and reloading your config (assumed to be in | |
;; .emacs.d/init.el). | |
;; | |
;; Useful when you have multiple machines with a same emacs configuration | |
;; that you want to keep synchronized (assuming of course that your emacs | |
;; repo is in some git server). | |
;; | |
;; It is also assumed that you don't need to write a password or passphrase | |
;; to apply any git command. | |
;; | |
;; For manual updates, it also provides the key binding `C-c u`. If you don't | |
;; have the `bind-key` module, you can rewrite the `bind-keys` call to | |
;; | |
;; (global-set-key (kbd "C-c u") 'emacs-conf-upgrade) | |
;; | |
;; instead. | |
(defun current-date () | |
"Human readable current date time." | |
(format-time-string "%d-%m-%Y %H:%M:%S")) | |
(defun emacs-conf-git-status () | |
"Check the git config status." | |
(let ((local (shell-command-to-string "git -C ~/.emacs.d rev-parse @{0}")) | |
(remote (shell-command-to-string "git -C ~/.emacs.d rev-parse @{u}")) | |
(base (shell-command-to-string "git -C ~/.emacs.d merge-base @{0} @{u}"))) | |
(cond ((string= local remote) 0) ; Up-to-date | |
((string= local base) 1) ; Need to pull | |
((string= remote base) 2) ; Need to push | |
(t 3)))) ; Diverge | |
(defun emacs-conf-git-pull () | |
"Pull emacs conf git repo." | |
(call-process-shell-command "git -C ~/.emacs.d pull")) | |
(defun emacs-conf-upgrade () | |
"Upgrade the Emacs conf from the git repo in case of updates." | |
(call-process-shell-command "git -C ~/.emacs.d fetch") | |
(message (concat "Emacs conf upgrade try at " (current-date))) | |
(let ((status (emacs-conf-git-status))) | |
(cond ((= status 1) | |
(progn (emacs-conf-git-pull) | |
(if (= (emacs-conf-git-status) 0) | |
(progn | |
(load "~/.emacs.d/init.el") | |
(message "Emacs conf successfully upgraded.")) | |
(message "Emacs conf git repo must be checked. Pull already tried.") | |
))) | |
((/= status 0) | |
(message "Emacs conf git repo must be manually checked. Need to push or merge.")) | |
(t (message "Emacs conf is up-to-date"))) | |
)) | |
(bind-keys* | |
("C-c u" . emacs-conf-upgrade)) | |
(emacs-conf-upgrade) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment