slackbuilds/git-status.el

395 lines
12 KiB
EmacsLisp
Raw Normal View History

2008-07-31 22:11:46 +02:00
;;; git-status -- control git from Emacs.
;; Copyright (C) 2008 Marius Vollmer
;;
;; Git-status is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 3, or (at your
;; option) any later version.
;;
;; Git-status is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Introduction
;; Invoking the git-status function will show a buffer with the
;; current status of the current git repository and its checkout.
;; That buffer offers key bindings for manipulating the status in
;; simple ways.
;;
;; The status buffer mainly shows the difference between the working
;; tree and the index, and the difference between the index and the
2008-08-04 01:05:02 +02:00
;; current HEAD. You can add individual hunks from the working tree
;; to the index, and you can commit the index.
2008-08-01 13:37:54 +02:00
2008-08-03 20:41:24 +02:00
;;; TODO
2008-08-04 14:35:25 +02:00
;; - Shortcut for staging/unstaging of whole files
;; - Dealing with merges from pulls
2008-08-04 14:52:53 +02:00
;; - Hide titles of empty sections
2008-08-04 14:35:25 +02:00
;; - Branch creation/switching
;; - Explicit diffing/merging of branches
2008-08-05 00:01:43 +02:00
;; - Detect and handle renames and copies.
2008-08-03 20:50:00 +02:00
;; - History browsing
2008-08-03 20:41:24 +02:00
2008-08-01 13:37:54 +02:00
;;; Utilities
(defun gits-shell (cmd &rest args)
(let ((str (shell-command-to-string (apply 'format cmd args))))
(if (string= str "")
nil
(if (equal (elt str (- (length str) 1)) ?\n)
(substring str 0 (- (length str) 1))
str))))
2008-08-03 20:33:25 +02:00
(defun gits-concat-with-delim (delim seqs)
(cond ((null seqs)
nil)
((null (cdr seqs))
(car seqs))
(t
(concat (car seqs) delim (gits-concat-with-delim delim (cdr seqs))))))
(defun gits-get (&rest keys)
(gits-shell "git-config %s" (gits-concat-with-delim "." keys)))
2008-08-04 01:05:02 +02:00
(defun gits-set (val &rest keys)
(if val
(gits-shell "git-config %s %s" (gits-concat-with-delim "." keys) val)
(gits-shell "git-config --unset %s" (gits-concat-with-delim "." keys))))
2008-08-01 13:37:54 +02:00
(defun gits-get-top-dir (cwd)
(let* ((cwd (expand-file-name cwd))
(git-dir (gits-shell "cd '%s' && git-rev-parse --git-dir 2>/dev/null"
cwd)))
(if git-dir
(file-name-as-directory (or (file-name-directory git-dir) cwd))
nil)))
2008-08-03 20:33:25 +02:00
(defun gits-get-ref (ref)
(gits-shell "git-symbolic-ref -q %s" ref))
(defun gits-get-current-branch ()
(let* ((head (gits-get-ref "HEAD"))
(pos (and head (string-match "^refs/heads/" head))))
(if pos
(substring head 11)
nil)))
(defun gits-read-top-dir (prefix)
(let ((dir (gits-get-top-dir default-directory)))
(if prefix
(gits-get-top-dir (read-directory-name "Git repository: " dir))
dir)))
2008-08-01 13:37:54 +02:00
2008-08-04 01:05:02 +02:00
(defun gits-insert-output (title washer cmd &rest args)
2008-08-04 02:52:16 +02:00
(insert title "\n")
(let* ((beg (point))
(status (apply 'call-process cmd nil t nil args))
(end (point)))
(if washer
(save-restriction
(narrow-to-region beg (point))
(funcall washer status)
(goto-char (point-max))
(insert "\n")))))
2008-08-03 21:50:39 +02:00
2008-08-04 01:05:02 +02:00
(defun gits-put-line-property (prop val)
(put-text-property (line-beginning-position) (line-end-position)
prop val))
2008-08-01 15:04:52 +02:00
;;; Running asynchronous commands
(defvar gits-process nil)
(defun gits-run (cmd &rest args)
(or (not gits-process)
(error "Git is already running."))
(let ((dir default-directory)
(buf (get-buffer-create "*git-process*")))
(save-excursion
(set-buffer buf)
(setq default-directory dir)
(erase-buffer)
2008-08-03 21:29:08 +02:00
(insert "$ " (gits-concat-with-delim " " (cons cmd args)) "\n")
2008-08-01 15:04:52 +02:00
(setq gits-process (apply 'start-process "git" buf cmd args))
(set-process-sentinel gits-process 'gits-process-sentinel))))
(defun gits-process-sentinel (process event)
(cond ((string= event "finished\n")
(message "Git finished.")
(setq gits-process nil))
((string= event "killed\n")
(message "Git was killed.")
(setq gits-process nil))
((string-match "exited abnormally" event)
(message "Git failed.")
(setq gits-process nil))
(t
(message "Git is weird.")))
(gits-update-status))
2008-08-03 21:29:08 +02:00
(defun gits-display-process ()
(interactive)
(display-buffer "*git-process*"))
2008-08-03 21:10:00 +02:00
;;; Keymap
(defvar gits-keymap nil)
(when (not gits-keymap)
(setq gits-keymap (make-keymap))
(suppress-keymap gits-keymap)
(define-key gits-keymap (kbd "g") 'git-status)
(define-key gits-keymap (kbd "S") 'git-stage-all)
2008-08-04 01:05:02 +02:00
(define-key gits-keymap (kbd "a") 'gits-add-thing-at-point)
2008-08-04 03:13:23 +02:00
(define-key gits-keymap (kbd "u") 'gits-unstage-thing-at-point)
2008-08-04 01:05:02 +02:00
(define-key gits-keymap (kbd "i") 'gits-ignore-thing-at-point)
2008-08-04 02:26:34 +02:00
(define-key gits-keymap (kbd "RET") 'gits-visit-thing-at-point)
(define-key gits-keymap (kbd "c") 'git-log-edit)
2008-08-03 21:29:08 +02:00
(define-key gits-keymap (kbd "p") 'gits-display-process))
2008-08-03 21:10:00 +02:00
2008-08-01 15:04:52 +02:00
;;; Status
2008-08-04 01:05:02 +02:00
(defun gits-wash-other-files (status)
(goto-char (point-min))
(while (not (eobp))
(let ((filename (buffer-substring (point) (line-end-position))))
(insert " ")
(gits-put-line-property 'face '(:foreground "red"))
(gits-put-line-property 'gits-info (list 'other-file filename)))
(forward-line)
(beginning-of-line)))
(defun gits-wash-diff-propertize-diff (head-beg head-end)
(let ((head-end (or head-end (point))))
(when head-beg
(put-text-property head-beg head-end
'gits-info (list 'diff
head-beg (point))))))
(defun gits-wash-diff-propertize-hunk (head-beg head-end hunk-beg)
(when hunk-beg
(put-text-property hunk-beg (point)
'gits-info (list 'hunk
head-beg head-end
hunk-beg (point)))))
2008-08-04 14:54:08 +02:00
2008-08-04 02:26:34 +02:00
(defun gits-wash-diff (status)
(goto-char (point-min))
(let ((n-files 1)
2008-08-04 02:52:16 +02:00
(head-beg nil)
(head-end nil)
(hunk-beg nil))
2008-08-04 02:26:34 +02:00
(while (not (eobp))
(let ((prefix (buffer-substring-no-properties
(point) (+ (point) n-files))))
2008-08-04 02:52:16 +02:00
(cond ((looking-at "^diff")
(gits-wash-diff-propertize-diff head-beg head-end)
2008-08-04 02:52:16 +02:00
(setq head-beg (point))
(setq head-end nil))
((looking-at "^@+")
2008-08-04 02:26:34 +02:00
(setq n-files (- (length (match-string 0)) 1))
(if (null head-end)
(setq head-end (point)))
(gits-wash-diff-propertize-hunk head-beg head-end hunk-beg)
2008-08-04 02:52:16 +02:00
(setq hunk-beg (point)))
2008-08-04 02:26:34 +02:00
((string-match "\\+" prefix)
(gits-put-line-property 'face '(:foreground "blue1")))
((string-match "-" prefix)
(gits-put-line-property 'face '(:foreground "red")))))
(forward-line)
(beginning-of-line))
(gits-wash-diff-propertize-diff head-beg head-end)
(gits-wash-diff-propertize-hunk head-beg head-end hunk-beg)))
2008-08-04 02:26:34 +02:00
2008-08-01 15:04:52 +02:00
(defun gits-update-status ()
(let ((buf (get-buffer "*git-status*")))
(save-excursion
(set-buffer buf)
2008-08-03 21:10:00 +02:00
(setq buffer-read-only t)
(use-local-map gits-keymap)
(let ((inhibit-read-only t))
(erase-buffer)
(let ((branch (gits-get-current-branch)))
2008-08-03 22:04:18 +02:00
(insert (format "Local: %s %s\n"
(or branch "(detached)")
(abbreviate-file-name default-directory)))
2008-08-03 21:10:00 +02:00
(if branch
(let ((remote (gits-get "branch" branch "remote")))
(if remote
(let* ((push (gits-get "remote" remote "push"))
(pull (gits-get "remote" remote "fetch"))
(desc (if (equal push pull)
push
2008-08-03 22:04:18 +02:00
(format "%s>%s" pull push))))
(insert (format "Remote: %s %s\n"
2008-08-03 21:10:00 +02:00
desc
(gits-get "remote" remote "url"))))))))
(insert "\n")
2008-08-04 01:05:02 +02:00
(gits-insert-output "Untracked files:" 'gits-wash-other-files
"git" "ls-files" "--others" "--exclude-standard")
2008-08-04 02:26:34 +02:00
(gits-insert-output "Local changes:" 'gits-wash-diff
2008-08-03 21:50:39 +02:00
"git" "diff")
2008-08-04 02:26:34 +02:00
(gits-insert-output "Staged changes:" 'gits-wash-diff
2008-08-03 21:50:39 +02:00
"git" "diff" "--cached")))))
2008-08-01 15:04:52 +02:00
2008-08-03 20:33:25 +02:00
(defun git-status (dir)
(interactive (list (gits-read-top-dir current-prefix-arg)))
(let ((buf (get-buffer-create "*git-status*")))
(switch-to-buffer buf)
(setq default-directory dir)
(gits-update-status)))
2008-08-01 13:37:54 +02:00
2008-08-04 01:05:02 +02:00
;;; Staging
2008-08-01 15:04:52 +02:00
2008-08-04 03:10:48 +02:00
(defun gits-write-hunk-patch (info file)
(write-region (elt info 1) (elt info 2) file)
(write-region (elt info 3) (elt info 4) file t))
2008-08-04 01:05:02 +02:00
(defun gits-add-thing-at-point ()
2008-08-01 15:04:52 +02:00
(interactive)
2008-08-04 01:05:02 +02:00
(let ((info (get-char-property (point) 'gits-info)))
(if info
(case (car info)
((other-file)
2008-08-04 03:10:48 +02:00
(gits-run "git" "add" (cadr info)))
((hunk)
(gits-write-hunk-patch info ".git/gits-tmp")
(gits-run "git" "apply" "--cached" ".git/gits-tmp"))))))
2008-08-01 15:04:52 +02:00
2008-08-04 03:13:23 +02:00
(defun gits-unstage-thing-at-point ()
(interactive)
(let ((info (get-char-property (point) 'gits-info)))
(if info
(case (car info)
((hunk)
(gits-write-hunk-patch info ".git/gits-tmp")
(gits-run "git" "apply" "--cached" "--reverse" ".git/gits-tmp"))))))
2008-08-04 01:05:02 +02:00
(defun gits-ignore-thing-at-point ()
2008-08-01 15:04:52 +02:00
(interactive)
2008-08-04 01:05:02 +02:00
(let ((info (get-char-property (point) 'gits-info)))
(if info
(case (car info)
((other-file)
(append-to-file (concat (cadr info) "\n") nil ".gitignore")
(gits-update-status))))))
2008-08-04 02:26:34 +02:00
(defun gits-visit-thing-at-point ()
(interactive)
(let ((info (get-char-property (point) 'gits-info)))
(message "visit %S" info)
(if info
(case (car info)
((other-file)
(find-file (cadr info)))))))
2008-08-04 01:05:02 +02:00
;;; Push and pull
(defstruct gits-remote-info
nick url push pull dirty-p)
(defun gits-get-remote-config (nick)
(let* ((branch (gits-get-current-branch))
(nick (or nick
(and branch (gits-get "branch" branch "remote"))
"origin"))
(url (gits-get "remote" nick "url"))
(push (gits-get "remote" nick "push"))
(pull (gits-get "remote" nick "fetch")))
(make-gits-remote-info :nick nick :url url :push push :pull pull
:dirty-p nil)))
(defun gits-set-remote-config (config)
(let ((nick (gits-remote-info-nick config)))
(let ((branch (gits-get-current-branch)))
(if branch
(gits-set nick "branch" branch "remote")))
(gits-set (gits-remote-info-url config) "remote" nick "url")
(gits-set (gits-remote-info-push config) "remote" nick "push")
(gits-set (gits-remote-info-pull config) "remote" nick "fetch")))
(defun gits-remote-info-complete (conf field prompt)
(when (or current-prefix-arg
(null (funcall field conf)))
(let ((url (read-string (format prompt (gits-remote-info-nick conf))
(funcall field conf))))
(eval `(setf (,field ',conf) ',url))
(setf (gits-remote-info-dirty-p conf) t))))
(defun gits-read-push-pull-args (push-p)
(let ((conf (gits-get-remote-config nil)))
(when current-prefix-arg
(let ((nick (read-string "Remote repository nickname: "
(gits-remote-info-nick conf))))
(setq conf (gits-get-remote-config nick))
(setf (gits-remote-info-dirty-p conf) t)))
(gits-remote-info-complete conf 'gits-remote-info-url
"Repository url of '%s': ")
(if push-p
(gits-remote-info-complete conf 'gits-remote-info-push
"Branch of '%s' to push to: ")
(gits-remote-info-complete conf 'gits-remote-info-pull
"Branch of '%s' to pull from: "))
(if (and (gits-remote-info-dirty-p conf)
(y-or-n-p "Save as defaults? "))
(gits-set-remote-config conf))
(list (gits-remote-info-url conf)
(if push-p
(gits-remote-info-push conf)
(gits-remote-info-pull conf)))))
(defun git-pull (nick branch)
(interactive (gits-read-push-pull-args nil))
(gits-run "git" "pull" nick branch))
(defun git-push (nick branch)
(interactive (gits-read-push-pull-args t))
(gits-run "git" "push" nick branch))
;;; Commit
2008-08-01 15:04:52 +02:00
2008-08-04 14:35:25 +02:00
(defvar gits-log-edit-map nil)
(when (not gits-log-edit-map)
(setq gits-log-edit-map (make-sparse-keymap))
(define-key gits-log-edit-map (kbd "C-c C-c") 'gits-log-edit-commit))
2008-08-04 14:35:25 +02:00
(defvar gits-pre-log-edit-window-configuration nil)
2008-08-04 14:35:25 +02:00
(defun gits-log-edit-commit ()
2008-08-04 14:35:25 +02:00
(interactive)
(if (> (buffer-size) 0)
(write-region (point-min) (point-max) ".git/gits-log")
(write-region "(Empty description)" nil ".git/gits-log"))
(erase-buffer)
2008-08-04 14:35:25 +02:00
(gits-run "git-commit" "-F" ".git/gits-log")
(bury-buffer)
(when gits-pre-log-edit-window-configuration
(set-window-configuration gits-pre-log-edit-window-configuration)
(setq gits-pre-log-edit-window-configuration nil)))
2008-08-04 14:35:25 +02:00
(defun git-log-edit ()
2008-08-01 15:04:52 +02:00
(interactive)
2008-08-04 14:35:25 +02:00
(let ((dir default-directory)
(buf (get-buffer-create "*git-log-edit*")))
(setq gits-pre-log-edit-window-configuration (current-window-configuration))
2008-08-04 14:35:25 +02:00
(pop-to-buffer buf)
(setq default-directory dir)
(use-local-map gits-log-edit-map)
(message "Use C-c C-c when done.")))
2008-08-04 01:05:02 +02:00
;;; Misc
(defun git-stage-all ()
(interactive)
(gits-run "git-add" "-u" "."))