Skip to content

Instantly share code, notes, and snippets.

@atomontage
Last active June 1, 2020 04:38
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 atomontage/64b767731b5896f1c6113adb9fd0a231 to your computer and use it in GitHub Desktop.
Save atomontage/64b767731b5896f1c6113adb9fd0a231 to your computer and use it in GitHub Desktop.
Setting GIT remotes in Emacs Lisp
#!/usr/bin/env -S emacs --quick --script
(require 'cl-lib)
(defvar *use-color* t)
(defvar *stdout* nil)
(defvar *stderr* #'external-debugging-output)
(defvar *output-stream* *stdout*)
(defun out (fmt &rest args) (princ (apply 'format fmt args)
*output-stream*))
(cl-defmacro with-color (color msg)
(let* ((colors [:black :red :green :yellow :blue :magenta :cyan :white])
(idx (cl-position color colors)))
(unless idx (error "Invalid color: %s" color))
`(format "%s%s%s"
(if *use-color* ,(format "\e[1;%sm" (+ idx 30)) "")
,msg
(if *use-color* "\e[0m" ""))))
(defun info (fmt &rest args)
(out "%s %s" (with-color :green "OK") (apply 'format fmt args)))
(defun err (fmt &rest args)
(let ((*output-stream* *stderr*))
(out "%s %s" (with-color :red "ERR") (apply 'format fmt args))))
(defun git (&rest args)
(with-temp-buffer
(cons (apply #'call-process "git" nil '(t t) nil args)
(buffer-string))))
(defun set-remote ()
(cl-loop
with status = 0
with root = (file-name-directory load-file-name)
with remote-list = (with-temp-buffer
(insert-file-contents-literally
(concat root ".gitremotes.el"))
(read (current-buffer)))
for (module . remotes) in remote-list
for default-directory = (concat root module)
for count from 0 do
(out "%s:\n" (with-color :blue module))
(cl-loop
for (remote . url) in remotes
for (code . out) = (git "remote" "get-url" remote)
do
(if (= code 0)
(progn
(if (string-match url out)
(info "%s exists: %s" remote out)
(setq status 1)
(err "%s exists and differs: %s" remote out)))
(let* ((ret (git "remote" "add" remote url))
(code (car ret))
(out (cdr ret)))
(if (= code 0)
(info "added %s (%s)\n" remote url)
(err "error %s" out)))))
(out "\n")
finally (out "Total: %d\n\n" count)
finally return status))
(kill-emacs (set-remote))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment