Redone diffing, logging, and resetting.

Provide generic support for revisions and ranges of revisions as
arguments to interactive functions.
This commit is contained in:
Marius Vollmer 2008-08-17 00:37:50 +03:00
parent 5072939384
commit 7d0da4a5bd

239
magit.el
View file

@ -119,6 +119,78 @@
(put-text-property (line-beginning-position) (line-end-position) (put-text-property (line-beginning-position) (line-end-position)
prop val)) prop val))
;;; Revisions and ranges
(defun magit-list-interesting-revisions ()
(magit-shell-lines "git branch -a | cut -c3-"))
(defun magit-read-rev (prompt &optional def)
(let ((rev (completing-read prompt (magit-list-interesting-revisions)
nil nil nil nil def)))
(if (string= rev "")
nil
rev)))
(defun magit-read-rev-range (op &optional def-beg def-end)
(if current-prefix-arg
(read-string (format "%s range: " op))
(let ((beg (magit-read-rev
(format "%s start%s: "
op
(if def-beg
(format " (default %s)"
(magit-rev-describe def-beg))
""))
def-beg)))
(if (not beg)
nil
(let ((end (magit-read-rev
(format "%s end%s: "
op
(if def-end
(format " (default %s)"
(magit-rev-describe def-end))
""))
def-end)))
(cons beg end))))))
(defun magit-rev-to-git (rev)
(or rev
(error "No revision specified"))
(if (string= rev ".")
(or (magit-marked-object)
(error "Commit mark not set"))
rev))
(defun magit-rev-range-to-git (range)
(or range
(error "No revision range specified"))
(if (stringp range)
range
(if (cdr range)
(format "%s..%s"
(magit-rev-to-git (car range))
(magit-rev-to-git (cdr range)))
(format "%s" (magit-rev-to-git (car range))))))
(defun magit-rev-describe (rev)
(or rev
(error "No revision specified"))
(if (string= rev ".")
"mark"
(magit-name-rev rev)))
(defun magit-rev-range-describe (range things)
(or range
(error "No revision range specified"))
(if (stringp range)
(format "%s in %s" things range)
(if (cdr range)
(format "%s from %s to %s" things
(magit-rev-describe (car range))
(magit-rev-describe (cdr range)))
(format "%s at %s" things (magit-rev-describe (car range))))))
;;; Sections ;;; Sections
(defun magit-insert-section (section title washer cmd &rest args) (defun magit-insert-section (section title washer cmd &rest args)
@ -236,17 +308,17 @@
(define-key map (kbd "S") 'magit-stage-all) (define-key map (kbd "S") 'magit-stage-all)
(define-key map (kbd "u") 'magit-unstage-thing-at-point) (define-key map (kbd "u") 'magit-unstage-thing-at-point)
(define-key map (kbd "i") 'magit-ignore-thing-at-point) (define-key map (kbd "i") 'magit-ignore-thing-at-point)
(define-key map (kbd "^") 'magit-describe-thing-at-point) (define-key map (kbd "?") 'magit-describe-thing-at-point)
(define-key map (kbd ".") 'magit-mark-thing-at-point) (define-key map (kbd ".") 'magit-mark-thing-at-point)
(define-key map (kbd "=") 'magit-diff-with-mark) (define-key map (kbd "=") 'magit-diff-with-mark)
(define-key map (kbd "?") 'magit-log-commit) (define-key map (kbd "l") 'magit-log-head)
(define-key map (kbd "a") 'magit-apply-commit) (define-key map (kbd "L") 'magit-log)
(define-key map (kbd "v") 'magit-revert-commit) (define-key map (kbd "d") 'magit-diff-head)
(define-key map (kbd "H") 'magit-checkout-commit) (define-key map (kbd "D") 'magit-diff)
(define-key map (kbd "x") 'magit-reset-soft) (define-key map (kbd "x") 'magit-reset-head)
(define-key map (kbd "X") 'magit-reset-hard) (define-key map (kbd "X") 'magit-reset-working-tree)
(define-key map (kbd "RET") 'magit-visit-thing-at-point) (define-key map (kbd "RET") 'magit-visit-thing-at-point)
(define-key map (kbd "b") 'magit-switch-branch) (define-key map (kbd "b") 'magit-checkout)
(define-key map (kbd "B") 'magit-create-branch) (define-key map (kbd "B") 'magit-create-branch)
(define-key map (kbd "m") 'magit-manual-merge) (define-key map (kbd "m") 'magit-manual-merge)
(define-key map (kbd "M") 'magit-automatic-merge) (define-key map (kbd "M") 'magit-automatic-merge)
@ -255,11 +327,7 @@
(define-key map (kbd "P") 'magit-push) (define-key map (kbd "P") 'magit-push)
(define-key map (kbd "c") 'magit-log-edit) (define-key map (kbd "c") 'magit-log-edit)
(define-key map (kbd "C") 'magit-add-log) (define-key map (kbd "C") 'magit-add-log)
(define-key map (kbd "l") 'magit-browse-log)
(define-key map (kbd "L") 'magit-browse-branch-log)
(define-key map (kbd "d") 'magit-diff-with-branch)
(define-key map (kbd "p") 'magit-display-process) (define-key map (kbd "p") 'magit-display-process)
(define-key map (kbd "q") 'magit-quit)
map)) map))
(defvar magit-mode-hook nil) (defvar magit-mode-hook nil)
@ -592,39 +660,37 @@ pushed.
;;; Branches ;;; Branches
(defun magit-list-branches () (defun magit-checkout (rev)
(magit-shell-lines "git branch -a | cut -c3-")) (interactive (list (magit-read-rev "Switch to: ")))
(if rev
(defun magit-read-rev (prompt) (magit-run "git" "checkout" (magit-rev-to-git rev))))
(completing-read prompt (magit-list-branches)))
(defun magit-switch-branch (branch)
(interactive (list (magit-read-rev "Switch to branch: ")))
(if (and branch (not (string= branch "")))
(magit-run "git" "checkout" branch)))
(defun magit-read-create-branch-args () (defun magit-read-create-branch-args ()
(let* ((branches (magit-list-branches)) (let* ((cur-branch (magit-get-current-branch))
(cur-branch (magit-get-current-branch))
(branch (read-string "Create branch: ")) (branch (read-string "Create branch: "))
(parent (completing-read "Parent: " branches nil t cur-branch))) (parent (magit-read-rev "Parent: " cur-branch)))
(list branch parent))) (list branch parent)))
(defun magit-create-branch (branch parent) (defun magit-create-branch (branch parent)
(interactive (magit-read-create-branch-args)) (interactive (magit-read-create-branch-args))
(if (and branch (not (string= branch "")) (if (and branch (not (string= branch ""))
parent (not (string= parent ""))) parent)
(magit-run "git" "checkout" "-b" branch parent))) (magit-run "git" "checkout" "-b"
branch
(magit-rev-to-git parent))))
;;; Merging ;;; Merging
(defun magit-manual-merge (branch) (defun magit-manual-merge (rev)
(interactive (list (magit-read-rev "Manually merge from branch: "))) (interactive (list (magit-read-rev "Manually merge: ")))
(magit-run "git" "merge" "--no-ff" "--no-commit" branch)) (if rev
(magit-run "git" "merge" "--no-ff" "--no-commit"
(magit-rev-to-git rev))))
(defun magit-automatic-merge (branch) (defun magit-automatic-merge (rev)
(interactive (list (magit-read-rev "Merge from branch: "))) (interactive (list (magit-read-rev "Merge: ")))
(magit-run "git" "merge" branch)) (if rev
(magit-run "git" "merge" (magit-rev-to-git branch))))
;;; Rebasing ;;; Rebasing
@ -644,7 +710,9 @@ pushed.
(interactive) (interactive)
(let ((info (magit-rebase-info))) (let ((info (magit-rebase-info)))
(if (not info) (if (not info)
(magit-run "git" "rebase" (magit-read-rev "Rebase against: ")) (let ((rev (magit-read-rev "Rebase to: ")))
(if rev
(magit-run "git" "rebase" (magit-read-rev "Rebase to: "))))
(let ((cursor-in-echo-area t) (let ((cursor-in-echo-area t)
(message-log-max nil)) (message-log-max nil))
(message "Rebase in progress. Abort, Skip, or Continue? ") (message "Rebase in progress. Abort, Skip, or Continue? ")
@ -659,16 +727,14 @@ pushed.
;;; Resetting ;;; Resetting
(defun magit-reset-soft (target) (defun magit-reset-head (rev)
(interactive (list (magit-commit-at-point))) (interactive (list (magit-read-rev "Reset head to: ")))
(magit-run "git" "reset" "--soft" target)) (if rev
(magit-run "git" "reset" "--soft" (magit-rev-to-git rev))))
(defun magit-reset-hard (target) (defun magit-reset-working-tree ()
(interactive (list (magit-commit-at-point))) (if (yes-or-no-p "Discard all uncommitted changes? ")
(if (yes-or-no-p (magit-run "git" "reset" "--hard")))
(format "Hard reset and throw away all uncommitted changes? "
target))
(magit-run "git" "reset" "--hard" target)))
;;; Push and pull ;;; Push and pull
@ -680,7 +746,7 @@ pushed.
(interactive) (interactive)
(magit-run "git" "push" "-v")) (magit-run "git" "push" "-v"))
;;; Commit ;;; Committing
(defvar magit-log-edit-map nil) (defvar magit-log-edit-map nil)
@ -755,7 +821,7 @@ pushed.
(open-line 1) (open-line 1)
(insert (format "(%s): " fun))))))))) (insert (format "(%s): " fun)))))))))
;;; Commit browsing ;;; Commits
(defun magit-commit-at-point () (defun magit-commit-at-point ()
(let* ((info (get-text-property (point) 'magit-info)) (let* ((info (get-text-property (point) 'magit-info))
@ -814,50 +880,57 @@ pushed.
(magit-put-line-property 'magit-info (list 'commit commit)))) (magit-put-line-property 'magit-info (list 'commit commit))))
(forward-line))) (forward-line)))
(defun magit-browse-log (head) (defun magit-log (range)
(interactive (list (magit-get-current-branch))) (interactive (list (magit-read-rev-range "Log" (magit-get-current-branch))))
(let* ((topdir (magit-get-top-dir default-directory))) (if range
(switch-to-buffer "*magit-log*") (let* ((topdir (magit-get-top-dir default-directory))
(setq default-directory topdir) (args (magit-rev-range-to-git range)))
(magit-mode) (switch-to-buffer "*magit-log*")
(let ((inhibit-read-only t)) (setq default-directory topdir)
(save-excursion (magit-mode)
(erase-buffer) (let ((inhibit-read-only t))
(magit-insert-section 'history (format "History of %s" head) (save-excursion
'magit-wash-log (erase-buffer)
"git" "log" "--graph" "--max-count=10000" (magit-insert-section 'history
"--pretty=oneline" head))) (magit-rev-range-describe range "Commits")
(magit-refresh-marks-in-buffer (current-buffer)))) 'magit-wash-log
"git" "log" "--graph" "--max-count=10000"
"--pretty=oneline" args)))
(magit-refresh-marks-in-buffer (current-buffer)))))
(defun magit-browse-branch-log () (defun magit-log-head ()
(interactive) (interactive)
(magit-browse-log (magit-read-rev "Browse history of branch: "))) (magit-log "HEAD"))
(defun magit-diff-with-mark ()
(interactive)
(let ((commit (magit-commit-at-point))
(marked (or (magit-marked-object)
(error "Nothing marked."))))
(magit-show-diff commit marked)))
;;; Diffing ;;; Diffing
(defun magit-show-diff (&rest args) (defun magit-diff (range)
(let ((dir default-directory) (interactive (list (magit-read-rev-range "Diff")))
(buf (get-buffer-create "*magit-diff*"))) (if range
(display-buffer buf) (let* ((dir default-directory)
(save-excursion (args (magit-rev-range-to-git range))
(set-buffer buf) (buf (get-buffer-create "*magit-diff*")))
(setq buffer-read-only t) (display-buffer buf)
(setq default-directory dir) (save-excursion
(let ((inhibit-read-only t)) (set-buffer buf)
(erase-buffer) (setq buffer-read-only t)
(apply 'magit-insert-section 'diff nil 'magit-wash-diff (setq default-directory dir)
"git" "diff" args))))) (let ((inhibit-read-only t))
(erase-buffer)
(magit-insert-section 'diff
(magit-rev-range-describe range "Changes")
'magit-wash-diff
"git" "diff" args))))))
(defun magit-diff-with-branch (branch) (defun magit-diff-head (rev)
(interactive (list (magit-read-rev "Diff against: "))) (interactive (list (magit-read-rev "Diff with: ")))
(magit-show-diff branch)) (if rev
(magit-diff (cons "HEAD" rev))))
(defun magit-diff-with-mark ()
(interactive)
(magit-diff (cons (magit-marked-object)
(magit-commit-at-point))))
;;; Markers ;;; Markers