Skip to content

Commit

Permalink
Add basic branches management #63
Browse files Browse the repository at this point in the history
  • Loading branch information
thierryvolpiatto committed Aug 28, 2021
1 parent d861fb4 commit 79ea39c
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions helm-ls-git.el
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ If you want to use magit use `magit-status-setup-buffer' and not
(make-obsolete-variable 'helm-ls-git-grep-command 'helm-grep-git-grep-command "1.8.0")

(defcustom helm-ls-git-default-sources '(helm-source-ls-git-status
helm-ls-git-branches-source
helm-source-ls-git-buffers
helm-source-ls-git)
"Default sources for `helm-ls-git-ls'."
Expand Down Expand Up @@ -154,6 +155,16 @@ See Issue #52."
"Files which contain rebase/merge conflicts."
:group 'helm-ls-git)

(defface helm-ls-git-branches-current
'((t :foreground "gold"))
"Color of the start prefixing current branch."
:group 'helm-ls-git)

(defface helm-ls-git-branches-name
'((t :foreground "red"))
"Color of branches names."
:group 'helm-ls-git)


(defvar helm-ls-git-map
(let ((map (make-sparse-keymap)))
Expand Down Expand Up @@ -441,6 +452,67 @@ See docstring of `helm-ls-git-ls-switches'.
(goto-char (point-min))
(diff-mode))


;;; Git branch basic management
;;
(defvar helm-ls-git-branches-show-all nil)

(defun helm-ls-git-collect-branches (&optional arg)
(helm-aif (helm-ls-git-root-dir)
(with-helm-default-directory it
(with-output-to-string
(with-current-buffer standard-output
(cond ((null arg)
;; Only local branches.
(apply #'call-process "git" nil t nil '("branch")))
(t
(apply #'call-process "git" nil t nil '("branch" "-a")))))))
""))

(defun helm-ls-git-branches-toggle-show-all ()
(interactive)
(setq helm-ls-git-branches-show-all (not helm-ls-git-branches-show-all))
(helm-force-update))

(defvar helm-ls-git-branches-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(define-key map (kbd "C-c b") 'helm-ls-git-branches-toggle-show-all)
map))

(defun helm-ls-git-check-out (candidate)
(with-helm-current-buffer
(let* ((branch (replace-regexp-in-string "[ ]" "" candidate))
(real (replace-regexp-in-string "\\`\\*" "" branch)))
(if (string-match "\\`[*]" candidate)
(message "Already on %s branch" real)
(let ((status (apply #'call-process
"git" nil nil nil
`("checkout" "-q" ,real))))
(if (= status 0)
(message "Switched to %s branch" real)
(error "Process exit with non zero status")))))))

(defun helm-ls-git-branches-transformer (candidates)
(cl-loop for c in candidates
collect (if (string-match "\\`\\([*]\\)\\(.*\\)" c)
(format "%s%s"
(propertize (match-string 1 c)
'face 'helm-ls-git-branches-current)
(propertize (match-string 2 c)
'face 'helm-ls-git-branches-name))
(propertize c 'face '((:foreground "red"))))))

(defvar helm-ls-git-branches-source
(helm-build-in-buffer-source "Git branches"
:init (lambda ()
(let ((data (helm-ls-git-collect-branches
helm-ls-git-branches-show-all)))
(helm-init-candidates-in-buffer 'global data)))
:candidate-transformer 'helm-ls-git-branches-transformer
:action '(("Checkout" . helm-ls-git-check-out))
:keymap 'helm-ls-git-branches-map))


(defun helm-ls-git-status ()
(when (and helm-ls-git-log-file
Expand Down

0 comments on commit 79ea39c

Please sign in to comment.