/
magit.el
1725 lines (1533 loc) · 66.3 KB
/
magit.el
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; magit.el --- control Git from Emacs
;; Copyright (C) 2008-2015 The Magit Project Developers
;;
;; For a full list of contributors, see the AUTHORS.md file
;; at the top-level directory of this distribution and at
;; https://raw.github.com/magit/magit/master/AUTHORS.md
;; Author: Marius Vollmer <marius.vollmer@gmail.com>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
;; Former-Maintainers:
;; Nicolas Dudebout <nicolas.dudebout@gatech.edu>
;; Peter J. Weisberg <pj@irregularexpressions.net>
;; Phil Jackson <phil@shellarchive.co.uk>
;; Rémi Vanicat <vanicat@debian.org>
;; Yann Hodique <yann.hodique@gmail.com>
;; Keywords: vc tools
;; Magit requires at least GNU Emacs 24.1 and Git 1.9.1.
;; Magit 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.
;;
;; Magit 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 Magit. If not, see http://www.gnu.org/licenses.
;;; Commentary:
;; Invoking the `magit-status' command will show a buffer with the
;; status of the current Git repository and its working tree. 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
;; current HEAD. You can add individual hunks from the working tree
;; to the index, and you can commit the index.
;;
;; See the Magit User Manual for more information.
;;; Code:
(when (version< emacs-version "24.1")
(error "Magit requires at least GNU Emacs 24.1"))
(require 'cl-lib)
(require 'dash)
(define-obsolete-variable-alias 'magit-highlight-indentation 'magit-diff-highlight-indentation)
(define-obsolete-variable-alias 'magit-highlight-trailing-whitespace 'magit-diff-highlight-trailing)
(define-obsolete-variable-alias 'magit-highlight-whitespace 'magit-diff-paint-whitespace)
(define-obsolete-variable-alias 'magit-mode-refresh-buffer-hook 'magit-refresh-buffer-hook)
(define-obsolete-variable-alias 'magit-repo-dirs 'magit-repository-directories)
(define-obsolete-variable-alias 'magit-repo-dirs-depth 'magit-repository-directories-depth)
(define-obsolete-variable-alias 'magit-revert-backup 'magit-apply-backup)
(define-obsolete-variable-alias 'magit-revert-buffer-hook 'magit-after-revert-hook)
(define-obsolete-variable-alias 'magit-show-child-count 'magit-section-show-child-count)
(define-obsolete-variable-alias 'magit-status-refresh-hook 'magit-refresh-status-hook)
(require 'with-editor)
(require 'git-commit)
(require 'git-rebase)
(require 'magit-core)
(require 'magit-diff)
(require 'magit-apply)
(require 'magit-log)
(require 'format-spec)
(require 'package nil t) ; used in `magit-version'
(eval-when-compile (require 'dired-x))
(declare-function dired-jump 'dired-x)
(eval-when-compile (require 'eshell))
(declare-function eshell-parse-arguments 'eshell)
(eval-when-compile (require 'message))
(declare-function message-goto-body 'message)
(eval-when-compile (require 'smerge-mode))
;;; Options
;;;; Status Mode
(defgroup magit-status nil
"Inspect and manipulate Git repositories."
:group 'magit-modes)
(defcustom magit-status-mode-hook nil
"Hook run after entering Magit-Status mode."
:group 'magit-status
:type 'hook)
(defcustom magit-status-headers-hook '(magit-insert-tags-header)
"Hook run to insert headers into the status buffer.
This hook is run by `magit-insert-status-headers', which always
inserts the \"Head\" and \"Upstream\" headers before the headers
listed here. `magit-insert-status-headers' has to be a member
of `magit-insert-status-sections', or no headers are inserted."
:package-version '(magit . "2.1.0")
:group 'magit-status
:type 'hook)
(defcustom 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-bisect-output
magit-insert-bisect-rest
magit-insert-bisect-log
magit-insert-untracked-files
magit-insert-unstaged-changes
magit-insert-staged-changes
magit-insert-stashes
magit-insert-unpulled-commits
magit-insert-unpushed-commits)
"Hook run to insert sections into the status buffer.
This option allows reordering the sections and adding sections
that are by default displayed in other Magit buffers. Doing the
latter is currently not recommended because not all functions
that insert sections have been adapted yet. Only inserters that
take no argument can be used and some functions exist that begin
with the `magit-insert-' prefix but do not insert a section.
Note that there are already plans to improve this and to add
similar hooks for other Magit modes."
:package-version '(magit . "2.1.0")
:group 'magit-status
:type 'hook)
(defcustom magit-status-refresh-hook nil
"Hook run when the status buffer has been refreshed."
:package-version '(magit . "2.1.0")
:group 'magit-status
:type 'hook)
(defcustom magit-status-buffer-switch-function 'pop-to-buffer
"Function used by `magit-status' to switch to the status buffer.
The function is given one argument, the status buffer."
:group 'magit-status
:type '(radio (function-item switch-to-buffer)
(function-item pop-to-buffer)
(function :tag "Other")))
(defcustom magit-status-buffer-name-format "*magit: %a*"
"Name format for buffers used to display a repository's status.
The following `format'-like specs are supported:
%a the absolute filename of the repository toplevel.
%b the basename of the repository toplevel."
:package-version '(magit . "2.1.0")
:group 'magit-status
:type 'string)
;;;; Refs Mode
(defgroup magit-refs nil
"Inspect and manipulate Git branches and tags."
:group 'magit-modes)
(defcustom magit-refs-mode-hook nil
"Hook run after entering Magit-Refs mode."
:group 'magit-refs
:type 'hook)
(defcustom magit-refs-sections-hook
'(magit-insert-branch-description
magit-insert-local-branches
magit-insert-remote-branches
magit-insert-tags)
"Hook run to insert sections into the references buffer."
:package-version '(magit . "2.1.0")
:group 'magit-refs
:type 'hook)
(defcustom magit-refs-buffer-name-format "*magit-refs: %a*"
"Name format for buffers used to display and manage refs.
The following `format'-like specs are supported:
%a the absolute filename of the repository toplevel.
%b the basename of the repository toplevel."
:package-version '(magit . "2.1.0")
:group 'magit-refs
:type 'string)
;;;; Miscellaneous
(defcustom magit-merge-warn-dirty-worktree t
"Whether to warn before merging with a dirty worktree."
:package-version '(magit . "2.1.0")
:group 'magit-commands
:type 'boolean)
(defcustom magit-repository-directories nil
"Directories containing Git repositories.
Magit will look into these directories for Git repositories
and offer them as choices for `magit-status'."
:group 'magit
:type '(repeat string))
(defcustom magit-repository-directories-depth 3
"The maximum depth to look for Git repositories.
When looking for a Git repository below the directories in
`magit-repository-directories', Magit will only descend this
many levels deep."
:group 'magit
:type 'integer)
;;;; Faces
(defface magit-dimmed
'((((class color) (background light)) :foreground "grey50")
(((class color) (background dark)) :foreground "grey50"))
"Face for text that shouldn't stand out."
:group 'magit-faces)
(defface magit-hash
'((((class color) (background light)) :foreground "grey60")
(((class color) (background dark)) :foreground "grey40"))
"Face for the sha1 part of the log output."
:group 'magit-faces)
(defface magit-tag
'((((class color) (background light))
:background "grey80"
:foreground "Goldenrod4")
(((class color) (background dark))
:background "grey30"
:foreground "LightGoldenrod2"))
"Face for tag labels shown in log buffer."
:group 'magit-faces)
(defface magit-branch-remote
'((((class color) (background light))
:background "grey80"
:foreground "DarkOliveGreen4")
(((class color) (background dark))
:background "grey30"
:foreground "DarkSeaGreen2"))
"Face for remote branch head labels shown in log buffer."
:group 'magit-faces)
(defface magit-branch-local
'((((class color) (background light))
:background "grey80"
:foreground "SkyBlue4")
(((class color) (background dark))
:background "grey30"
:foreground "LightSkyBlue1"))
"Face for local branches."
:group 'magit-faces)
(defface magit-branch-current
'((((class color) (background light)) :inherit magit-branch-local :box t)
(((class color) (background dark)) :inherit magit-branch-local :box t))
"Face for local branches."
:group 'magit-faces)
(defface magit-head
'((((class color) (background light)) :inherit magit-branch-local)
(((class color) (background dark)) :inherit magit-branch-local))
"Face for the symbolic ref \"HEAD\"."
:group 'magit-faces)
(defface magit-refname
'((((class color) (background light))
:background "grey75"
:foreground "grey30")
(((class color) (background dark))
:background "grey30"
:foreground "grey80"))
"Face for refnames without a dedicated face."
:group 'magit-faces)
(defface magit-refname-stash
'((t :inherit magit-refname))
"Face for wip refnames."
:group 'magit-faces)
(defface magit-refname-wip
'((t :inherit magit-refname))
"Face for wip refnames."
:group 'magit-faces)
(defface magit-signature-good
'((t :foreground "green"))
"Face for good signatures."
:group 'magit-faces)
(defface magit-signature-bad
'((t :foreground "red"))
"Face for bad signatures."
:group 'magit-faces)
(defface magit-signature-untrusted
'((t :foreground "cyan"))
"Face for good untrusted signatures."
:group 'magit-faces)
(defface magit-cherry-unmatched
'((t :foreground "cyan"))
"Face for unmatched cherry commits.")
(defface magit-cherry-equivalent
'((t :foreground "magenta"))
"Face for equivalent cherry commits.")
(defface magit-filename
'((t :weight normal))
"Face for filenames."
:group 'magit-faces)
;;; Inspect
;;;; Status Mode
(defvar magit-status-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map magit-mode-map)
(define-key map "jz" 'magit-jump-to-stashes)
(define-key map "jn" 'magit-jump-to-untracked)
(define-key map "ju" 'magit-jump-to-unstaged)
(define-key map "js" 'magit-jump-to-staged)
(define-key map "jf" 'magit-jump-to-unpulled)
(define-key map "jp" 'magit-jump-to-unpushed)
map)
"Keymap for `magit-status-mode'.")
(eval-after-load 'dired-x
'(define-key magit-status-mode-map [remap dired-jump] 'magit-dired-jump))
(define-derived-mode magit-status-mode magit-mode "Magit"
"Mode for looking at Git status.
This mode is documented in info node `(magit)Status'.
\\<magit-status-mode-map>\
Type \\[magit-refresh] to refresh the current buffer.
Type \\[magit-dispatch-popup] to see available action popups.
Type \\[magit-section-toggle] to expand or hide the section at point.
\\<magit-hunk-section-map>\
Type \\[magit-diff-visit-file] to visit the change at point.
Type \\[magit-stage] to stage the change at point; \\[magit-unstage] to unstage.
\\<magit-status-mode-map>\
Type \\[magit-commit-popup] to create a commit.
\n\\{magit-status-mode-map}"
:group 'magit-status)
;;;###autoload
(defun magit-status (&optional directory)
"Show the status of the current Git repository in a buffer.
With a prefix argument prompt for a repository to be shown.
With two prefix arguments prompt for an arbitrary directory.
If that directory isn't the root of an existing repository
then offer to initialize it as a new repository."
(interactive
(list (and (or current-prefix-arg (not (magit-get-top-dir)))
(magit-read-repository
(>= (prefix-numeric-value current-prefix-arg) 16)))))
(if directory
(let ((toplevel (magit-toplevel-safe directory)))
(setq directory (file-name-as-directory (expand-file-name directory)))
(if (and toplevel (string-equal directory toplevel))
(magit-status-internal directory)
(when (y-or-n-p
(if toplevel
(format "%s is a repository. Create another in %s? "
toplevel directory)
(format "Create repository in %s? " directory)))
(magit-init directory))))
(magit-status-internal default-directory)))
(put 'magit-status 'interactive-only 'magit-status-internal)
(defun magit-status-internal (default-directory &optional switch-function)
(magit-mode-setup magit-status-buffer-name-format
(or switch-function
magit-status-buffer-switch-function)
#'magit-status-mode
#'magit-status-refresh-buffer))
(defun ido-enter-magit-status ()
"Drop into `magit-status' from file switching.
To make this command available use something like:
(add-hook 'ido-setup-hook
(lambda ()
(define-key ido-completion-map
(kbd \"C-x g\") 'ido-enter-magit-status)))"
(interactive)
(with-no-warnings ; FIXME these are internal variables
(setq ido-exit 'fallback fallback 'magit-status))
(exit-minibuffer))
(defun magit-status-refresh-buffer ()
(magit-git-exit-code "update-index" "--refresh")
(magit-insert-section (status)
(run-hooks 'magit-status-sections-hook))
(run-hooks 'magit-status-refresh-hook))
(defun magit-insert-status-headers (&optional branch upstream)
(unless branch
(setq branch (magit-get-current-branch)))
(-if-let (hash (magit-rev-verify "HEAD"))
(let ((line (magit-rev-format "%h %s" "HEAD")))
(string-match "^\\([^ ]+\\) \\(.*\\)" line)
(magit-bind-match-strings (hash msg) line
(magit-insert-section it (branch (or branch hash))
(unless branch
(setf (magit-section-type it) 'commit))
(magit-insert-heading
(magit-string-pad "Head: " 10)
(propertize hash 'face 'magit-hash) " "
(and branch
(concat (propertize branch 'face 'magit-branch-local) " "))
msg "\n")
(when (or upstream (setq upstream (magit-get-tracked-branch branch)))
(setq line (or (magit-rev-format "%h %s" upstream) ""))
(string-match "^\\([^ ]+\\) \\(.*\\)" line)
(magit-bind-match-strings (hash msg) line
(magit-insert-section (branch upstream)
(magit-insert
(concat
(magit-string-pad "Upstream: " 10)
(if hash (propertize hash 'face 'magit-hash) "missing") " "
(and (magit-get-boolean "branch" branch "rebase") "onto ")
(propertize
upstream 'face
(if (string= (magit-get "branch" branch "remote") ".")
'magit-branch-local
'magit-branch-remote))
" " msg "\n")))))
(run-hooks 'magit-status-headers-hook)
(insert "\n"))))
(insert "In the beginning there was darkness\n\n")))
(defun magit-insert-tags-header (&optional pad)
(let* ((this-tag (magit-get-current-tag nil t))
(next-tag (magit-get-next-tag nil t))
(this-cnt (cadr this-tag))
(next-cnt (cadr next-tag))
(this-tag (car this-tag))
(next-tag (car next-tag))
(both-tags (and this-tag next-tag t)))
(when (or this-tag next-tag)
(magit-insert-section (tag (or this-tag next-tag))
(magit-insert
(concat
(magit-string-pad (if both-tags "Tags: " "Tag: ") (or pad 10))
(and this-tag (magit-format-status-tag-sentence this-tag this-cnt nil))
(and both-tags ", ")
(and next-tag (magit-format-status-tag-sentence next-tag next-cnt t))
"\n"))))))
(defun magit-format-status-tag-sentence (tag count next)
(concat (propertize tag 'face 'magit-tag)
(and (> count 0)
(format " (%s)"
(propertize (format "%s" count) 'face
(if next 'magit-tag 'magit-branch-local))))))
(defvar magit-untracked-section-map
(let ((map (make-sparse-keymap)))
(define-key map "k" 'magit-discard)
(define-key map "s" 'magit-stage)
map)
"Keymap for the `untracked' section.")
(magit-define-section-jumper untracked "Untracked files")
(defun magit-insert-untracked-files ()
"Maybe insert a list or tree of untracked files.
Do so depending on the value of `status.showUntrackedFiles'."
(let ((show (or (magit-get "status.showUntrackedFiles") "normal")))
(unless (equal show "no")
(if (equal show "all")
(-when-let (files (magit-untracked-files))
(magit-insert-section (untracked)
(magit-insert-heading "Untracked files:")
(magit-insert-untracked-files-1 files nil)
(insert ?\n)))
(-when-let (files (--mapcat (and (eq (aref it 0) ??)
(list (magit-decode-git-path
(substring it 3))))
(magit-git-lines "status" "--porcelain")))
(magit-insert-section (untracked)
(magit-insert-heading "Untracked files:")
(dolist (file files)
(magit-insert-section (file file)
(insert (propertize file 'face 'magit-filename) ?\n))))
(insert ?\n))))))
(defun magit-insert-untracked-files-1 (files directory)
(while (and files (string-prefix-p (or directory "") (car files)))
(let ((dir (file-name-directory (car files))))
(if (equal dir directory)
(let ((file (pop files)))
(magit-insert-section (file file)
(insert (propertize file 'face 'magit-filename) ?\n)))
(magit-insert-section (file dir t)
(insert (propertize dir 'file 'magit-filename) ?\n)
(magit-insert-heading)
(setq files (magit-insert-untracked-files-1 files dir))))))
files)
;;;; Refs Mode
(defvar magit-refs-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map magit-mode-map)
map)
"Keymap for `magit-refs-mode'.")
(define-derived-mode magit-refs-mode magit-mode "Magit Branch"
"Mode which lists and compares references.
This mode is documented in info node `(magit)Branches and Remotes'.
\\<magit-refs-mode-map>\
Type \\[magit-refresh] to refresh the current buffer.
Type \\[magit-branch-popup] to see available branch commands.
Type \\[magit-show-commit] or \\[magit-diff-show-or-scroll-up]\
to visit the commit at point.
Type \\[magit-merge-popup] to merge the branch or commit at point.
Type \\[magit-cherry-pick] to cherry-pick the commit at point.
Type \\[magit-reset-head] to reset HEAD to the commit at point.
\n\\{magit-refs-mode-map}"
:group 'magit-modes)
(magit-define-popup magit-show-refs-popup
"Popup console for `magit-show-refs'."
'magit-popups
:man-page "git-branch"
:switches '((?m "Merged to HEAD" "--merged")
(?M "Merged to master" "--merged=master")
(?n "Not merged to HEAD" "--no-merged")
(?N "Not merged to master" "--no-merged=master"))
:options '((?c "Contains" "--contains=" magit-read-branch-or-commit)
(?m "Merged" "--merged=" magit-read-branch-or-commit)
(?n "Not merged" "--no-merged=" magit-read-branch-or-commit))
:actions '((?y "Show refs, comparing them with HEAD"
magit-show-refs-head)
(?c "Show refs, comparing them with current branch"
magit-show-refs-current)
(?o "Show refs, comparing them with other branch"
magit-show-refs))
:default-action 'magit-show-refs-head
:use-prefix 'popup)
;;;###autoload
(defun magit-show-refs-head (&optional args)
"List and compare references in a dedicated buffer.
Refs are compared with `HEAD'."
(interactive (list (magit-show-refs-arguments)))
(magit-show-refs nil args))
;;;###autoload
(defun magit-show-refs-current (&optional args)
"List and compare references in a dedicated buffer.
Refs are compared with the current branch or `HEAD' if
it is detached."
(interactive (list (magit-show-refs-arguments)))
(magit-show-refs (magit-get-current-branch) args))
;;;###autoload
(defun magit-show-refs (&optional ref args)
"List and compare references in a dedicated buffer.
Refs are compared with a branch read form the user."
(interactive (list (magit-read-other-branch "Compare with")
(magit-show-refs-arguments)))
(magit-mode-setup magit-refs-buffer-name-format nil
#'magit-refs-mode
#'magit-refs-refresh-buffer ref args))
(defun magit-refs-refresh-buffer (&rest ignore)
(magit-insert-section (branchbuf)
(run-hooks 'magit-refs-sections-hook)))
(defconst magit-wash-branch-line-re
(concat "^"
"\\(?:[ \\*]\\) "
"\\(?1:([^)]+)\\|[^ ]+?\\)" ; branch
"\\(?: +\\)"
"\\(?2:[0-9a-fA-F]+\\) " ; sha1
"\\(?:\\["
"\\(?4:[^:]+\\): " ; upstream
"\\(?:"
"\\(?7:gone\\)\\|" ; gone
"\\(?:ahead \\(?5:[0-9]+\\)\\)?" ; ahead
"\\(?:, \\)?"
"\\(?:behind \\(?6:[0-9]+\\)\\)?" ; behind
"\\)"
"\\] \\)?"
"\\(?3:.*\\)")) ; message
(defvar magit-local-branch-format "%4c %-25n %U%m\n")
(defvar magit-remote-branch-format "%4c %-25n %m\n")
(defvar magit-tags-format "%4c %-25n %m\n")
(defvar magit-refs-indent-cherry-lines 3)
(defvar magit-branch-section-map
(let ((map (make-sparse-keymap)))
(define-key map "\r" 'magit-show-commit)
(define-key map "k" 'magit-branch-delete)
(define-key map "R" 'magit-branch-rename)
map)
"Keymap for `branch' sections.")
(defvar magit-remote-section-map
(let ((map (make-sparse-keymap)))
(define-key map "k" 'magit-remote-remove)
(define-key map "R" 'magit-remote-rename)
map)
"Keymap for `remote' sections.")
(defun magit-insert-local-branches ()
(magit-insert-section (local nil)
(magit-insert-heading "Branches:")
(let ((current (magit-get-current-branch))
(branches (magit-list-local-branch-names)))
(dolist (line (magit-git-lines "branch" "-vv"
(cadr magit-refresh-args)))
(string-match magit-wash-branch-line-re line)
(magit-bind-match-strings
(branch hash message upstream ahead behind gone) line
(when (string-match-p "(" branch)
(setq branch nil))
(magit-insert-branch
branch current branches
magit-local-branch-format 'magit-branch-local
hash message upstream ahead behind gone))))
(insert ?\n)))
(defun magit-insert-remote-branches ()
(dolist (remote (magit-list-remotes))
(magit-insert-section (remote remote)
(magit-insert-heading
(let ((pull (magit-get "remote" remote "url"))
(push (magit-get "remote" remote "pushurl")))
(format "%s (%s):" (capitalize remote)
(concat pull (and pull push ", ") push))))
(let ((current (magit-get-current-branch))
(branches (magit-list-local-branch-names)))
(dolist (line (magit-git-lines "branch" "-vvr"
(cadr magit-refresh-args)))
(when (string-match magit-wash-branch-line-re line)
(magit-bind-match-strings (branch hash message) line
(when (string-match-p (format "^%s/" remote) branch)
(magit-insert-branch
branch current branches
magit-remote-branch-format 'magit-branch-remote hash message))))))
(insert ?\n))))
(defun magit-insert-branch (branch &rest args)
(if (equal branch "HEAD")
(magit-insert-section it (commit (magit-rev-parse "HEAD") t)
(apply #'magit-insert-branch-1 it nil args))
(magit-insert-section it (branch branch t)
(apply #'magit-insert-branch-1 it branch args))))
(defun magit-insert-branch-1
(section branch current branches format face
&optional hash message upstream ahead behind gone)
(let* ((head (or (car magit-refresh-args) current "HEAD"))
(count (and (string-match-p "%-?[0-9]+c" format)
(if branch (cadr (magit-rev-diff-count head branch)) 0)))
(mark (and (or (equal branch head)
(and (not branch) (equal head "HEAD")))
(if (equal branch current)
(propertize "@" 'face 'magit-head)
(propertize "#" 'face 'magit-tag)))))
(when upstream
(setq upstream (propertize upstream 'face
(if (member upstream branches)
'magit-branch-local
'magit-branch-remote))))
(magit-insert-heading
(format-spec
format
`((?a . ,(or ahead ""))
(?b . ,(or behind ""))
(?c . ,(or mark
(and count (> count 0)
(propertize (number-to-string count)
'face 'magit-dimmed))
""))
(?C . ,(or mark " "))
(?h . ,(or (propertize hash 'face 'magit-hash) ""))
(?m . ,(or message ""))
(?n . ,(propertize (or branch "(detached)") 'face face))
(?u . ,(or upstream ""))
(?U . ,(if upstream
(format (propertize "[%s%s] " 'face 'magit-dimmed)
upstream
(cond
(gone
(concat ": " (propertize gone 'face 'error)))
((or ahead behind)
(concat ": "
(and ahead (format "ahead %s" ahead))
(and ahead behind ", ")
(and behind (format "behind %s" behind))))
(t "")))
"")))))
(when (or (not count) (> count 0))
(if (magit-section-hidden section)
(setf (magit-section-washer section)
`(lambda ()
(magit-insert-cherry-commits-1 ,head ,branch)
(insert (propertize "\n" 'magit-section ,section))))
(magit-insert-cherry-commits-1 head branch)
(insert ?\n)))))
(defvar magit-tag-section-map
(let ((map (make-sparse-keymap)))
(define-key map "\r" 'magit-show-commit)
(define-key map "k" 'magit-tag-delete)
map)
"Keymap for `tag' sections.")
(defun magit-insert-tags ()
(-when-let (tags (magit-git-lines "tag" "-l" "-n"))
(magit-insert-section (tags)
(magit-insert-heading "Tags:")
(dolist (tag (nreverse tags))
(string-match "^\\([^ \t]+\\)[ \t]+\\([^ \t\n].*\\)?" tag)
(let* ((message (match-string 2 tag))
(tag (match-string 1 tag))
(count (and (string-match-p "%-?[0-9]+c" magit-tags-format)
(cadr (magit-rev-diff-count
(or (car magit-refresh-args) "HEAD") tag)))))
(magit-insert-section (tag tag)
(magit-insert
(format-spec magit-tags-format
`((?n . ,(propertize tag 'face 'magit-tag))
(?c . ,(if (and count (> count 0))
(propertize (number-to-string count)
'face 'magit-dimmed)
""))
(?m . ,(or message ""))))))))
(insert ?\n))))
;;;; Files
;;;###autoload
(defun magit-find-file (rev file)
(interactive (magit-find-file-read-args "Find file"))
(switch-to-buffer (magit-find-file-noselect rev file)))
;;;###autoload
(defun magit-find-file-other-window (rev file)
(interactive (magit-find-file-read-args "Find file in other window"))
(switch-to-buffer-other-window (magit-find-file-noselect rev file)))
(defun magit-find-file-read-args (prompt)
(let ((rev (magit-read-branch-or-commit "Find file from revision")))
(list rev (magit-read-file-from-rev rev prompt))))
(defvar magit-read-file-hist nil)
(defun magit-read-file-from-rev (rev prompt &optional default)
(let ((files (magit-revision-files rev)))
(magit-completing-read
prompt files nil t nil 'magit-read-file-hist
(car (member (or default (magit-current-file)) files)))))
(defun magit-get-revision-buffer (rev file &optional create)
(funcall (if create 'get-buffer-create 'get-buffer)
(format "%s.~%s~" file (subst-char-in-string ?/ ?_ rev))))
(defun magit-get-revision-buffer-create (rev file)
(magit-get-revision-buffer rev file t))
(defun magit-find-file-noselect (rev file)
(with-current-buffer (magit-get-revision-buffer-create rev file)
(let ((inhibit-read-only t))
(erase-buffer)
(magit-git-insert "cat-file" "-p" (concat rev ":" file)))
(setq magit-buffer-revision (magit-rev-format "%H" rev)
magit-buffer-refname rev
magit-buffer-file-name (expand-file-name file (magit-get-top-dir)))
(let ((buffer-file-name magit-buffer-file-name))
(normal-mode t))
(setq buffer-read-only t)
(set-buffer-modified-p nil)
(goto-char (point-min))
(run-hooks 'magit-find-file-hook)
(current-buffer)))
(defun magit-find-file-index-noselect (file)
(let* ((bufname (concat file ".~{index}~"))
(origbuf (get-buffer bufname)))
(with-current-buffer (get-buffer-create bufname)
(when (or (not origbuf)
(y-or-n-p (format "%s already exists; revert it? " bufname)))
(let ((inhibit-read-only t)
(temp (car (split-string
(magit-git-string "checkout-index" "--temp" file)
"\t"))))
(erase-buffer)
(insert-file-contents temp nil nil nil t)
(delete-file temp)))
(setq magit-buffer-revision "{index}"
magit-buffer-refname "{index}"
magit-buffer-file-name (expand-file-name file (magit-get-top-dir)))
(let ((buffer-file-name magit-buffer-file-name))
(normal-mode t))
(setq buffer-read-only t)
(set-buffer-modified-p nil)
(goto-char (point-min))
(run-hooks 'magit-find-index-hook)
(current-buffer))))
(defun magit-update-index ()
(interactive)
(let ((file (magit-file-relative-name)))
(unless (equal magit-buffer-refname "{index}")
(user-error "%s isn't visiting the index" file))
(unless (y-or-n-p (format "Update index with contents of %s" (buffer-name)))
(user-error "Abort"))
(let ((index (make-temp-file "index"))
(buffer (current-buffer)))
(with-temp-file index
(insert-buffer-substring buffer))
(magit-call-git "update-index" "--cacheinfo"
(substring (magit-git-string "ls-files" "-s" file) 0 6)
(magit-git-string "hash-object" "-t" "blob" "-w"
(concat "--path=" file)
"--" index)
file))
(set-buffer-modified-p nil))
(--when-let (magit-mode-get-buffer
magit-status-buffer-name-format 'magit-status-mode)
(with-current-buffer it (magit-refresh)))
t)
;;;###autoload
(defun magit-dired-jump (&optional other-window)
"Visit file at point using Dired.
With a prefix argument, visit in other window. If there
is no file at point then instead visit `default-directory'."
(interactive "P")
(dired-jump other-window
(file-truename (or (magit-file-at-point) default-directory))))
;;; Manipulate
;;;; Init
;;;###autoload
(defun magit-init (directory)
"Initialize a Git repository, then show its status.
If the directory is below an existing repository, then the user
has to confirm that a new one should be created inside. If the
directory is the root of the existing repository, then the user
has to confirm that it should be reinitialized.
Non-interactively DIRECTORY is (re-)initialized unconditionally."
(interactive
(let ((directory (file-name-as-directory
(expand-file-name
(read-directory-name "Create repository in: ")))))
(-when-let (toplevel (magit-toplevel-safe directory))
(setq toplevel (expand-file-name toplevel))
(unless (y-or-n-p (if (string-equal toplevel directory)
(format "Reinitialize existing repository %s? "
directory)
(format "%s is a repository. Create another in %s? "
toplevel directory)))
(user-error "Abort")))
(list directory)))
(make-directory directory t)
;; `git init' does not understand the meaning of "~"!
(magit-call-git "init" (expand-file-name directory))
(magit-status-internal directory))
;;;; Branch
(magit-define-popup magit-branch-popup
"Popup console for branch commands."
'magit-popups
:man-page "git-branch"
:switches '((?t "Set upstream configuration" "--track"))
:actions '((?b "Checkout" magit-checkout)
(?c "Create" magit-branch)
(?B "Create & Checkout" magit-branch-and-checkout)
(?u "Set upstream" magit-branch-set-upstream)
(?r "Rename" magit-branch-rename)
(?k "Delete" magit-branch-delete))
:default-arguments '("--track")
:default-action 'magit-checkout)
;;;###autoload
(defun magit-checkout (revision)
"Checkout REVISION, updating the index and the working tree.
If REVISION is a local branch then that becomes the current
branch. If it is something else then `HEAD' becomes detached.
Checkout fails if the working tree or the staging area contain
changes.
\n(git checkout REVISION)."
(interactive (list (magit-read-other-branch-or-commit "Checkout")))
(magit-run-git "checkout" revision))
(defun magit-branch (branch start-point &optional args)
"Create BRANCH at branch or revision START-POINT.
\n(git branch [ARGS] BRANCH START-POINT)."
(interactive (magit-branch-read-args "Create branch"))
(magit-run-git "branch" args branch start-point))
;;;###autoload
(defun magit-branch-and-checkout (branch start-point &optional args)
"Create and checkout BRANCH at branch or revision START-POINT.
\n(git checkout [ARGS] -b BRANCH START-POINT)."
(interactive (magit-branch-read-args "Create and checkout branch"
(magit-stash-at-point)))
(if (string-match-p "^\\(stash\\|backup\\)@{[0-9]+}$" start-point)
(magit-run-git "stash" "branch" branch start-point)
(magit-run-git "checkout" args "-b" branch start-point)))
(defun magit-branch-read-args (prompt &optional secondary-default)
(let* ((args (magit-branch-arguments))
(start (magit-read-branch-or-commit (concat prompt " starting at")
secondary-default))
(branch
(magit-read-string
"Branch name"
(and (member start (magit-list-remote-branch-names))
(mapconcat #'identity (cdr (split-string start "/")) "/")))))
(when (and (member "--track" args)
(not (magit-branch-p start)))
(setq args (delete "--track" args)))
(list branch start args)))
;;;###autoload
(defun magit-branch-delete (branches &optional force)
"Delete one or multiple branches.
If the region marks multiple branches, then offer to delete
those, otherwise prompt for a single branch to be deleted,
defaulting to the branch at point."
(interactive
(let ((branches (magit-region-values 'branch))
(force current-prefix-arg))
(if (if (> (length branches) 1)
(magit-confirm t nil "Delete %i branches" branches)
(setq branches
(list (magit-read-branch (if current-prefix-arg
"Force delete branch"
"Delete branch")
(magit-get-previous-branch)))))
(unless force
(-when-let (unmerged (-intersection
(--map (substring it 2)
(magit-git-lines "branch" "--no-merged"))
branches))
(if (magit-confirm 'delete-unmerged-branch
"Delete unmerged branch %s"
"Delete %i unmerged branches" unmerged)
(setq force t)
(or (setq branches (-difference branches unmerged))
(user-error "Abort")))))
(user-error "Abort"))
(list branches force)))
(let ((ref (magit-ref-fullname (car branches))))
(cond
((string-match "^refs/remotes/\\([^/]+\\)" ref)
(let* ((remote (match-string 1 ref))
(offset (1+ (length remote))))
(magit-run-git-async
"push" remote (--map (concat ":" (substring it offset)) branches))))
((> (length branches) 1)
(magit-run-git "branch" (if force "-D" "-d")
(delete (magit-get-current-branch) branches)))
(t ; And now for something completely different.
(let* ((branch (car branches))
(prompt (format "Branch %s is checked out. " branch)))
(when (equal branch (magit-get-current-branch))
(pcase (if (or (equal branch "master")
(not (magit-rev-verify "master")))
(magit-read-char-case prompt nil
(?d "[d]etach HEAD & delete" 'detach)
(?a "[a]bort" 'abort))
(magit-read-char-case prompt nil
(?d "[d]etach HEAD & delete" 'detach)
(?c "[c]heckout master & delete" 'master)
(?a "[a]bort" 'abort)))
(`detach (magit-call-git "checkout" "--detach"))
(`master (magit-call-git "checkout" "master"))
(`abort (user-error "Abort")))
(setq force t))
(magit-run-git "branch" (if force "-D" "-d") branch))))))
(put 'magit-branch-delete 'interactive-only t)