-
Notifications
You must be signed in to change notification settings - Fork 34
/
egg.el
6073 lines (5452 loc) · 219 KB
/
egg.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
;;; egg -- Emacs Got Git
;;; A magit fork
;; Copyright (C) 2008 Linh Dang
;; Copyright (C) 2008 Marius Vollmer
;; Copyright (C) 2009 Tim Moore
;;
;; Egg 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.
;;
;; Egg 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary
;;; This is my fork of Marius's excellent magit. his work is at:
;;; http://zagadka.vm.bytemark.co.uk/magit
;;;
(require 'cl)
(require 'electric)
(require 'ediff)
(require 'ffap)
(defgroup egg nil
"Controlling Git from Emacs."
:prefix "egg-"
:group 'tools)
(defgroup egg-faces nil
"Colourful Faces for Egg."
:group 'egg)
(defface egg-header
'((t :weight bold :inherit variable-pitch :height 1.1))
"Face for generic headers.
Many Egg faces inherit from this one by default."
:group 'egg-faces)
(defface egg-text-base
'((((class color) (background light))
:foreground "navy" :inherit variable-pitch)
(((class color) (background dark))
:foreground "SteelBlue" :inherit variable-pitch)
(t))
"Face for description text."
:group 'egg-faces)
(defface egg-text-1
'((t :inherit egg-text-base))
"Face for description text."
:group 'egg-faces)
(defface egg-text-help
'((t :inherit egg-text-base :height 0.8))
"Face for help text."
:group 'egg-faces)
(defface egg-help-header-1
'((t :inherit egg-text-base :weight bold))
"Face for help text."
:group 'egg-faces)
(defface egg-help-header-2
'((((class color) (background light))
:foreground "Black" :inherit egg-text-1 :height 0.9)
(((class color) (background dark))
:foreground "LightSteelBlue" :inherit egg-text-1 :height 0.9)
(t :inherit egg-text-1))
"Face for help text."
:group 'egg-faces)
(defface egg-text-2
'((t :inherit egg-text-base :height 1.1))
"Face for description text."
:group 'egg-faces)
(defface egg-text-3
'((t :inherit egg-text-base :height 1.2))
"Face for description text."
:group 'egg-faces)
(defface egg-text-4
'((t :inherit egg-text-base :height 1.4))
"Face for description text."
:group 'egg-faces)
(defface egg-electrict-choice
'((((class color) (background light))
:foreground "Blue" :inherit egg-text-1 :weight bold)
(((class color) (background dark))
:foreground "Cyan" :inherit egg-text-1 :weight bold)
(t))
"Face for description text."
:group 'egg-faces)
(defface egg-section-title
'((((class color) (background light))
:foreground "DarkGoldenrod" :inherit egg-header :height 1.1)
(((class color) (background dark))
:foreground "PaleGreen" :inherit egg-header :height 1.1)
(t :weight bold))
"Face for generic header lines.
Many Egg faces inherit from this one by default."
:group 'egg-faces)
(defface egg-branch
'((((class color) (background light))
:foreground "SkyBlue" :inherit egg-header :height 1.1)
(((class color) (background dark))
:foreground "Yellow" :inherit egg-header :height 1.1)
(t :weight bold))
"Face for the current branch."
:group 'egg-faces)
(defface egg-log-buffer-mark
'((((class color) (background light))
:foreground "black" :inherit bold)
(((class color) (background dark))
:foreground "orchid1" :inherit bold)
(t :weight bold))
"Face to mark commit line in log-buffer."
:group 'egg-faces)
(defface egg-branch-mono
'((((class color) (background light))
:foreground "SkyBlue" :inherit bold)
(((class color) (background dark))
:foreground "Yellow" :inherit bold)
(t :weight bold))
"Face for a branch."
:group 'egg-faces)
(defface egg-tag-mono
'((((class color) (background light))
:foreground "GoldenRod" :inherit bold)
(((class color) (background dark))
:foreground "SkyBlue" :inherit bold)
(t :weight bold))
"Face for a tag."
:group 'egg-faces)
(defface egg-an-tag-mono
'((((class color) (background light))
:foreground "DarkGoldenRod" :inherit bold)
(((class color) (background dark))
:foreground "LightGreen" :inherit bold)
(t :weight bold))
"Face for an annotated branch."
:group 'egg-faces)
(defface egg-stash-mono
'((((class color) (background light))
:foreground "DarkGoldenRod" :inherit bold)
(((class color) (background dark))
:foreground "LightGreen" :inherit bold)
(t :weight bold))
"Face for a stash identifier."
:group 'egg-faces)
(defface egg-remote-mono
'((((class color) (background light))
:foreground "Orchid" :inherit bold)
(((class color) (background dark))
:foreground "DarkSalmon" :inherit bold)
(t :weight bold))
"Face for a remote."
:group 'egg-faces)
(defface egg-term
'((((class color) (background light))
:foreground "SkyBlue" :inherit bold)
(((class color) (background dark))
:foreground "Yellow" :inherit bold)
(t :weight bold))
"Face for an important term."
:group 'egg-faces)
(defface egg-help-key
'((t :inherit 'egg-term :height 0.9))
"Hilight Face in help text."
:group 'egg-faces)
(defface egg-warning
'((((class color) (background light))
:foreground "Red" :inherit bold)
(((class color) (background dark))
:foreground "Orange" :inherit bold)
(t :weight bold))
"Face for a warning."
:group 'egg-faces)
(defface egg-diff-file-header
'((((class color) (background light))
:foreground "SlateBlue" :inherit egg-header)
(((class color) (background dark))
:foreground "LightSlateBlue" :inherit egg-header)
(t :weight bold))
"Face for diff file headers."
:group 'egg-faces)
(defface egg-unmerged-diff-file-header
'((((class color) (background light))
:foreground "Red" :inherit egg-diff-file-header)
(((class color) (background dark))
:foreground "Orange" :inherit egg-diff-file-header)
(t :weight bold))
"Face for unmerged diff file headers."
:group 'egg-faces)
(defface egg-diff-hunk-header
'((((class color) (background light))
:background "grey85")
(((class color) (background dark))
:background "grey45"))
"Face for diff hunk headers."
:group 'egg-faces)
(defface egg-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 'egg-faces)
(defface egg-diff-none
'((((class color) (background light))
:foreground "grey50")
(((class color) (background dark))
:foreground "grey70"))
"Face for lines in a diff that are unchanged."
:group 'egg-faces)
(defface egg-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 'egg-faces)
(defface egg-diff-conflict
'((((class color) (background light))
:foreground "Blue")
(((class color) (background dark))
:foreground "Orange"))
"Face for lines in a diff that have been deleted."
:group 'egg-faces)
(defface egg-graph
'((((class color) (background light))
:foreground "grey90")
(((class color) (background dark))
:foreground "grey30"))
"Face for graph."
:group 'egg-faces)
(defface egg-blame
'((((class color) (background light))
:background: "grey85" :foreground "black")
(((class color) (background dark))
:background "grey15" :foreground "white")
(t :inherit region))
"Face for blame header."
:group 'egg-faces)
(defface egg-blame-culprit
'((((class color) (background light))
:inherit egg-text-2 :background "grey85" :foreground "grey35")
(((class color) (background dark))
:inherit egg-text-2 :background "grey15" :foreground "grey60")
(t :inherit egg-blame))
"Face for blame culprit."
:group 'egg-faces)
(defface egg-blame-subject
'((((class color) (background light))
:inherit egg-blame-culprit :foreground "black")
(((class color) (background dark))
:inherit egg-blame-culprit :foreground "white")
(t :inherit egg-blame))
"Face for blame tag line."
:group 'egg-faces)
(defface egg-log-HEAD
'((t (:inherit region)))
"Face to highlight HEAD in the log buffer."
:group 'egg-faces)
(defcustom egg-buffer-hide-sub-blocks-on-start nil
"Initially hide all sub-blocks."
:group 'egg
:type '(set (const :tag "Status Buffer" egg-status-buffer-mode)
(const :tag "Log Buffer" egg-log-buffer-mode)
(const :tag "File Log Buffer" egg-file-log-buffer-mode)
(const :tag "RefLog Buffer" egg-reflog-buffer-mode)
(const :tag "Diff Buffer" egg-diff-buffer-mode)
(const :tag "Commit Buffer" egg-commit-buffer-mode)))
(defcustom egg-buffer-hide-section-type-on-start nil
"Initially hide sections of the selected type."
:group 'egg
:type '(set (cons :tag "Status Buffer"
(const :tag "Hide Blocks of type"
egg-status-buffer-mode)
(radio (const :tag "Section" :section)
(const :tag "File" :diff)
(const :tag "Hunk" :hunk)))
(cons :tag "Commit Log Buffer"
(const :tag "Hide Blocks of type"
egg-commit-buffer-mode)
(radio (const :tag "Section" :section)
(const :tag "File" :diff)
(const :tag "Hunk" :hunk)))
(cons :tag "Diff Buffer"
(const :tag "Hide Blocks of type"
egg-diff-buffer-mode)
(radio (const :tag "File" :diff)
(const :tag "Hunk" :hunk)))))
(defcustom egg-buffer-hide-help-on-start nil
"Initially hide keybindings help."
:group 'egg
:type '(set (const :tag "Status Buffer" egg-status-buffer-mode)
(const :tag "Log Buffer" egg-log-buffer-mode)
(const :tag "File Log Buffer" egg-file-log-buffer-mode)
(const :tag "RefLog Buffer" egg-reflog-buffer-mode)
(const :tag "Diff Buffer" egg-diff-buffer-mode)
(const :tag "Commit Buffer" egg-commit-buffer-mode)))
(defcustom egg-log-HEAD-max-len 1000
"Maximum number of entries when showing the history of HEAD."
:group 'egg
:type 'integer)
(defcustom egg-log-all-max-len 10000
"Maximum number of entries when showing the history of HEAD."
:group 'egg
:type 'integer)
(defcustom egg-confirm-next-action t
"Always prompt for confirmation while guessing the next logical action ."
:group 'egg
:type 'boolean)
(defcustom egg-confirm-undo t
"Always prompt for confirmation before removing delta from workdir."
:group 'egg
:type 'boolean)
(defcustom egg-status-buffer-sections '(repo unstaged staged untracked)
"Sections to be listed in the status buffer and their order."
:group 'egg
:type '(repeat (choice (const :tag "Repository Info" repo)
(const :tag "Unstaged Changes Section" unstaged)
(const :tag "Staged Changes Section" staged)
(const :tag "Untracked/Uignored Files" untracked))))
(defcustom egg-commit-buffer-sections '(staged unstaged untracked)
"Sections to be listed in the status buffer and their order."
:group 'egg
:type '(repeat (choice (const :tag "Unstaged Changes Section" unstaged)
(const :tag "Staged Changes Section" staged)
(const :tag "Untracked/Uignored Files" untracked))))
(defcustom egg-refresh-index-in-backround nil
"Whether to refresh the index in the background when emacs is idle."
:group 'egg
:type 'boolean)
(defcustom egg-enable-tooltip nil
"Whether to refresh the index in the background when emacs is idle."
:group 'egg
:type 'boolean)
(defcustom egg-git-rebase-subdir "rebase-merge"
"Name of the rebase's workdir.
Different versions of git have different names for this subdir."
:group 'egg
:type '(choice (const ".dotest-merge")
(const "rebase-merge")
string))
(defcustom egg-show-key-help-in-buffers
'(:log :status :diff :file-log :reflog)
"Display keybinding help in egg special buffers."
:group 'egg
:type '(set (const :tag "Status Buffer" :status)
(const :tag "Log Buffer" :log)
(const :tag "File Log Buffer" :file-log)
(const :tag "RefLog Buffer" :reflog)
(const :tag "Diff Buffer" :diff)
(const :tag "Commit Buffer" :commit)))
(define-widget 'egg-quit-window-actions-set 'lazy
"Custom Type for quit-window actions."
:offset 4
:format "%v"
:type '(set :tag "Actions"
(const :tag "Kill Buffer" kill)
(const :tag "Restore Windows" restore-windows)))
(defcustom egg-quit-window-actions nil
"Actions to perform upon quitting an egg special buffer."
:group 'egg
:type '(set (cons :format "%v" (const :tag "Status Buffer" egg-status-buffer-mode)
egg-quit-window-actions-set)
(cons :format "%v" (const :tag "Log (History) Buffer" egg-log-buffer-mode)
egg-quit-window-actions-set)
(cons :format "%v" (const :tag "Commit Log Buffer" egg-commit-buffer-mode)
egg-quit-window-actions-set)
(cons :format "%v" (const :tag "RefLog Buffer" egg-reflog-buffer-mode)
egg-quit-window-actions-set)
(cons :format "%v" (const :tag "Diff Buffer" egg-diff-buffer-mode)
egg-quit-window-actions-set)
(cons :format "%v" (const :tag "File Log (History) Buffer" egg-file-log-buffer-mode)
egg-quit-window-actions-set)))
(defcustom egg-git-command "git"
"Name or full-path to the git command.
Set this to the appropriate string in the case where `git' is not the
desirable way to invoke GIT."
:group 'egg
:type 'string)
(defcustom egg-patch-command "patch"
"Name or full-path to the patch command.
Set this to the appropriate string in the case where `patch' is not the
desirable way to invoke gnu patch command."
:group 'egg
:type 'string)
(defcustom egg-dummy-option nil
"Foo bar"
:group 'egg
:type '(set (const :bold) (const :italic)))
;;;========================================================
;;; simple routines
;;;========================================================
(defmacro egg-text (text face)
"Format TEXT with face FACE at compile-time or run-time."
(cond ((stringp text)
(propertize text 'face (if (symbolp face) face
(nth 1 face))))
((null text)
`(propertize "<internal-bug>" 'face ,face))
(t `(propertize ,text 'face ,face))))
;;(cl-macroexpand '(egg-text blah 'egg-text-3))
(defmacro egg-prop (text &rest prop)
"Propertize TEXT with properties list PROP at compile-time or run-time."
(if (stringp text)
(apply 'propertize text
(mapcar (lambda (sym)
(if (consp sym)
(nth 1 sym)
sym))
prop))
`(propertize ,text ,@prop)))
(defalias 'egg-string-at-point 'ffap-string-at-point)
(defalias 'egg-find-file-at-point 'find-file-at-point)
(defsubst egg-prepend (str prefix &rest other-properties)
"Make STR appear to have prefix PREFIX.
If OTHER-PROPERTIES was non-nil, apply it to STR."
(setq prefix (concat prefix (substring str 0 1)))
(setq str (apply 'propertize str other-properties))
(put-text-property 0 1 'display prefix str)
str)
(defsubst egg-commit-contents (rev)
"Retrieve the raw-contents of the commit REV."
(with-temp-buffer
(call-process egg-git-command nil t nil "cat-file" "commit" rev)
(buffer-string)))
(defsubst egg-commit-message (rev)
"Retrieve the commit message of REV."
(with-temp-buffer
(call-process egg-git-command nil t nil "cat-file" "commit" rev)
(goto-char (point-min))
(re-search-forward "^\n")
(buffer-substring-no-properties (match-end 0) (point-max))))
(defsubst egg-cmd-to-string-1 (program args)
"Execute PROGRAM and return its output as a string.
ARGS is a list of arguments to pass to PROGRAM."
(with-temp-buffer
(if (= (apply 'call-process program nil t nil args) 0)
(buffer-substring-no-properties
(point-min) (if (> (point-max) (point-min))
(1- (point-max)) (point-max))))))
(defsubst egg-cmd-to-string (program &rest args)
"Execute PROGRAM and return its output as a string.
ARGS is a list of arguments to pass to PROGRAM."
(egg-cmd-to-string-1 program args))
(defsubst egg-git-to-string (&rest args)
"run GIT wih ARGS and return the output as a string."
(egg-cmd-to-string-1 egg-git-command args))
(defsubst egg-cmd-ok (program buffer &rest args)
"run PROGRAM with ARGS and insert output into BUFFER at point.
return the t if the exit-code was 0. if BUFFER was t then
current-buffer would be used."
(= (apply 'call-process program nil buffer nil args) 0))
(defsubst egg-git-ok (buffer &rest args)
"run GIT with ARGS and insert output into BUFFER at point.
return the t if the exit-code was 0. if BUFFER was t then
current-buffer would be used."
(= (apply 'call-process egg-git-command nil buffer nil args) 0))
(defsubst egg-git-region-ok (start end &rest args)
"run GIT with ARGS and insert output into current buffer at point.
return the t if the exit-code was 0. The text between START and END
is used as input to GIT."
(= (apply 'call-process-region start end egg-git-command t t nil args) 0))
(defsubst egg-wdir-clean () (egg-git-ok nil "diff" "--quiet"))
(defsubst egg-file-updated (file)
(egg-git-ok nil "diff" "--quiet" "--" file))
(defsubst egg-file-committed (file)
(egg-git-ok nil "diff" "--quiet" "HEAD" "--" file))
(defsubst egg-file-index-empty (file)
(egg-git-ok nil "diff" "--quiet" "--cached" "--" file))
(defsubst egg-index-empty () (egg-git-ok nil "diff" "--cached" "--quiet"))
(defsubst egg-git-to-lines (&rest args)
"run GIT with ARGS.
Return the output lines as a list of strings."
(save-match-data
(split-string (or (egg-cmd-to-string-1 egg-git-command args) "")
"[\n]+" t)))
(defun egg-git-lines-matching (re idx &rest args)
"run GIT with ARGS.
Return the output lines as a list of strings."
(with-temp-buffer
(when (= (apply 'call-process egg-git-command nil t nil args) 0)
(let (lines)
(save-match-data
(goto-char (point-min))
(while (re-search-forward re nil t)
(setq lines (cons (match-string-no-properties idx) lines)))
lines)))))
(defun egg-git-lines-matching-multi (re indices &rest args)
"run GIT with ARGS.
Return the output lines as a list of strings."
(with-temp-buffer
(when (= (apply 'call-process egg-git-command nil t nil args) 0)
(let (lines matches)
(save-match-data
(goto-char (point-min))
(while (re-search-forward re nil t)
(setq matches nil)
(dolist (idx indices)
(when (match-beginning idx)
(setq matches
(cons (cons idx (match-string-no-properties idx))
matches))))
(setq lines (cons matches lines)))
lines)))))
(defsubst egg-file-git-name (file)
"return the repo-relative name of FILE."
(car (egg-git-to-lines "ls-files" "--full-name" "--" file)))
(defsubst egg-buf-git-name (&optional buf)
"return the repo-relative name of the file visited by BUF.
if BUF was nil then use current-buffer"
(egg-file-git-name (buffer-file-name buf)))
(defsubst egg-files-git-name (files)
"return the repo-relative name for each file in the list of files FILES."
(delete-duplicates
(apply 'egg-git-to-lines "ls-files" "--full-name" "--" files)
:test 'string-equal))
(defsubst egg-unmerged-files ()
"return a list of repo-relative names for each unmerged files."
(save-match-data
(delete-duplicates
(mapcar 'car
(mapcar 'last
(mapcar
'split-string
(egg-git-to-lines "ls-files" "--full-name" "-u"))))
:test 'string-equal)))
(defsubst egg-local-branches ()
"Get a list of local branches. E.g. (\"master\", \"wip1\")."
(egg-git-to-lines "rev-parse" "--symbolic" "--branches"))
(defsubst egg-local-refs ()
"Get a list of local refs. E.g. (\"master\", \"wip1\")."
(egg-git-to-lines "rev-parse" "--symbolic" "--branches" "--tags"))
(defun egg-remote-branches (&optional raw)
"Get a list of remote branches. E.g. (\"origin/master\", \"joe/fork1\")."
(let ((lst (egg-git-to-lines "rev-parse" "--symbolic" "--remotes")))
(if raw lst
(mapcar (lambda (full-name)
(let ((tmp (save-match-data (split-string full-name "/"))))
(cons (cadr tmp) (car tmp))))
lst))))
(defsubst egg-rbranch-to-remote (rbranch)
"Return the remote name in the remote-branch RBRANCH.
E.g: `foo' in `foo/bar'"
(and (stringp rbranch)
(> (length rbranch) 0)
(directory-file-name (file-name-directory rbranch))))
(defsubst egg-rbranch-name (rbranch)
"Return the ref name in the remote-branch RBRANCH.
E.g: `bar' in `foo/bar'"
(and (stringp rbranch)
(> (length rbranch) 0)
(file-name-nondirectory rbranch)))
(defsubst egg-short-ref (full-ref)
"Return the short ref name of the full ref name FULL-REF.
like `my_tag' in `refs/tags/my_tag'."
(and (stringp full-ref)
(> (length full-ref) 0)
(file-name-nondirectory full-ref)))
(defsubst egg-file-as-string-raw (file-name)
(with-temp-buffer
(insert-file-contents-literally file-name)
(buffer-string)))
(defsubst egg-file-as-string (file-name)
"return the contents of file FILE-NAME as a string."
(with-temp-buffer
(insert-file-contents-literally file-name)
(buffer-substring-no-properties
(point-min) (if (> (point-max) (point-min))
(1- (point-max)) (point-max)))))
(defun egg-pick-file-contents (file-name regexp &rest indices)
"Pick a string out of the contents of the file FILE-NAME.
This function searches for and return the 1st match of REGEXP on the
contents of the file. If indices was not nil, then return the first
successful submatch in the order in INDICES."
(with-temp-buffer
(insert-file-contents-literally file-name)
(goto-char (point-min))
(when (re-search-forward regexp nil t)
(if (null indices)
(match-string-no-properties 0)
(dolist (idx indices)
(if (match-beginning idx)
(return (match-string-no-properties idx))))))))
(defun egg-pick-file-records (file-name start-re end-re)
"Return a list of strings from the contents of the file FILE-NAME.
START-RE is the regexp to match the beginning of a record.
END-RE is the regexp to match the end of a record."
(with-temp-buffer
(insert-file-contents-literally file-name)
(goto-char (point-min))
(let ((beg (point-min))
(end (point-max))
lst)
(save-match-data
(while (and (> end beg)
(not (eobp))
(re-search-forward start-re nil t))
(setq beg (match-beginning 0))
(when (re-search-forward end-re nil t)
(setq end (match-beginning 0))
(if (> end beg)
(setq lst (cons (buffer-substring-no-properties
beg (match-beginning 0))
lst)))
(goto-char end))))
lst)))
(defsubst egg-is-in-git ()
"is the default-directory in a git repo."
(= (call-process egg-git-command nil nil nil "rev-parse" "--git-dir") 0))
(defsubst egg-is-dir-in-git (dir)
"is DIR in a git repo."
(let ((default-directory dir)) (egg-is-in-git)))
(defsubst egg-name-rev (rev)
"get the symbolic name of REV."
(egg-git-to-string "name-rev" "--always" "--name-only" rev))
(defsubst egg-describe-rev (rev)
"get the long symbolic name of REV."
(egg-git-to-string "describe" "--always" "--tags" rev))
(defsubst egg-sha1 (rev)
"get the SHA1 of REV."
(egg-git-to-string "rev-parse" (concat rev "~0")))
(defun egg-read-git-dir ()
"call GIT to read the git directory of default-directory."
(let ((dir (egg-git-to-string "rev-parse" "--git-dir")))
(if (stringp dir)
(expand-file-name dir))))
(defsubst egg-read-dir-git-dir (dir)
"call GIT to read the git directory of DIR."
(let ((default-directory dir)) (egg-read-git-dir)))
(defvar egg-git-dir nil)
(defsubst egg-git-dir (&optional error-if-not-git)
"return the (pre-read) git-dir of default-directory"
(if (local-variable-p 'egg-git-dir)
egg-git-dir
(set (make-local-variable 'egg-git-dir)
(or (egg-read-git-dir)
(and error-if-not-git
(or (kill-local-variable 'egg-git-dir) t)
(error "Not in a git repository: %s" default-directory))))
;; first time, no status yet.
;; this directory's specific var will be updated by
;; egg-set-mode-info
(set (intern (concat "egg-" egg-git-dir "-HEAD")) " Egg")
egg-git-dir))
(defsubst egg-buf-git-dir (buffer)
"return the (pre-read) git-dir of BUFFER."
(with-current-buffer buffer
(egg-git-dir)))
(defun egg-HEAD ()
"return HEAD. Either a symbolic ref or a sha1."
(let* ((git-dir (egg-git-dir)))
(if git-dir
(egg-pick-file-contents (concat git-dir "/HEAD")
"^ref: refs/heads/\\(.+\\)\\|^\\([0-9a-f]+\\)" 1 2))))
(defun egg-all-refs ()
"Get a list of all refs."
(append (egg-git-to-lines "rev-parse" "--symbolic"
"--branches" "--tags" "--remotes")
(delq nil
(mapcar
(lambda (head)
(if (file-exists-p (concat (egg-git-dir) "/" head))
head))
'("HEAD" "ORIG_HEAD" "MERGE_HEAD" "FETCH_HEAD")))))
(defun egg-ref-type-alist ()
"Build an alist of (REF-NAME . :type) cells."
(mapcar (lambda (ref-desc)
(cons (cdr (assq 5 ref-desc))
(cond ((assq 2 ref-desc) :head)
((assq 3 ref-desc) :tag)
((assq 4 ref-desc) :remote))))
(egg-git-lines-matching-multi
"^.+ \\(refs/\\(?:\\(heads\\)\\|\\(tags\\)\\|\\(remotes\\)\\)/\\(\\([^/\n]+/\\)?[^/\n]+\\)\\)$"
;; 1: full-name
;; 2: head
;; 3: tag
;; 4: remote
;; 5: name
;; 6: remote-host
'(1 2 3 4 5 6) "show-ref")))
(defsubst egg-tooltip-func ()
(if egg-enable-tooltip 'egg-buffer-help-echo))
(defun egg-full-ref-decorated-alist (head-properties
tag-properties
atag-properties
remote-ref-properties
remote-site-properties)
"Build an alist of (ref . :type) cells.
A ref string of a head will be decorated with head-PROPERTIES. A
ref string of a tag will be decorated with TAG-PROPERTIES or
ATAG-PROPERTIES. A ref string of a remote will be formatted with
REMOTE-REF-PROPERTIES and REMOTE-SITE-PROPERTIES."
(let ((refs-desc-list
(egg-git-lines-matching-multi
"^.+ \\(refs/\\(?:\\(heads\\)\\|\\(tags\\)\\|\\(remotes\\)\\)/\\(\\([^/\n]+/\\)?[^/\n{}]+\\)\\)\\(\\^{}\\)?$"
;; 1: full-name
;; 2: head
;; 3: tag
;; 4: remote
;; 5: name
;; 6: remote-host
;; 7: is annotated tag
'(1 2 3 4 5 6 7) "show-ref" "-d"))
annotated-tags)
;; remove the annotated tags from the list
(setq refs-desc-list
(delq nil
(mapcar (lambda (desc)
(if (not (assq 7 desc))
;; not an annotated tag
desc
(setq annotated-tags
(cons (cdr (assq 1 desc))
annotated-tags))
nil))
refs-desc-list)))
;; decorate the ref alist
(mapcar (lambda (desc)
(let ((full-name (cdr (assq 1 desc)))
(name (cdr (assq 5 desc)))
(remote (cdr (assq 6 desc))))
(cond ((assq 2 desc)
;; head
(cons full-name
(apply 'propertize name
:ref (cons name :head)
head-properties)))
((assq 3 desc)
;; tag
(cons full-name
(apply 'propertize name
:ref (cons name :tag)
(if (member full-name annotated-tags)
atag-properties
tag-properties))))
((assq 4 desc)
;; remote
(cons full-name
(concat
(if (stringp remote)
(apply 'propertize remote
:ref (cons name :remote)
remote-site-properties)
;; svn has no remote name
"")
(apply 'propertize (substring name (length remote))
:ref (cons name :remote)
remote-ref-properties)))))))
refs-desc-list)))
(defun egg-complete-rev (string &optional ignored all)
"Do revision completion"
(save-match-data
(cond ((string-match "\\`:[0-3]*" string) ;; stages
(funcall (if all 'all-completions 'try-completion)
string '(":0" ":1" ":2" ":3")))
;; rev^, rev~10 etc.
((string-match "[\\^~][\\^~0-9]*\\'" string)
;; check with rev-parse
(if (egg-git-ok nil "rev-parse" string)
;; rev-parse ok
(if all
;; fixme: how to do a full expansion?
(list string)
;; match
string)))
;; normal rev name
(t (let ((matches
;; match all types of refs
(egg-git-to-lines "for-each-ref" "--format=%(refname)"
(concat "refs/*/" string "*")
(concat "refs/*/" string "*/*")))
prefix)
;; get the short name
;; with 1.6.x: for-each-ref" "--format=%(refname=short)
(setq matches
(mapcar (lambda (long)
(string-match
"\\`refs/\\(?:heads\\|tags\\|remotes\\)/\\(.+\\)\\'"
long)
(match-string-no-properties 1 long))
matches))
;; do the completion
(setq prefix
(funcall (if all 'all-completions 'try-completion)
string
(nconc (directory-files (egg-git-dir)
nil "HEAD")
matches)))
(cond (all prefix)
((stringp prefix) prefix)
((null prefix) nil)
(t string)))))))
(defsubst egg-get-symbolic-HEAD (&optional file)
;; get the symbolic name of HEAD
(setq file (or file (concat (egg-git-dir) "/HEAD")))
(egg-pick-file-contents file
"^ref: refs/heads/\\(.+\\)"
1))
(defsubst egg-get-full-symbolic-HEAD (&optional file)
;; get the symbolic full name of HEAD
(setq file (or file (concat (egg-git-dir) "/HEAD")))
(egg-pick-file-contents file
"^ref: \\(refs/heads/.+\\)"
1))
(defsubst egg-get-current-sha1 ()
(or (egg-git-to-string "rev-parse" "--verify" "-q" "HEAD")
"0000000000000000000000000000000000000000"))
(defsubst egg-set-mode-info (state)
"Set the mode-line string for buffers visiting files in the current repo.
The string is built based on the current state STATE."
(set (intern (concat "egg-" egg-git-dir "-HEAD"))
(format " Git:%s" (cond ((plist-get state :rebase-dir)
"(rebasing)")
((plist-get state :merge-heads)
"(merging)")
((plist-get state :branch)
(plist-get state :branch))
(t "(detached)")))))
(defsubst egg-get-rebase-merge-state (rebase-dir)
"Build a plist of rebase info of REBASE-DIR.
this is for rebase -m variant."
(list :rebase-dir rebase-dir
:rebase-head
(egg-name-rev (egg-file-as-string (concat rebase-dir "head-name")))
:rebase-upstream
(egg-describe-rev (egg-file-as-string (concat rebase-dir "onto_name")))
:rebase-step ;; string-to-number?
(egg-file-as-string (concat rebase-dir "msgnum"))
:rebase-num ;; string-to-number?
(egg-file-as-string (concat rebase-dir "end"))))
(defsubst egg-get-rebase-interactive-state (rebase-dir)
"Build a plist of rebase info of REBASE-DIR.
this is for rebase -i variant."
(list :rebase-dir rebase-dir
:rebase-head
(egg-name-rev (egg-file-as-string (concat rebase-dir "head-name")))
:rebase-upstream
(egg-describe-rev (egg-file-as-string (concat rebase-dir "onto")))
:rebase-num
(length
(egg-pick-file-records (concat rebase-dir "git-rebase-todo.backup")
"^[pes]" "$"))
:rebase-step
(if (file-exists-p (concat rebase-dir "done"))
(length (egg-pick-file-records (concat rebase-dir "done")
"^[pes]" "$"))
0)
:rebase-cherry
(if (file-exists-p (concat rebase-dir "done"))
(car (egg-pick-file-records
(concat rebase-dir "done")
"^[pes]" "$")))))
(defsubst egg-git-rebase-dir (&optional git-dir)
(concat (or git-dir (egg-git-dir)) "/" egg-git-rebase-subdir "/"))
(defsubst egg-rebase-author-info (rebase-dir)
"Retrieve an alist of commit environment variables of the current
cherry in REBASE-DIR."
(mapcar (lambda (lst)
;; chop the ' '
(setcar (cdr lst) (substring (cadr lst) 1 -1))
lst)
(mapcar (lambda (line)
;; name-value split
(save-match-data (split-string line "=" t)))
;; grab the GIT_xxx=yyy
(egg-pick-file-records (concat rebase-dir "author-script")
"^GIT_\\(.+\\)" "$"))))
(defsubst egg-interactive-rebase-in-progress ()
"Is an interactive rebase in progress in the current repo?"
(file-exists-p (concat (egg-git-dir) "/" egg-git-rebase-subdir
"/interactive") ))
(defvar egg-internal-current-state nil)
(defun egg-get-repo-state (&optional extras)
"Retrieve current repo's state as a plist.
The properties: