forked from noguchit/egg
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathegg-git.el
2253 lines (2010 loc) · 86.1 KB
/
egg-git.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-base.el --- Emacs Got Git - Emacs interface to Git
;; Copyright (C) 2008 Linh Dang
;; Copyright (C) 2011-12 byplayer
;;
;; Author: Bogolisk <[email protected]>
;; Created: 19 Aug 2008
;; Version: 1.0.2
;; Keywords: git, version control, release management
;;
;; Special Thanks to
;; Antoine Levitt, Bogolisk,
;; Christian Köstlin
;; Max Mikhanosha
;; Aleksandar Simic
;;
;; 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.
(require 'egg-custom)
(require 'egg-base)
(defvar egg--internal-index-file nil)
(defsubst egg--git (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-args (buffer 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 (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--git-region-args (start end 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--git-check-index ()
(if egg--internal-index-file
(setenv-internal (copy-sequence process-environment)
"GIT_INDEX_FILE" egg--internal-index-file nil)
process-environment))
(defmacro with-egg-index (&rest body)
(declare (indent 0) (debug t))
`(let ((process-environment (egg--git-check-index)))
,@body))
(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."
(with-egg-index (egg--git-args buffer args)))
(defsubst egg-git-ok-args (buffer 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."
(with-egg-index (egg--git-args buffer args)))
(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."
(with-egg-index (egg--git-region-args start end args)))
(defsubst egg-git-region-ok-args (start end 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."
(with-egg-index (egg--git-region-args start end args)))
(defsubst egg-commit-contents (rev)
"Retrieve the raw-contents of the commit REV."
(with-temp-buffer
(when (egg--git t "cat-file" "commit" rev)
(buffer-string))))
(defsubst egg-commit-message (rev)
"Retrieve the commit message of REV."
(save-match-data
(with-temp-buffer
(when (egg--git t "cat-file" "commit" rev)
(goto-char (point-min))
(re-search-forward "^\n")
(buffer-substring-no-properties (match-end 0) (point-max))))))
(defsubst egg-pick-from-commit-message (rev regex &optional index)
"Retrieve the commit message of REV."
(save-match-data
(with-temp-buffer
(when (egg--git t "cat-file" "commit" rev)
(goto-char (point-min))
(re-search-forward "^\n")
(when (re-search-forward regex nil t)
(match-string-no-properties (or index 0)))))))
(defun egg-commit-subject (rev)
"Retrieve the commit subject of REV."
(save-match-data
(with-temp-buffer
(when (egg--git t "cat-file" "commit" rev)
(goto-char (point-min))
(re-search-forward "^\n")
(buffer-substring-no-properties (match-end 0)
(if (or (re-search-forward "\n\n" nil t)
(re-search-forward "\n" nil t))
(match-beginning 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
(when (= (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-args (args)
"run GIT wih ARGS and return the output as a string."
(with-temp-buffer
(when (egg-git-ok-args t args)
(buffer-substring-no-properties
(point-min) (if (> (point-max) (point-min))
(1- (point-max))
(point-max))))))
(defsubst egg--git-to-string-args (args)
"run GIT wih ARGS and return the output as a string."
(with-temp-buffer
(when (egg--git-args t args)
(buffer-substring-no-properties
(point-min) (if (> (point-max) (point-min))
(1- (point-max))
(point-max))))))
(defsubst egg-git-to-string (&rest args)
"run GIT wih ARGS and return the output as a string."
(egg-git-to-string-args args))
(defsubst egg--git-to-string (&rest args)
"run GIT wih ARGS and return the output as a string."
(egg--git-to-string-args args))
(defun egg-git-show-file-args (buffer file rev args)
(let* ((mode (assoc-default file auto-mode-alist 'string-match))
(extras (and mode (assoc-default mode egg-git-diff-file-options-alist 'eq))))
(egg--git-args buffer (append (list "--no-pager" "show")
extras
args
(list rev "--" file)))))
(defsubst egg-git-show-file (buffer file rev &rest args)
(egg-git-show-file-args buffer file rev args))
(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 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-has-stashed-wip ()
(egg--git nil "rev-parse" "--verify" "-q" "stash@{0}"))
(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-git-to-string-args args) "") "[\n]+" t)))
(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--git-to-string-args args) "") "[\n]+" t)))
(defsubst egg-git-to-string-list (&rest args)
"run GIT with ARGS.
Return the output lines as a list of strings."
(save-match-data
(split-string (or (egg-git-to-string-args args) "") "[\n\t ]+" t)))
(defsubst egg--git-to-string-list (&rest args)
"run GIT with ARGS.
Return the output lines as a list of strings."
(save-match-data
(split-string (or (egg--git-to-string-args args) "") "[\n\t ]+" 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 (egg-git-ok-args t args)
(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-stdin (stdin re idx &rest args)
"run GIT with ARGS.
Return the output lines as a list of strings."
(with-temp-buffer
(let (lines pos)
(insert stdin)
(setq pos (point-max))
(when (egg-git-region-ok-args (point-min) (point-max) args)
(save-match-data
(goto-char pos)
(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 (egg-git-ok-args t args)
(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-git-range-length (young-commitish old-commitish)
;; add one for the base commit which was excluded by the ... notation
(1+ (length (egg-git-to-lines "rev-list" (concat old-commitish "..." young-commitish)))))
(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 (file-truename (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-dups
(apply 'egg--git-to-lines "ls-files" "--full-name" "--" files)))
(defsubst egg-unmerged-files ()
"return a list of repo-relative names for each unmerged files."
(save-match-data
(delete-dups
(mapcar 'car
(mapcar 'last
(mapcar
'split-string
(egg-git-to-lines "ls-files" "--full-name" "-u")))))))
(defun egg--get-status-code ()
(let ((lines (egg-git-to-lines "status" "--porcelain" "--untracked-files=no"))
alist code index dir status)
(dolist (line lines)
(setq code (substring line 0 2))
(setq index (aref code 0)
dir (aref code 1))
(setq status nil)
(cond ((and (= dir ? ) (memq index '(?M ?A ?R ?C)))
(add-to-list 'status :wdir-index))
((and (= dir ?M) (memq index '(?M ?A ?R ?C ? )))
(add-to-list 'status :wdir-modified))
((= dir ?D)
(if (memq index '(?M ?A ?R ?C ? ))
(add-to-list 'status :wdir-deleted)
(add-to-list 'status :unmerged)
(cond ((= index ?D) (add-to-list 'status :both-deleted))
((= index ?U) (add-to-list 'status :they-deleted)))))
((= dir ?U)
(add-to-list 'status :unmerged)
(cond ((= index ?A) (add-to-list 'status :we-added))
((= index ?D) (add-to-list 'status :we-deleted))
((= index ?U) (add-to-list 'status :both-modified))))
((= dir ?A)
(add-to-list 'status :unmerged)
(cond ((= index ?U) (add-to-list 'status :they-added))
((= index ?A) (add-to-list 'status :both-added)))))
(cond ((= index ? ) (add-to-list 'status :index-head))
((and (= index ?M) (memq dir '(?M ?D ? ))) (add-to-list 'status :index-modified))
((and (= index ?A) (memq dir '(?M ?D ? ))) (add-to-list 'status :index-added))
((and (= index ?D) (memq dir '(?M ? ))) (add-to-list 'status :index-deleted))
((and (= index ?R) (memq dir '(?M ?D ? ))) (add-to-list 'status :index-moved))
((and (= index ?C) (memq dir '(?M ?D ? ))) (add-to-list 'status :index-copied)))
(add-to-list 'alist (cons (substring line 3) status)))
alist))
(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))))
(defun egg-upstream (branch)
(and (egg--git nil "config" (concat "branch." branch ".merge"))
(let ((upstream (egg-git-to-string "name-rev" "--name-only"
(concat branch "@{upstream}"))))
(if (and (> (length upstream) 8)
(string-equal (substring upstream 0 8) "remotes/"))
(substring upstream 8)
upstream))))
(defsubst egg-is-in-git ()
"is the default-directory in a git repo."
(egg--git nil "rev-parse" "--git-dir"))
(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))
(defun egg-pretty-short-rev (rev)
(let ((rev (egg-name-rev rev))
(short-sha (and rev (substring (egg-git-to-string "rev-parse" rev) 0 8))))
(save-match-data
(when (string-match "\\`\\(remotes\\|tags\\)/" rev)
(setq rev (substring rev (match-end 0)))))
(if (> (length rev) 24)
short-sha
rev)))
(defsubst egg-git-canon-name (rev file)
(when (and rev file)
(concat rev ":" (egg-file-git-name file))))
(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-completing-read-sha1 (from prompt &optional default max-back)
(let* ((max-count (or max-back 100))
(sha1-list (egg--git-to-lines "rev-list" "--topo-order"
(format "--max-count=%d" max-count)
"--abbrev-commit"
from))
(sha1-hist (copy-sequence sha1-list)))
(egg-sha1 (completing-read prompt sha1-list nil nil default 'sha1-hist))))
(defun egg-read-git-dir ()
"call GIT to read the git directory of default-directory."
(let* ((dotgit-parent (locate-dominating-file default-directory ".git"))
(dotgit (and dotgit-parent (concat dotgit-parent "/.git")))
(dir (or (and dotgit (file-directory-p dotgit) dotgit)
(egg--git-to-string "rev-parse" "--git-dir")))
(work-tree dotgit-parent))
(when (stringp dir)
(setq dir (expand-file-name dir))
(when (stringp work-tree)
(setq work-tree (expand-file-name work-tree))
(put-text-property 0 (length dir) :work-tree work-tree dir))
dir)))
(defvar egg-git-dir nil)
(defun egg-git-dir (&optional error-if-not-git)
"return the (pre-read) git-dir of default-directory"
(if (and (local-variable-p 'egg-git-dir) 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-work-tree-dir (&optional git-dir fall-back)
(unless git-dir (setq git-dir (egg-git-dir)))
(if git-dir
(or (get-text-property 0 :work-tree git-dir)
(file-name-directory git-dir))
fall-back))
(defun egg-file-get-index-buffer ()
(let* ((git-name (egg-file-git-name (buffer-file-name)))
(default-directory (egg-work-tree-dir)))
(get-buffer (concat "*:0:" git-name "*"))))
(defun egg-index-get-file-visiting-buffer ()
(let* ((default-directory (egg-work-tree-dir))
(file-name (buffer-local-value 'egg-git-name (current-buffer))))
(find-buffer-visiting file-name)))
(defsubst egg-repo-name (&optional git-dir)
(let* ((dir (or git-dir (egg-git-dir)))
(work-tree-dir (egg-work-tree-dir dir)))
(when (stringp work-tree-dir)
(file-name-nondirectory (directory-file-name work-tree-dir)))))
(defsubst egg-buf-git-dir (buffer)
"return the (pre-read) git-dir of BUFFER."
(with-current-buffer buffer
(egg-git-dir)))
(defun egg-commit-parents (rev)
(let ((default-directory (egg-work-tree-dir))
parents)
(with-temp-buffer
(egg--git t "--no-pager" "cat-file" "-p" rev)
(goto-char (point-min))
(while (re-search-forward (rx line-start
"parent " (group (= 40 hex-digit))
(0+ space)
line-end) nil t)
(add-to-list 'parents (match-string-no-properties 1)))
(setq parents (mapcar (lambda (long)
(substring-no-properties long 0 8))
(nreverse parents))))
parents))
(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))))
(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"))
(defun egg-get-all-refs (prefix &optional exact)
(if exact
(egg-git-to-lines "for-each-ref" "--format=%(refname:short)"
(format "refs/heads/%s" prefix)
(format "refs/tags/%s" prefix)
(format "refs/remotes/%s" prefix))
(egg-git-to-lines "for-each-ref" "--format=%(refname:short)"
(format "refs/heads/%s*" prefix)
(format "refs/tags/%s*" prefix)
(format "refs/remotes/%s*/*" prefix)
(format "refs/remotes/%s*" prefix))))
(defun egg-get-local-refs (prefix)
(egg-git-to-lines "for-each-ref" "--format=%(refname:short)"
(format "refs/heads/%s*" prefix)
(format "refs/tags/%s*" prefix)))
(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") ))
(defun egg-rebase-in-progress ()
(plist-get (egg-repo-state) :rebase-step))
(defsubst egg-get-rebase-apply-state (rebase-dir)
"Build a plist of rebase info of REBASE-DIR.
this is for rebase -m variant."
(let ((patch-files (directory-files rebase-dir nil "\\`[0-9]+\\'")))
(list :rebase-dir rebase-dir
:rebase-head (egg-pretty-short-rev
(egg-file-as-string (concat rebase-dir "head-name")))
:rebase-upstream
(egg-pretty-short-rev (egg-file-as-string (concat rebase-dir "onto")))
:rebase-step (string-to-number (car patch-files))
:rebase-num (string-to-number (car (nreverse patch-files))))))
(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-pretty-short-rev (egg-file-as-string (concat rebase-dir "head-name")))
:rebase-upstream
(egg-pretty-short-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-pretty-short-rev (egg-file-as-string (concat rebase-dir "head-name")))
:rebase-upstream
(egg-pretty-short-rev (egg-file-as-string (concat rebase-dir "onto")))
:rebase-num
(length
(egg-pick-file-records (concat rebase-dir "git-rebase-todo.backup")
"^[pesf]" "$"))
:rebase-step
(if (file-exists-p (concat rebase-dir "done"))
(length (egg-pick-file-records (concat rebase-dir "done")
"^[pesf]" "$"))
0)
:rebase-stopped
(if (file-exists-p (concat rebase-dir "stopped-sha"))
(egg-pick-file-contents (concat rebase-dir "stopped-sha") "^[0-9a-f]+$"))
:rebase-cherry
(if (file-exists-p (concat rebase-dir "done"))
(car (egg-pick-file-records
(concat rebase-dir "done")
"^[pesf]" "$")))))
(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 :squash-head)
"(squashing)")
((plist-get state :branch)
(plist-get state :branch))
(t "(detached)")))))
(defvar egg-internal-current-state nil)
(defun egg-get-repo-state (&optional extras)
"Retrieve current repo's state as a plist.
The properties:
:gitdir :head :branch :sha1 :merge-heads :rebase-dir :rebase-head
:rebase-upstream :rebase-step :rebase-num :rebase-cherry
EXTRAS contains the extra properties to retrieve: :staged :unstaged
if EXTRAS contains :error-if-not-git then error-out if not a git repo.
"
(let* ((git-dir (egg-git-dir (memq :error-if-not-git extras)))
(head-file (concat git-dir "/HEAD"))
(merge-file (concat git-dir "/MERGE_HEAD"))
(squash-file (concat git-dir "/SQUASH_MSG"))
(branch (egg-get-symbolic-HEAD head-file))
(branch-full-name (egg-get-full-symbolic-HEAD head-file))
(sha1 (egg-get-current-sha1))
(merge-heads
(mapcar 'egg-pretty-short-rev
(if (file-readable-p merge-file)
(egg-pick-file-records merge-file "^" "$"))))
(squash-head (when (file-readable-p squash-file)
(egg-pick-file-contents squash-file (rx line-start "commit "
(group (= 40 hex-digit))
line-end)
1)))
(rebase-apply (if (file-directory-p (concat git-dir "/rebase-apply"))
(concat git-dir "/rebase-apply/")))
(rebase-dir
(or rebase-apply
(if (file-directory-p (concat git-dir "/" egg-git-rebase-subdir))
(concat git-dir "/" egg-git-rebase-subdir "/"))))
(is-rebase-interactive
(and (not rebase-apply)
(file-exists-p (concat rebase-dir "interactive"))))
(rebase-state
(if rebase-apply
(egg-get-rebase-apply-state rebase-dir)
(when rebase-dir
(if is-rebase-interactive
(egg-get-rebase-interactive-state rebase-dir)
(egg-get-rebase-merge-state rebase-dir)))))
(state (nconc (list :gitdir git-dir
:head branch-full-name
:branch branch
:sha1 sha1
:squash-head (and squash-head (egg-pretty-short-rev squash-head))
:merge-heads merge-heads)
rebase-state))
files)
(dolist (req extras)
(cond ((eq req :unstaged)
(setq files (egg-git-to-lines "diff" "--name-only"))
(setq state (nconc (list :unstaged files) state))
(when (and files (stringp (car files)))
(setq state (nconc (list :unmerged (egg-unmerged-files))
state))))
((eq req :staged)
(setq state
(nconc (list :staged (egg-git-to-lines "diff" "--cached" "--name-only"))
state)))
((eq req :name)
(setq state
(nconc (list :name (or (egg-git-to-string "config" "user.name")
(capitalize (user-login-name))))
state)))
((eq req :email)
(setq state
(nconc (list :email (or (egg-git-to-string "config" "user.email")
(concat (user-login-name) "@" (system-name))))
state)))))
;; update mode-line
(egg-set-mode-info state)
state))
(defun egg-repo-state (&rest args)
"return the cached repo state or re-read it.
if ARGS contained :force then ignore the cached state."
(if (or (null egg-internal-current-state) ;; not cached
(memq :force args) ;; forced
(memq nil ;; cached copy has no extra reqs
(mapcar (lambda (req)
(memq req egg-internal-current-state))
args)))
(egg-get-repo-state args)
egg-internal-current-state))
(defsubst egg-repo-clean (&optional state)
"Whether the current repos is clean base on the current repo state.
use STATE as repo state if it was not nil. Otherwise re-read the repo state."
(unless state
(setq state (egg-repo-state :staged :unstaged)))
(and
(null (plist-get state :rebase-num))
(null (plist-get state :merge-heads))
(null (plist-get state :squash-head))
(not (if (memq :unstaged state)
(plist-get state :unstaged)
(egg-wdir-clean)))
(not (if (memq :staged state)
(plist-get state :staged)
(egg-index-empty)))))
(defsubst egg-is-merging (state)
(or (plist-get state :merge-heads)
(plist-get state :rebase-dir)
(plist-get state :squash-head)))
(defun egg-wdir-dirty () (plist-get (egg-repo-state :unstaged) :unstaged))
(defun egg-staged-changes () (plist-get (egg-repo-state :staged) :staged))
(defsubst egg-current-branch (&optional state)
"The current symbolic value of HEAD. i.e. name of a branch. if STATE
was not nil then use it as repo state instead of re-read from disc."
(plist-get (or state (egg-repo-state)) :branch))
(defsubst egg-current-sha1 (&optional state)
"The immutable sha1 of HEAD. if STATE was not nil then use it
as repo state instead of re-read from disc."
(plist-get (or state (egg-repo-state)) :sha1))
(defsubst egg-short-sha1 (&optional sha1)
(egg-git-to-string "rev-parse" "--short" (or sha1 (egg-current-sha1))))
(defsubst egg-user-name (&optional state)
"The configured user name."
(plist-get (or state (egg-repo-state :name)) :name))
(defsubst egg-user-email (&optional state)
"The configured email."
(plist-get (or state (egg-repo-state :email)) :email))
(defsubst egg-head (&optional state)
"a cons cell (branch . sha1) of HEAD. if STATE was not nil then use it
as repo state instead of re-read from disc."
(if (egg-git-dir)
(let ((state (or state (egg-repo-state))))
(cons (egg-current-sha1 state)
(egg-current-branch state)))))
(defsubst egg-branch-or-HEAD () (or (egg-get-symbolic-HEAD) "HEAD"))
(defsubst egg-config-section-raw (type &optional name)
(egg-pick-file-contents (concat (egg-git-dir) "/config")
(concat "^"
(if name
(format "\\[%s \"%s\"\\]" type name)
(format "\\[%s\\]" type))
"\n"
"\\(\\(?:\t.+\n\\)+\\)")
1))
(defsubst egg-config-section (type &optional name)
(save-match-data
(mapcar
(lambda (line)
(split-string line "[ =]+" t))
(split-string (or (egg-config-section-raw type name) "")
"[\t\n]+" t))))
(defun egg-config-get-all (called-interactively file type)
(interactive "p\nfFilename: \nsType: ")
(let (res)
(setq res
(save-match-data
(mapcar (lambda (rec)
(let ((key (car rec))
(infos (cdr rec)))
(cons (progn (string-match "\"\\(.+\\)\"" key)
(match-string-no-properties 1 key))
(mapcar (lambda (attr)
(split-string attr "[ =]+" t))
infos))))
(mapcar (lambda (str)
(split-string str "[\t\n]+" t))
(egg-pick-file-records file
(concat "^\\[" type " \"")
"^\\[\\|\\'")))))
(if called-interactively
(message "%S" res))
res))
(defsubst egg-config-get-all-branches ()
(egg-config-get-all nil (concat (egg-git-dir) "/config") "branch"))
(defsubst egg-config-get-all-remotes ()
(egg-config-get-all nil (concat (egg-git-dir) "/config") "remote"))
(defsubst egg-config-get-all-remote-names ()
(nconc (mapcar 'car (egg-config-get-all-remotes))
(egg-get-all-remotes)))
(defsubst egg-config-get (type attr &optional name)
(and (egg-git-dir)
(cadr (assoc attr (egg-config-section type name)))))
(defun egg-tracking-target (branch &optional mode)
(let ((remote (egg-config-get "branch" "remote" branch))
(rbranch-full (egg-config-get "branch" "merge" branch))
rbranch)
(when (stringp rbranch-full)
(setq rbranch (egg-rbranch-name rbranch-full))
(cond ((null mode) (concat remote "/" rbranch))
((eq :name-only mode) rbranch)
((eq :remote mode) (list rbranch-full remote))
(t (list rbranch remote))))))
(defun egg-complete-get-all-refs (prefix &optional matches)
(if matches
(try-completion prefix matches)
(egg-get-all-refs prefix)))
(defun egg-complete-get-local-refs (prefix &optional matches)
(if matches
(try-completion prefix matches)
(egg-get-local-refs prefix)))
(defun egg-get-match-files-substring (sub &optional matches)
(if matches
(try-completion sub (mapcar #'file-name-nondirectory matches))
(let ((default-directory (egg-work-tree-dir))
files name-matched-files full-match)
(setq files (egg-git-to-lines "--no-pager" "ls-files"
(concat sub "*")
(concat "*/" sub "*")))
(dolist (file files)
(if (string-equal file sub)
(setq full-match file))
(if (string-equal (file-name-nondirectory file) sub)
(add-to-list 'name-matched-files file)))
(or (and full-match (list full-match))
name-matched-files
files))))
(defun egg-do-completion (string &optional func all)
"Do ref name completion"
(let* ((matches (funcall func string))
(single (= (length matches) 1))
(perfect (and single (equal (car matches) string)))
prefix)
(if all matches
(when matches
(setq prefix (funcall func string matches)))
(cond ((null matches) nil)
(perfect t)
(single (car matches))
((stringp prefix) prefix)
((null prefix) nil)
(t string)))))
(defsubst egg-read-ref (prompt &optional default no-match-ok)
(completing-read prompt #'egg-do-completion #'egg-complete-get-all-refs (not no-match-ok) default))
(defsubst egg-read-local-ref (prompt &optional default no-match-ok)
(completing-read prompt #'egg-do-completion #'egg-complete-get-local-refs (not no-match-ok) default))
(defsubst egg-read-rev (prompt &optional default)
"Query user for a revision using PROMPT. DEFAULT is the default value."
(completing-read prompt 'egg-complete-rev nil nil default))
(defvar egg-add-remote-properties nil)
(defun egg-add-remote-properties (name remote &optional branch)
(or (and (functionp egg-add-remote-properties)
(funcall egg-add-remote-properties name remote branch))
(and (consp egg-add-remote-properties)
(let ((name name))
(dolist (func egg-add-remote-properties)
(setq name (funcall func name remote branch)))
name))
name))
(defvar egg-get-remote-properties nil)
(defun egg-get-remote-properties (remote branch)
(cond ((functionp egg-get-remote-properties)
(funcall egg-get-remote-properties remote branch))
((consp egg-get-remote-properties)
(dolist-done (func egg-get-remote-properties props)
(setq props (funcall func remote branch))))
(t nil)))
(defsubst egg-read-remote (prompt &optional default)
"Query user for a remote using PROMPT. DEFAULT is the default value."
(let ((remote (completing-read prompt (egg-config-get-all-remote-names) nil t default)))
(egg-add-remote-properties remote remote)))
(defvar egg-get-all-remotes nil)
(defun egg-get-all-remotes ()
(cond ((functionp egg-get-all-remotes)
(funcall egg-get-all-remotes))
((consp egg-get-all-remotes)
(apply #'append (mapcar #'funcall egg-get-all-remotes)))
(t nil)))
(defun egg-full-ref-decorated-alist (head-properties
tag-properties
atag-properties
remote-ref-properties
remote-site-properties
&optional head-properties-HEAD stash-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* ((ref-re
(rx line-start
(one-or-more not-newline)
(group "refs/"
(or (seq (or (group "heads")
(group "tags")
(seq (group "remotes")
"/"
(group (one-or-more (not (any ?\n blank ?^ ?/))))))
"/"
(group (one-or-more (not (any ?\n blank ?^)))))
(group "stash")))
(optional (group "^{}"))
line-end))
(from-regexp-list
(egg-git-lines-matching-multi ref-re
;; 1: full-name
;; 2: head
;; 3: tag
;; 4: remote
;; 5: remote-host
;; 6: short-name
;; 7: stash
;; 8: is annotated tag
'(1 2 3 4 5 6 7) "show-ref" "-d"))
(refs-desc-list
;; Convert to canonical form:
;; 1: full-name
;; 2: head
;; 3: tag
;; 4: remote
;; 5: name
;; 6: remote-host
;; 7: stash
;; 8: is annotated tag
(let (result)
(dolist (old-record from-regexp-list)
(let* (
(full-name (cdr (assq 1 old-record)))
(head (cdr (assq 2 old-record)))
(tag (cdr (assq 3 old-record)))
(remote (cdr (assq 4 old-record)))
(remote-host (cdr (assq 5 old-record)))
(short-name (cdr (assq 6 old-record)))
(stash (cdr (assq 7 old-record)))
(is-annotated-tag (cdr (assq 8 old-record)))
new-record
(cond-add-to-new-record-func (lambda (value nth)
(when (stringp value)
(setq new-record (cons (cons nth value) new-record)))))
)
(funcall cond-add-to-new-record-func full-name 1)
(funcall cond-add-to-new-record-func head 2)
(funcall cond-add-to-new-record-func tag 3)
(funcall cond-add-to-new-record-func remote 4)
(when (or (stringp remote-host) (stringp short-name))
(setq new-record (cons (cons 5
(concat remote-host (and (stringp remote-host) (stringp short-name) "/") short-name))
new-record)))
(funcall cond-add-to-new-record-func remote-host 6)
(funcall cond-add-to-new-record-func stash 7)
(funcall cond-add-to-new-record-func is-annotated-tag 8)
(setq result (cons new-record result))
)
)
result)
)
(symbolic-HEAD (egg-get-symbolic-HEAD))
annotated-tags refs)
;; remove the annotated tags from the list
(setq refs-desc-list
(delq nil
(mapcar (lambda (desc)
(if (not (assq 8 desc))
;; not an annotated tag
desc
(setq annotated-tags
(cons (cdr (assq 1 desc))