summary refs log tree commit diff
path: root/emacs/custom/fcuny-git.el
blob: 7cc0feb8c8ecd4830fdb1f94b2f3cce0f36bf209 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(require 'fcuny-defuns)
(require 'fcuny-vars)

(use-package gitconfig-mode
  :ensure t)

(use-package gitattributes-mode
  :ensure t)

(use-package gitignore-mode
  :ensure t)

(use-package magit
  :ensure t
  :after (flyspell)
  :bind (("C-x g" . magit-status))
  :custom
  (vc-follow-symlinks t)
  :config
  ;; if we're on darwin, we're on a work laptop, so let's make sure we
  ;; use the proper `git' binary.
  (when (string= system-type "darwin")
    (setq magit-git-executable "/opt/twitter_mde/bin/git"))
  ;; I want to see these things by default
  (add-to-list 'magit-section-initial-visibility-alist '(untracked . show))
  (add-to-list 'magit-section-initial-visibility-alist '(unstaged . show))
  (add-to-list 'magit-section-initial-visibility-alist '(unpulled . show))
  (add-to-list 'magit-section-initial-visibility-alist '(unpushed . show))
  (add-to-list 'magit-section-initial-visibility-alist '(stashes . show))
  ;; no need to show the last 10 commits, 5 is enough.
  (setq magit-log-section-commit-count 5)
  ;; when working with source, some of the hooks are extremely
  ;; expensive and don't add much values (i.e tags). Let's reduce the
  ;; list to things that are actually useful. This still takes ~10
  ;; seconds when running `magit-status'.
  (setq git-commit-setup-hook
        '(git-commit-save-message
          git-commit-turn-on-auto-fill
          git-commit-turn-on-flyspell
          git-commit-propertize-diff
          with-editor-usage-message))
  (setq magit-refs-sections-hook
      '(magit-insert-error-header
        magit-insert-branch-description
        magit-insert-local-branches))
  (setq magit-status-sections-hook
        '(magit-insert-status-headers
          magit-insert-merge-log
          magit-insert-rebase-sequence
          magit-insert-am-sequence
          magit-insert-sequencer-sequence
          magit-insert-untracked-files
          magit-insert-unstaged-changes
          magit-insert-staged-changes
          magit-insert-stashes))
  (setq magit-status-headers-hook
      '(magit-insert-error-header
        magit-insert-diff-filter-header
        magit-insert-head-branch-header)))

(use-package magit-repos
  :ensure nil
  :after (magit)
  :custom
  (magit-repository-directories '(("~/workspace" . 1))))

(use-package git-commit
  :ensure t
  :after magit
  :hook (git-commit-mode . fcuny/git-commit-auto-fill)
  :custom
  (git-commit-summary-max-length 50)
  :preface
  (defun fcuny/git-commit-auto-fill ()
    "Ensures that the commit body does not exceed 72 characters."
    (setq-local fill-column 72)
    (setq-local comment-auto-fill-only-comments nil)))

;; from https://sideshowcoder.com/2020/07/02/opening-sourcegraph-from-emacs/
;; in a repo, add the following in .git/config:
;;
;; [git-link]
;;        remote = mysourcegraph.sourcegraph
;; [remote "mysourcegraph.sourcegraph"]
;;        url = https://sourcegraph.twitter.biz/gitpuppet.twitter.biz/puppet-twitter
;;
(use-package git-link
  :ensure t
  :config
  (defun git-link-sourcegraph (hostname dirname filename _branch commit start end)
    (let ((line-or-range (if end (format "%s-%s" start end) start)))
      (format "https://%s/%s@%s/-/blob/%s#L%s"
              hostname
              dirname
              commit
              filename
              line-or-range)))

  (defun git-link-commit-sourcegraph (hostname dirname commit)
    (format "https://%s/%s/-/commit/%s"
            hostname
            dirname
            commit))

  (add-to-list 'git-link-remote-alist '("sourcegraph" git-link-sourcegraph))
  (add-to-list 'git-link-commit-remote-alist '("sourcegraph" git-link-commit-sourcegraph))

  (setq git-link-open-in-browser 't))

;; https://magit.vc/manual/magit/Per_002dRepository-Configuration.html
;; we don't want to refresh buffers in source. This should help with
;; performances.
(dir-locals-set-class-variables 'huge-git-repository
                                '((nil . ((magit-refresh-buffers . nil)))))

(dir-locals-set-directory-class
 "/Users/fcuny/workspace/source" 'huge-git-repository)

;; https://magit.vc/manual/magit/Performance.html
;; disable Git from the VC mode, since we use magit. This should help
;; with performances.
(setq vc-handled-backends (delq 'Git vc-handled-backends))

(defun fcuny/clone-repo (url)
  "Clone a repository in the workspace"
  (interactive "sURL:")
  (let* ((repo-name (magit-clone--url-to-name url))
         (target-dir (concat fcuny/path-workspace "/" repo-name)))
    (magit-clone-regular url target-dir nil)))

(provide 'fcuny-git)