Skip to content

Commit

Permalink
Merge branch 'main' of github.com:lem-project/lem
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxxr committed Jul 15, 2024
2 parents 77a8f8d + ebd6acb commit e16eddd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
33 changes: 33 additions & 0 deletions extensions/legit/legit.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Currently Git-only. Concretely, this calls Git with the -w option.")
(define-key lem/peek-legit:*peek-legit-keymap* "M-n" 'legit-next-header)
(define-key lem/peek-legit:*peek-legit-keymap* "M-p" 'legit-previous-header)
(define-key *legit-diff-mode-keymap* "Tab" 'next-window)
(define-key *legit-diff-mode-keymap* "Return" 'legit-jump-to-hunk)

;; help
(define-key lem/peek-legit:*peek-legit-keymap* "?" 'legit-help)
Expand Down Expand Up @@ -389,6 +390,38 @@ Currently Git-only. Concretely, this calls Git with the -w option.")
point
start))))

(define-command legit-jump-to-hunk () ()
"Jump to the corresponding line in the source file for the current hunk."
(let ((first-line (with-point ((p (current-point)))
(buffer-start p)
(line-string p))))
(when (str:starts-with-p "diff" first-line)
(let* ((hunk-text (%current-hunk))
(lines (str:lines hunk-text))
(file-line (find-if (lambda (line) (str:starts-with? "+++ b/" line)) lines))
(hunk-header (find-if (lambda (line) (str:starts-with? "@@ " line)) lines)))
(if (and file-line hunk-header)
(let* ((relative-file (if (str:starts-with-p "diff --git" first-line)
(subseq file-line 6) ; Remove "+++ b/" prefix for Git
(cl-ppcre:register-groups-bind (file-path)
("\\+\\+\\+ b/([^\\t]+)" file-line)
file-path))) ; For Mercurial (also has datetime)
(start-line (cl-ppcre:register-groups-bind (nil line)
("@@ -(\\d+),\\d+ \\+(\\d+)" hunk-header)
line))
(target-line (when start-line
(+
(parse-integer start-line :junk-allowed t)
lem/porcelain:*diff-context-lines*))))
(if (and relative-file target-line)
(with-current-project ()
(let ((absolute-file (merge-pathnames relative-file (uiop:getcwd))))
(lem/peek-legit:quit)
(find-file (namestring absolute-file))
(goto-line target-line)))
(message "Could not determine file or line number")))
(message "Could not parse hunk information"))))))

(defparameter *commit-buffer-message*
"~%# Please enter the commit message for your changes.~%~
# Lines starting with '#' will be discarded, and an empty message does nothing.~%~
Expand Down
17 changes: 12 additions & 5 deletions extensions/legit/porcelain.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
:stage
:unstage
:vcs-project-p
:*diff-context-lines*
:commits-log
:*commits-log-page-size*
:commit-count)
Expand Down Expand Up @@ -84,6 +85,9 @@ Mercurial:
For staged files, --cached is added by the command.")

(defvar *diff-context-lines* 4
"How many lines of context before/after the first committed line")

(defvar *vcs* nil
"git, fossil? For current project. Bind this in the caller.
Expand Down Expand Up @@ -341,16 +345,19 @@ allows to learn about the file state: modified, deleted, ignored… "
(run-git
(concatenate 'list
*file-diff-args*
(list (format nil "-U~D" *diff-context-lines*))
(if cached '("--cached"))
(list file))))

(defun hg-file-diff (file &key cached)
"Show the diff of staged files (and only them)."
(when cached
(run-hg (list "diff" file))
;; files not staged can't be diffed.
;; We could read and display their full content anyways?
))
(run-hg (list "diff"
(format nil "-U~D" *diff-context-lines*)
file))
;; files not staged can't be diffed.
;; We could read and display their full content anyways?
))

(defun fossil-file-diff (file &key cached)
(declare (ignorable cached))
Expand Down Expand Up @@ -607,7 +614,7 @@ summary: test
(parse-integer
(str:trim (run-hg '("id" "--num" "--rev" "tip")))))
(:fossil
(length))
(fossil-commit-count))
(t
(porcelain-error "commit-count not implemented for VCS: ~a" *vcs*))))

Expand Down

0 comments on commit e16eddd

Please sign in to comment.