forked from magit/magit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magit.el
4913 lines (4353 loc) · 170 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) 2010 Aaron Culich.
;; Copyright (C) 2010 Alan Falloon.
;; Copyright (C) 2008, 2010 Alex Ott.
;; Copyright (C) 2008, 2009, 2010 Alexey Voinov.
;; Copyright (C) 2010 Ben Walton.
;; Copyright (C) 2010 Chris Bernard.
;; Copyright (C) 2010 Christian Kluge.
;; Copyright (C) 2008 Daniel Farina.
;; Copyright (C) 2010 David Abrahams.
;; Copyright (C) 2009 David Wallin.
;; Copyright (C) 2009, 2010 Hannu Koivisto.
;; Copyright (C) 2009 Ian Eure.
;; Copyright (C) 2009 Jesse Alama.
;; Copyright (C) 2009 John Wiegley.
;; Copyright (C) 2010 Leo.
;; Copyright (C) 2008, 2009 Marcin Bachry.
;; Copyright (C) 2008, 2009 Marius Vollmer.
;; Copyright (C) 2010 Mark Hepburn.
;; Copyright (C) 2010 Moritz Bunkus.
;; Copyright (C) 2010 Nathan Weizenbaum.
;; Copyright (C) 2010 Oscar Fuentes.
;; Copyright (C) 2009 Pavel Holejsovsky.
;; Copyright (C) 2009, 2010 Phil Jackson.
;; Copyright (C) 2010 Philip Weaver.
;; Copyright (C) 2010 Ramkumar Ramachandra.
;; Copyright (C) 2010 Remco van 't Veer.
;; Copyright (C) 2009 René Stadler.
;; Copyright (C) 2010 Robin Green.
;; Copyright (C) 2010 Roger Crew.
;; Copyright (C) 2009, 2010, 2011 Rémi Vanicat.
;; Copyright (C) 2010 Sean Bryant.
;; Copyright (C) 2009 Steve Purcell.
;; Copyright (C) 2010 Timo Juhani Lindfors.
;; Copyright (C) 2010 Yann Hodique.
;; Copyright (C) 2010 oscar.
;; Copyright (C) 2010 Ævar Arnfjörð Bjarmason.
;; Copyright (C) 2010 Óscar Fuentes.
;; Copyright (C) 2011 Peter J Weisberg
;; Original Author: Marius Vollmer <[email protected]>
;; Lead developer: Phil Jackson <[email protected]>
;; Version: 1.0.0
;; Keywords: tools
;;
;; 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 function 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:
;; magit core
(require 'magit-key-mode)
(require 'magit-bisect)
(eval-when-compile (require 'cl))
(require 'log-edit)
(require 'easymenu)
(require 'diff-mode)
;; Silences byte-compiler warnings
(eval-when-compile (require 'view))
(declare-function view-mode 'view)
(eval-when-compile (require 'iswitchb))
(eval-when-compile (require 'ediff))
;;; Code:
(defgroup magit nil
"Controlling Git from Emacs."
:prefix "magit-"
:group 'tools)
(defcustom magit-git-executable "git"
"The name of the Git executable."
:group 'magit
:type 'string)
(defcustom magit-git-standard-options '("--no-pager")
"Standard options when running Git."
:group 'magit
:type '(repeat string))
(defcustom magit-repo-dirs 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-repo-dirs-depth 3
"The maximum depth to look for Git repos.
When looking for a Git repository below the directories in `magit-repo-dirs',
Magit will only descend this many levels deep."
:group 'magit
:type 'integer)
(defcustom magit-save-some-buffers t
"Non-nil means that \\[magit-status] will save modified buffers before running.
Setting this to t will ask which buffers to save, setting it to 'dontask will
save all modified buffers without asking."
:group 'magit
:type '(choice (const :tag "Never" nil)
(const :tag "Ask" t)
(const :tag "Save without asking" dontask)))
(defcustom magit-save-some-buffers-predicate
'magit-save-buffers-predicate-tree-only
"Specifies a predicate function on \\[magit-save-some-buffers] to determine which
unsaved buffers should be prompted for saving."
:group 'magit
:type '(radio (function-item magit-save-buffers-predicate-tree-only)
(function-item magit-save-buffers-predicate-all)
(function :tag "Other")))
(defcustom magit-commit-all-when-nothing-staged 'ask
"Determines what \\[magit-log-edit] does when nothing is staged.
Setting this to nil will make it do nothing, setting it to t will
arrange things so that the actual commit command will use the \"--all\" option,
setting it to 'ask will first ask for confirmation whether to do this,
and setting it to 'ask-stage will cause all changes to be staged,
after a confirmation."
:group 'magit
:type '(choice (const :tag "No" nil)
(const :tag "Always" t)
(const :tag "Ask" ask)
(const :tag "Ask to stage everything" ask-stage)))
(defcustom magit-commit-signoff nil
"When performing git commit adds --signoff."
:group 'magit
:type 'boolean)
(defcustom magit-log-cutoff-length 100
"The maximum number of commits to show in the log and whazzup buffers."
:group 'magit
:type 'integer)
(defcustom magit-log-infinite-length 99999
"Number of log used to show as maximum for `magit-log-cutoff-length'."
:group 'magit
:type 'integer)
(defcustom magit-process-popup-time -1
"Popup the process buffer if a command takes longer than this many seconds."
:group 'magit
:type '(choice (const :tag "Never" -1)
(const :tag "Immediately" 0)
(integer :tag "After this many seconds")))
(defcustom magit-revert-item-confirm t
"Require acknowledgment before reverting an item."
:group 'magit
:type 'boolean)
(defcustom magit-log-edit-confirm-cancellation nil
"Require acknowledgment before canceling the log edit buffer."
:group 'magit
:type 'boolean)
(defcustom magit-remote-ref-format 'branch-then-remote
"What format to use for autocompleting refs, in pariticular for remotes.
Autocompletion is used by functions like `magit-checkout',
`magit-interactive-rebase' and others which offer branch name
completion.
The value 'name-then-remote means remotes will be of the
form \"name (remote)\", while the value 'remote-slash-name
means that they'll be of the form \"remote/name\". I.e. something that's
listed as \"remotes/upstream/next\" by \"git branch -l -a\"
will be \"upstream/next\"."
:group 'magit
:type '(choice (const :tag "name (remote)" branch-then-remote)
(const :tag "remote/name" remote-slash-branch)))
(defcustom magit-process-connection-type (not (eq system-type 'cygwin))
"Connection type used for the git process.
nil mean pipe, it is usually faster and more efficient, and work on cygwin.
t mean pty, it enable magit to prompt for passphrase when needed."
:group 'magit
:type 'boolean)
(defcustom magit-completing-read-function 'magit-builtin-completing-read
"Function to be called when requesting input from the user."
:group 'magit
:type '(radio (function-item magit-iswitchb-completing-read)
(function-item magit-builtin-completing-read)
(function :tag "Other")))
(defgroup magit-faces nil
"Customize the appearance of Magit"
:prefix "magit-"
:group 'faces
:group 'magit)
(defface magit-header
'((t))
"Face for generic header lines.
Many Magit faces inherit from this one by default."
:group 'magit-faces)
(defface magit-section-title
'((t :weight bold :inherit magit-header))
"Face for section titles."
:group 'magit-faces)
(defface magit-branch
'((t :weight bold :inherit magit-header))
"Face for the current branch."
:group 'magit-faces)
(defface magit-diff-file-header
'((t :inherit magit-header))
"Face for diff file header lines."
:group 'magit-faces)
(defface magit-diff-hunk-header
'((t :slant italic :inherit magit-header))
"Face for diff hunk header lines."
:group 'magit-faces)
(defface magit-diff-add
'((((class color) (background light))
:foreground "blue1")
(((class color) (background dark))
:foreground "white"))
"Face for lines in a diff that have been added."
:group 'magit-faces)
(defface magit-diff-none
'((t))
"Face for lines in a diff that are unchanged."
:group 'magit-faces)
(defface magit-diff-del
'((((class color) (background light))
:foreground "red")
(((class color) (background dark))
:foreground "OrangeRed"))
"Face for lines in a diff that have been deleted."
:group 'magit-faces)
(defface magit-log-graph
'((((class color) (background light))
:foreground "grey11")
(((class color) (background dark))
:foreground "grey80"))
"Face for the graph element of the log output."
:group 'magit-faces)
(defface magit-log-sha1
'((((class color) (background light))
:foreground "firebrick")
(((class color) (background dark))
:foreground "tomato"))
"Face for the sha1 element of the log output."
:group 'magit-faces)
(defface magit-log-message
'((t))
"Face for the message element of the log output."
:group 'magit-faces)
(defface magit-item-highlight
'((((class color) (background light))
:background "gray95")
(((class color) (background dark))
:background "dim gray"))
"Face for highlighting the current item."
:group 'magit-faces)
(defface magit-item-mark
'((((class color) (background light))
:foreground "red")
(((class color) (background dark))
:foreground "orange"))
"Face for highlighting marked item."
:group 'magit-faces)
(defface magit-log-tag-label
'((((class color) (background light))
:background "LightGoldenRod")
(((class color) (background dark))
:background "DarkGoldenRod"))
"Face for git tag labels shown in log buffer."
:group 'magit-faces)
(defface magit-log-head-label-bisect-good
'((((class color) (background light))
:box t
:background "light green"
:foreground "dark olive green")
(((class color) (background dark))
:box t
:background "light green"
:foreground "dark olive green"))
"Face for good bisect refs"
:group 'magit-faces)
(defface magit-log-head-label-bisect-bad
'((((class color) (background light))
:box t
:background "IndianRed1"
:foreground "IndianRed4")
(((class color) (background dark))
:box t
:background "IndianRed1"
:foreground "IndianRed4"))
"Face for bad bisect refs"
:group 'magit-faces)
(defface magit-log-head-label-remote
'((((class color) (background light))
:box t
:background "Grey85"
:foreground "OliveDrab4")
(((class color) (background dark))
:box t
:background "Grey11"
:foreground "DarkSeaGreen2"))
"Face for remote branch head labels shown in log buffer."
:group 'magit-faces)
(defface magit-log-head-label-tags
'((((class color) (background light))
:box t
:background "LemonChiffon1"
:foreground "goldenrod4")
(((class color) (background dark))
:box t
:background "LemonChiffon1"
:foreground "goldenrod4"))
"Face for tag labels shown in log buffer."
:group 'magit-faces)
(defface magit-log-head-label-patches
'((((class color) (background light))
:box t
:background "IndianRed1"
:foreground "IndianRed4")
(((class color) (background dark))
:box t
:background "IndianRed1"
:foreground "IndianRed4"))
"Face for Stacked Git patches"
:group 'magit-faces)
(defvar magit-custom-options '()
"List of custom options to pass git. Do not customise this.")
(defvar magit-read-rev-history nil
"The history of inputs to `magit-read-rev'.")
(defvar magit-omit-untracked-dir-contents nil
"When non-nil magit will only list an untracked directory, not its contents.")
(defface magit-log-head-label-local
'((((class color) (background light))
:box t
:background "Grey85"
:foreground "LightSkyBlue4")
(((class color) (background dark))
:box t
:background "Grey13"
:foreground "LightSkyBlue1"))
"Face for local branch head labels shown in log buffer."
:group 'magit-faces)
(defface magit-log-head-label-default
'((((class color) (background light))
:box t
:background "Grey50")
(((class color) (background dark))
:box t
:background "Grey50"))
"Face for unknown ref labels shown in log buffer."
:group 'magit-faces)
(defface magit-menu-selected-option
'((((class color) (background light))
:foreground "red")
(((class color) (background dark))
:foreground "orange"))
"Face for selected options on magit's menu"
:group 'magit-faces)
(defvar magit-mode-map
(let ((map (make-keymap)))
(suppress-keymap map t)
(define-key map (kbd "n") 'magit-goto-next-section)
(define-key map (kbd "p") 'magit-goto-previous-section)
(define-key map (kbd "TAB") 'magit-toggle-section)
(define-key map (kbd "<backtab>") 'magit-expand-collapse-section)
(define-key map (kbd "1") 'magit-show-level-1)
(define-key map (kbd "2") 'magit-show-level-2)
(define-key map (kbd "3") 'magit-show-level-3)
(define-key map (kbd "4") 'magit-show-level-4)
(define-key map (kbd "M-1") 'magit-show-level-1-all)
(define-key map (kbd "M-2") 'magit-show-level-2-all)
(define-key map (kbd "M-3") 'magit-show-level-3-all)
(define-key map (kbd "M-4") 'magit-show-level-4-all)
(define-key map (kbd "M-h") 'magit-show-only-files)
(define-key map (kbd "M-H") 'magit-show-only-files-all)
(define-key map (kbd "M-s") 'magit-show-level-4)
(define-key map (kbd "M-S") 'magit-show-level-4-all)
(define-key map (kbd "<M-left>") 'magit-goto-parent-section)
(define-key map (kbd "g") 'magit-refresh)
(define-key map (kbd "G") 'magit-refresh-all)
(define-key map (kbd "?") 'magit-describe-item)
(define-key map (kbd "!") 'magit-key-mode-popup-running)
(define-key map (kbd ":") 'magit-git-command)
(define-key map (kbd "C-x 4 a") 'magit-add-change-log-entry-other-window)
(define-key map (kbd "RET") 'magit-visit-item)
(define-key map (kbd "SPC") 'magit-show-item-or-scroll-up)
(define-key map (kbd "DEL") 'magit-show-item-or-scroll-down)
(define-key map (kbd "C-w") 'magit-copy-item-as-kill)
(define-key map (kbd "R") 'magit-rebase-step)
(define-key map (kbd "t") 'magit-key-mode-popup-tagging)
(define-key map (kbd "r") 'magit-key-mode-popup-rewriting)
(define-key map (kbd "P") 'magit-key-mode-popup-pushing)
(define-key map (kbd "f") 'magit-key-mode-popup-fetching)
(define-key map (kbd "b") 'magit-key-mode-popup-branching)
(define-key map (kbd "B") 'magit-key-mode-popup-bisecting)
(define-key map (kbd "F") 'magit-key-mode-popup-pulling)
(define-key map (kbd "l") 'magit-key-mode-popup-logging)
(define-key map (kbd "$") 'magit-display-process)
(define-key map (kbd "c") 'magit-log-edit)
(define-key map (kbd "E") 'magit-interactive-rebase)
(define-key map (kbd "e") 'magit-ediff)
(define-key map (kbd "w") 'magit-wazzup)
(define-key map (kbd "q") 'quit-window)
map))
(defvar magit-commit-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "a") 'magit-apply-item)
(define-key map (kbd "A") 'magit-cherry-pick-item)
(define-key map (kbd "v") 'magit-revert-item)
map))
(defvar magit-status-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "-") 'magit-diff-smaller-hunks)
(define-key map (kbd "+") 'magit-diff-larger-hunks)
(define-key map (kbd "0") 'magit-diff-default-hunks)
(define-key map (kbd "s") 'magit-stage-item)
(define-key map (kbd "S") 'magit-stage-all)
(define-key map (kbd "u") 'magit-unstage-item)
(define-key map (kbd "U") 'magit-unstage-all)
(define-key map (kbd "i") 'magit-ignore-item)
(define-key map (kbd "I") 'magit-ignore-item-locally)
(define-key map (kbd ".") 'magit-mark-item)
(define-key map (kbd "=") 'magit-diff-with-mark)
(define-key map (kbd "d") 'magit-diff-working-tree)
(define-key map (kbd "D") 'magit-diff)
(define-key map (kbd "a") 'magit-apply-item)
(define-key map (kbd "A") 'magit-cherry-pick-item)
(define-key map (kbd "v") 'magit-revert-item)
(define-key map (kbd "b") 'magit-key-mode-popup-branching)
(define-key map (kbd "m") 'magit-key-mode-popup-merging)
(define-key map (kbd "M") 'magit-key-mode-popup-submodule)
(define-key map (kbd "k") 'magit-discard-item)
(define-key map (kbd "C") 'magit-add-log)
(define-key map (kbd "x") 'magit-reset-head)
(define-key map (kbd "X") 'magit-reset-working-tree)
(define-key map (kbd "z") 'magit-key-mode-popup-stashing)
map))
(defvar magit-stash-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "-") 'magit-diff-smaller-hunks)
(define-key map (kbd "+") 'magit-diff-larger-hunks)
(define-key map (kbd "0") 'magit-diff-default-hunks)
(define-key map (kbd "a") 'magit-apply-item)
(define-key map (kbd "A") 'magit-cherry-pick-item)
(define-key map (kbd "v") 'magit-revert-item)
map))
(defvar magit-log-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd ".") 'magit-mark-item)
(define-key map (kbd "=") 'magit-diff-with-mark)
(define-key map (kbd "d") 'magit-diff-working-tree)
(define-key map (kbd "D") 'magit-diff)
(define-key map (kbd "a") 'magit-apply-item)
(define-key map (kbd "A") 'magit-cherry-pick-item)
(define-key map (kbd "v") 'magit-revert-item)
(define-key map (kbd "b") 'magit-key-mode-popup-branching)
(define-key map (kbd "B") 'magit-key-mode-popup-bisecting)
(define-key map (kbd "m") 'magit-key-mode-popup-merging)
(define-key map (kbd "x") 'magit-reset-head)
(define-key map (kbd "e") 'magit-log-show-more-entries)
(define-key map (kbd "l") 'magit-key-mode-popup-logging)
(define-key map (kbd "t") 'magit-key-mode-popup-tagging)
map))
(defvar magit-reflog-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd ".") 'magit-mark-item)
(define-key map (kbd "=") 'magit-diff-with-mark)
(define-key map (kbd "d") 'magit-diff-working-tree)
(define-key map (kbd "D") 'magit-diff)
(define-key map (kbd "a") 'magit-apply-item)
(define-key map (kbd "A") 'magit-cherry-pick-item)
(define-key map (kbd "v") 'magit-revert-item)
(define-key map (kbd "x") 'magit-reset-head)
map))
(defvar magit-diff-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "-") 'magit-diff-smaller-hunks)
(define-key map (kbd "+") 'magit-diff-larger-hunks)
(define-key map (kbd "0") 'magit-diff-default-hunks)
(define-key map (kbd "a") 'magit-apply-item)
(define-key map (kbd "A") 'magit-cherry-pick-item)
(define-key map (kbd "v") 'magit-revert-item)
map))
(defvar magit-wazzup-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd ".") 'magit-mark-item)
(define-key map (kbd "=") 'magit-diff-with-mark)
(define-key map (kbd "d") 'magit-diff-working-tree)
(define-key map (kbd "D") 'magit-diff)
(define-key map (kbd "a") 'magit-apply-item)
(define-key map (kbd "A") 'magit-cherry-pick-item)
(define-key map (kbd "v") 'magit-revert-item)
(define-key map (kbd "b") 'magit-key-mode-popup-branching)
(define-key map (kbd "m") 'magit-key-mode-popup-merging)
(define-key map (kbd "x") 'magit-reset-head)
(define-key map (kbd "i") 'magit-ignore-item)
map))
(defvar magit-show-branches-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'magit-branches-window-checkout)
(define-key map (kbd "b") 'magit-branches-window-checkout)
(define-key map (kbd "k") 'magit-remove-branch)
(define-key map (kbd "K") 'magit-remove-branch-in-remote-repo)
(define-key map (kbd "$") 'magit-display-process)
(define-key map (kbd "q") 'magit-quit-branches-window)
(define-key map (kbd "g") 'magit-show-branches)
(define-key map (kbd "v") 'magit-show-branches)
(define-key map (kbd "T") 'magit-change-what-branch-tracks)
(define-key map (kbd "t") 'magit-key-mode-popup-tagging)
(define-key map (kbd "n") 'next-line)
(define-key map (kbd "p") 'previous-line)
map))
(defvar magit-bug-report-url
"http://github.com/philjackson/magit/issues")
(defun magit-bug-report (str)
(message (concat
"Unknown error: %s\nPlease, with as much "
"information as possible, file a bug at %s")
str magit-bug-report-url))
(defun magit-buffer-switch (buf)
(if (string-match "magit" (buffer-name))
(switch-to-buffer buf)
(pop-to-buffer buf)))
;;; Macros
(defmacro magit-with-refresh (&rest body)
(declare (indent 0))
`(magit-refresh-wrapper (lambda () ,@body)))
(eval-when-compile
(when (< emacs-major-version 23)
(defvar line-move-visual nil)))
;;; Compatibilities
(if (functionp 'start-file-process)
(defalias 'magit-start-process 'start-file-process)
(defalias 'magit-start-process 'start-process))
;;; Utilities
(defvar magit-submode nil)
(make-variable-buffer-local 'magit-submode)
(put 'magit-submode 'permanent-local t)
(defun magit-iswitchb-completing-read (prompt choices &optional predicate require-match
initial-input hist def)
"iswitchb-based completing-read almost-replacement."
(require 'iswitchb)
(let ((iswitchb-make-buflist-hook
(lambda ()
(setq iswitchb-temp-buflist (if (consp (first choices))
(mapcar #'car choices)
choices)))))
(iswitchb-read-buffer prompt (or initial-input def) require-match)))
(defun magit-builtin-completing-read (prompt choices &optional predicate require-match
initial-input hist def)
"Magit wrapper for standard completing-read function."
(completing-read (if (and def (> (length prompt) 2)
(string-equal ": " (substring prompt -2)))
(format "%s (default %s): " (substring prompt 0 -2) def)
prompt)
choices predicate require-match initial-input hist def))
(defun magit-completing-read (prompt choices &optional predicate require-match
initial-input hist def)
(funcall magit-completing-read-function prompt choices predicate require-match
initial-input hist def))
(defun magit-use-region-p ()
(if (fboundp 'use-region-p)
(use-region-p)
(and transient-mark-mode mark-active)))
(defun magit-goto-line (line)
"Like `goto-line' but doesn't set the mark."
(save-restriction
(widen)
(goto-char 1)
(forward-line (1- line))))
(defun magit-trim-line (str)
(if (string= str "")
nil
(if (equal (elt str (- (length str) 1)) ?\n)
(substring str 0 (- (length str) 1))
str)))
(defun magit-split-lines (str)
(if (string= str "")
nil
(let ((lines (nreverse (split-string str "\n"))))
(if (string= (car lines) "")
(setq lines (cdr lines)))
(nreverse lines))))
(defun magit-git-insert (args)
(apply #'process-file
magit-git-executable
nil (list t nil) nil
(append magit-git-standard-options args)))
(defun magit-git-output (args)
(with-output-to-string
(with-current-buffer
standard-output
(magit-git-insert args))))
(defun magit-git-string (&rest args)
(magit-trim-line (magit-git-output args)))
(defun magit-git-lines (&rest args)
(magit-split-lines (magit-git-output args)))
(defun magit-git-exit-code (&rest args)
(apply #'process-file magit-git-executable nil nil nil
(append magit-git-standard-options args)))
(defun magit-file-lines (file)
(when (file-exists-p file)
(with-temp-buffer
(insert-file-contents file)
(let ((rev (nreverse (split-string (buffer-string) "\n"))))
(nreverse (if (equal (car rev) "")
(cdr rev)
rev))))))
(defun magit-write-file-lines (file lines)
(with-temp-buffer
(dolist (l lines)
(insert l "\n"))
(write-file file)))
(defun magit-concat-with-delim (delim seqs)
(cond ((null seqs)
nil)
((null (cdr seqs))
(car seqs))
(t
(concat (car seqs) delim (magit-concat-with-delim delim (cdr seqs))))))
(defun magit-get (&rest keys)
(magit-git-string "config" (magit-concat-with-delim "." keys)))
(defun magit-get-all (&rest keys)
(magit-git-lines "config" "--get-all" (magit-concat-with-delim "." keys)))
(defun magit-set (val &rest keys)
(if val
(magit-git-string "config" (magit-concat-with-delim "." keys) val)
(magit-git-string "config" "--unset" (magit-concat-with-delim "." keys))))
(defun magit-remove-conflicts (alist)
(let ((dict (make-hash-table :test 'equal))
(result nil))
(dolist (a alist)
(puthash (car a) (cons (cdr a) (gethash (car a) dict))
dict))
(maphash (lambda (key value)
(if (= (length value) 1)
(push (cons key (car value)) result)
(let ((sub (magit-remove-conflicts
(mapcar (lambda (entry)
(let ((dir (directory-file-name
(substring entry 0 (- (length key))))))
(cons (concat (file-name-nondirectory dir) "/" key)
entry)))
value))))
(setq result (append result sub)))))
dict)
result))
(defun magit-git-repo-p (dir)
(file-exists-p (expand-file-name ".git" dir)))
(defun magit-no-commit-p ()
"return non-nil if there is no commit in the current git repository"
(not (magit-git-string
"rev-list" "HEAD" "--max-count=1")))
(defun magit-list-repos* (dir level)
(if (magit-git-repo-p dir)
(list dir)
(apply #'append
(mapcar (lambda (entry)
(unless (or (string= (substring entry -3) "/..")
(string= (substring entry -2) "/."))
(magit-list-repos* entry (+ level 1))))
(and (file-directory-p dir)
(< level magit-repo-dirs-depth)
(directory-files dir t nil t))))))
(defun magit-list-repos (dirs)
(magit-remove-conflicts
(apply #'append
(mapcar (lambda (dir)
(mapcar #'(lambda (repo)
(cons (file-name-nondirectory repo)
repo))
(magit-list-repos* dir 0)))
dirs))))
(defun magit-get-top-dir (cwd)
(let ((cwd (expand-file-name (file-truename cwd))))
(when (file-directory-p cwd)
(let* ((default-directory (file-name-as-directory cwd))
(cdup (magit-git-string "rev-parse" "--show-cdup")))
(when cdup
(file-name-as-directory (expand-file-name cdup cwd)))))))
(defun magit-get-ref (ref)
(magit-git-string "symbolic-ref" "-q" ref))
(defun magit-get-current-branch ()
(let* ((head (magit-get-ref "HEAD"))
(pos (and head (string-match "^refs/heads/" head))))
(if pos
(substring head 11)
nil)))
(defun magit-get-remote (branch)
"Return the name of the remote for BRANCH.
If branch is nil or it has no remote, but a remote named
\"origin\" exists, return that. Otherwise, return nil."
(let ((remote (or (and branch (magit-get "branch" branch "remote"))
(and (magit-get "remote" "origin" "url") "origin"))))
(if (string= remote "") nil remote)))
(defun magit-get-current-remote ()
"Return the name of the remote for the current branch.
If there is no current branch, or no remote for that branch,
but a remote named \"origin\" is configured, return that.
Otherwise, return nil."
(magit-get-remote (magit-get-current-branch)))
(defun magit-ref-exists-p (ref)
(= (magit-git-exit-code "show-ref" "--verify" ref) 0))
(defun magit-read-top-dir (rawp)
(if (and (not rawp) magit-repo-dirs)
(let* ((repos (magit-list-repos magit-repo-dirs))
(reply (magit-completing-read "Git repository: " repos)))
(file-name-as-directory
(or (cdr (assoc reply repos))
(if (file-directory-p reply)
(expand-file-name reply)
(error "Not a repository or a directory: %s" reply)))))
(file-name-as-directory
(read-directory-name "Git repository: "
(or (magit-get-top-dir default-directory)
default-directory)))))
(defun magit-rev-parse (ref)
"Return the SHA hash for REF."
(magit-git-string "rev-parse" ref))
(defun magit-ref-ambiguous-p (ref)
"Return whether or not REF is ambiguous."
;; If REF is ambiguous, rev-parse just prints errors,
;; so magit-git-string returns nil.
(not (magit-git-string "rev-parse" "--abbrev-ref" ref)))
(defun magit-name-rev (rev)
"Return a human-readable name for REV.
Unlike git name-rev, this will remove tags/ and remotes/ prefixes
if that can be done unambiguously. In addition, it will filter
out revs involving HEAD."
(when rev
(let ((name (magit-git-string "name-rev" "--no-undefined" "--name-only" rev)))
;; There doesn't seem to be a way of filtering HEAD out from name-rev,
;; so we have to do it manually.
;; HEAD-based names are too transient to allow.
(when (and (stringp name)
(string-match "^\\(.*\\<HEAD\\)\\([~^].*\\|$\\)" name))
(let ((head-ref (match-string 1 name))
(modifier (match-string 2 name)))
;; Sometimes when name-rev gives a HEAD-based name,
;; rev-parse will give an actual branch or remote name.
(setq name (concat (magit-git-string "rev-parse" "--abbrev-ref" head-ref)
modifier))
;; If rev-parse doesn't give us what we want, just use the SHA.
(when (or (null name) (string-match-p "\\<HEAD\\>" name))
(setq name (magit-rev-parse rev)))))
(setq rev (or name rev))
(when (string-match "^\\(?:tags\\|remotes\\)/\\(.*\\)" rev)
(let ((plain-name (match-string 1 rev)))
(unless (magit-ref-ambiguous-p plain-name)
(setq rev plain-name))))
rev)))
(defun magit-put-line-property (prop val)
(put-text-property (line-beginning-position) (line-beginning-position 2)
prop val))
(defun magit-format-commit (commit format)
(magit-git-string "log" "--max-count=1"
(concat "--pretty=format:" format)
commit))
(defun magit-current-line ()
(buffer-substring-no-properties (line-beginning-position)
(line-end-position)))
(defun magit-insert-region (beg end buf)
(let ((text (buffer-substring-no-properties beg end)))
(with-current-buffer buf
(insert text))))
(defun magit-insert-current-line (buf)
(let ((text (buffer-substring-no-properties
(line-beginning-position) (line-beginning-position 2))))
(with-current-buffer buf
(insert text))))
(defun magit-file-uptodate-p (file)
(eq (magit-git-exit-code "diff" "--quiet" "--" file) 0))
(defun magit-anything-staged-p ()
(not (eq (magit-git-exit-code "diff" "--quiet" "--cached") 0)))
(defun magit-everything-clean-p ()
(and (not (magit-anything-staged-p))
(eq (magit-git-exit-code "diff" "--quiet") 0)))
(defun magit-commit-parents (commit)
(cdr (split-string (magit-git-string "rev-list" "-1" "--parents" commit))))
;; XXX - let the user choose the parent
(defun magit-choose-parent-id (commit op)
(let* ((parents (magit-commit-parents commit)))
(if (> (length parents) 1)
(error "Can't %s merge commits" op)
nil)))
;;; Revisions and ranges
(defvar magit-current-range nil
"The range described by the current buffer.
This is only non-nil in diff and log buffers.
This has three possible (non-nil) forms. If it's a string REF or
a singleton list (REF), then the range is from REF to the current
working directory state (or HEAD in a log buffer). If it's a
pair (START . END), then the range is START..END.")
(make-variable-buffer-local 'magit-current-range)
(defun magit-list-interesting-refs (&optional uninteresting)
"Return interesting references as given by `git show-ref'.
Removes references matching UNINTERESTING from the
results. UNINTERESTING can be either a function taking a single
argument or a list of strings used as regexps."
(let ((refs ()))
(dolist (line (magit-git-lines "show-ref"))
(if (string-match "[^ ]+ +\\(.*\\)" line)
(let ((ref (match-string 1 line)))
(cond ((and (functionp uninteresting)
(funcall uninteresting ref)))
((and (not (functionp uninteresting))
(loop for i in uninteresting thereis (string-match i ref))))
((string-match "refs/heads/\\(.*\\)" ref)
(let ((branch (match-string 1 ref)))
(push (cons branch branch) refs)))
((string-match "refs/tags/\\(.*\\)" ref)
(push (cons (format
(if (eq magit-remote-ref-format 'branch-then-remote)
"%s (tag)" "%s")
(match-string 1 ref))
ref)
refs))
((string-match "refs/remotes/\\([^/]+\\)/\\(.+\\)" ref)
(push (cons (if (eq magit-remote-ref-format 'branch-then-remote)
(format "%s (%s)"
(match-string 2 ref)
(match-string 1 ref))
(format "%s/%s"
(match-string 1 ref)
(match-string 2 ref)))
ref)
refs))))))
(nreverse refs)))
(defvar magit-uninteresting-refs '("refs/remotes/\\([^/]+\\)/HEAD$"))
;; TODO: fix this so that def can (must?) be git rev instead of, say, "master (origin)"
;; which involves a particular display strategy and shouldn't be visible to callers
;; of magit-read-rev
(defun magit-read-rev (prompt &optional def uninteresting)
(let* ((interesting-refs (magit-list-interesting-refs
(or uninteresting magit-uninteresting-refs)))
(reply (magit-completing-read (concat prompt ": ") interesting-refs
nil nil nil 'magit-read-rev-history def))
(rev (or (cdr (assoc reply interesting-refs)) reply)))
(if (string= rev "")
nil
rev)))
(defun magit-read-rev-range (op &optional def-beg def-end)
(let ((beg (magit-read-rev (format "%s start" op)
def-beg)))
(if (not beg)
nil
(save-match-data
(if (string-match "^\\(.+\\)\\.\\.\\(.+\\)$" beg)
(cons (match-string 1 beg) (match-string 2 beg))
(let ((end (magit-read-rev (format "%s end" op) def-end)))
(cons beg end)))))))
(defun magit-rev-to-git (rev)
(or rev
(error "No revision specified"))
(if (string= rev ".")
(magit-marked-commit)
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"