-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagm.sh
executable file
·1338 lines (1257 loc) · 41.5 KB
/
agm.sh
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
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Script to source
#
#
AGM_INSTALLED=~/bin/agm
AGM_TRIGGER=$AGM_INSTALLED/agm.d
AGM_SCRIPT=agm.sh
AGM_TOOL=$AGM_INSTALLED/$AGM_SCRIPT
AGM_MODULE_VERSION="$(cat $AGM_INSTALLED/agm.version 2>/dev/null)"
function AGM_check_agm
{
MSG=""
if [ -f $AGM_INSTALLED/.origin.dat ]
then
FORIGIN="$(cat $AGM_INSTALLED/.origin.dat)"
if [ ! -f $FORIGIN ]
then
rm -f $AGM_INSTALLED/.origin.dat
else
if [ "$(cat $AGM_TOOL | md5sum)" != "$(cat $FORIGIN| md5sum)" ]
then
MSG="A different version ($FORIGIN) of your $AGM_SCRIPT copy is found. You may need to install it with: $FORIGIN setup ; agm-update"
return
fi
fi
fi
if [ "$AGM_MD5" != "$(cat $AGM_TOOL | md5sum)" ]
then
MSG="please, reload agm with 'agm-update'. "
fi
}
function agm-version
{
printf "$AGM_MODULE_VERSION"
if [[ -f $AGM_INSTALLED/agm.version ]]
then
if [[ "$(cat $AGM_INSTALLED/agm.version)" != "$AGM_MODULE_VERSION" ]]
then
printf " \e[1;33m(please load version ($AGM_INSTALLED/agm.version) with 'agm-update'\e[0m"
fi
fi
printf "\n"
}
function agm-update
{
if [[ $AGM_MODULE_VERSION != "$(cat $AGM_INSTALLED/agm.version)" ]]
then
source $AGM_TOOL
fi
}
function agm-setup
{
if [ -f $AGM_INSTALLED/prepare-commit-msg ]
then
AGM_VER_FOUND="$(awk -F'|' '$1 ~ /version$/ { printf "%s\n",$2 }' $AGM_INSTALLED/prepare-commit-msg)"
if [ "$AGM_MODULE_VERSION" != "$AGM_VER_FOUND" ]
then
rm -f $AGM_INSTALLED/prepare-commit-msg
echo "$AGM_INSTALLED/prepare-commit-msg out of date. Need to be updated."
fi
fi
if [ ! -f $AGM_INSTALLED/prepare-commit-msg ]
then
mkdir -p $AGM_INSTALLED
cat > $AGM_INSTALLED/prepare-commit-msg << __END
#!/bin/bash
#
# AGM hook to report current AGM defect or UserStory to the commit message.
if [ "\$2" = "hook" ] && [ "\$3" = "version" ]
then
echo "# AGM hook version|$AGM_MODULE_VERSION|" > "\$1"
exit
fi
if [ -r $AGM_TOOL ]
then
export AGM_ID=""
# Loading $AGM_SCRIPT script. It loads automatically the correct AGM_ID, thanks to the name of the current branch.
source $AGM_TOOL
echo "Selected AGM: \$AGM_ID"
if [ "\$AGM_ID" != "" ]
then
if [ "\$(echo "\$AGM_ID" | grep '^Innovation')" != "" ]
then
if [ "\$(grep -ie "Innovation:" "\$1")" = "" ]
then
echo "Creating message..."
cp -p "\$1" "\$1".bak
printf "Innovation: \$AGM_Subject\n\$(cat "\$1".bak)" > "\$1"
rm -f "\$1".bak
fi
else
if [ "\$(grep -ie "\\(defect\\|userstory\\)#" "\$1")" != "" ]
then
echo "Updating message..."
sed --in-place 's/\(defect\|userstory\)\#[0-9 ]*:/'"\$AGM_ID"' :/gi' "\$1"
else
echo "Creating message..."
cp -p "\$1" "\$1".bak
printf "\$AGM_ID : \$(cat "\$1".bak)" > "\$1"
rm -f "\$1".bak
fi
fi
fi
fi
# vim: syntax=sh
__END
chmod +x $AGM_INSTALLED/prepare-commit-msg
echo "installed central $AGM_INSTALLED/prepare-commit-msg"
fi
if [ "$AGM_CUR_REPO_PATH" != "" ]
then
if [ -r "$AGM_CUR_REPO_PATH/.git/hooks/prepare-commit-msg" ]
then
if [ "$(grep "AGM hook version" "$AGM_CUR_REPO_PATH/.git/hooks/prepare-commit-msg")" != "" ]
then
rm -f $AGM_CUR_REPO_PATH/.git/hooks/prepare-commit-msg
ln -sf $AGM_INSTALLED/prepare-commit-msg $AGM_CUR_REPO_PATH/.git/hooks
echo "AGM git hook re-installed."
else
echo "Oops... there is another prepare-commit-msg hook in place. You have to edit it, and add this line in front of any code:
$AGM_INSTALLED/prepare-commit-msg \"\$1\" \"\$2\" \"\$3\" "
fi
else
ln -sf $AGM_INSTALLED/prepare-commit-msg $AGM_CUR_REPO_PATH/.git/hooks
echo "AGM git hook installed."
fi
else
echo "You are not in a git repository. Nothing more to do there."
fi
}
function agm-configure
{
if [ "$(echo $PROMPT_COMMAND | grep AGM_show)" != "" ] && [ "$1" != "--force" ]
then
echo "AGM tools is already configured, as PROMPT_COMMAND is set with AGM_show call. If you need to edit it, update your bashrc or .bash_profile."
return 1
fi
typeset GIT=True
__git_ps1 2>/dev/null >/dev/null
if [ $? -ne 0 ]
then
echo "For your information: You do not have GIT prompt loaded... You will see a limited number of prompt.
On Fedora, you should install git (yum install git) and load /usr/share/git-core/contrib/completion/git-prompt.sh in your ~ /.bashrc
On Ubuntu, you should just install git (sudo apt-get install git)"
typeset EXCLUDE="-and ! -path '*git*/*.prpt'"
else
typeset EXCLUDE=""
fi
echo "agm-configure is going to configure your prompt.
To simplify your prompt, the following list are provided by the script. You will be able to update it later from your .bashrc:
First, review the list and then select the right one.
Press Enter to continue.
"
read
eval "find $AGM_INSTALLED/ -name \*.prpt $EXCLUDE" | while read PRPT_FILE
do
FILE="$(echo $PRPT_FILE | sed 's|^.*/bin/agm/\(.*\)\.prpt|\1|g')"
echo "
---------------------------------------------------------------------------------------
$PRPT_FILE
---------------------------------------------------------------------------------------"
cat "$PRPT_FILE" | grep -v -i -e type:
done | more
echo "---------------------------------------------------------------------------------------"
select PRPT in $(eval "find $AGM_INSTALLED/ -name \*.prpt $EXCLUDE") exit
do
if [ "$PRPT" != "exit" ]
then
# Re-initializing AGM_PRPT options
unset AGM_PRPT_ESC_PS1_COLOR AGM_PRPT_FOR_PS1 AGM_PRPT_PS1
eval "$(cat $PRPT | grep -i -e "type: *" |grep -v -e "type: *#" | sed 's/^.*type: *//gi')"
fi
break
done
if [ "$PRPT" = "exit" ]
then
return
fi
echo "The prompt has been set on your default shell.
Do you want to install it in your .bashrc?"
while true
do
read -n 1 -p "y or N (Default)? " ANS
case $ANS in
y | Y )
ANS=Yes
break;;
n | N | "")
ANS=No
break;;
esac
printf "\nPlease type y or n.\n"
done
if [ $ANS = Yes ]
then
if [ ! -f ~/.bashrc ] || [ "$(cat ~/.bashrc | grep -e "^ *(source|\.) (~/)*\.agmrc")" = "" ]
then
echo "source ~/.agmrc" >> ~/.bashrc
fi
cat $PRPT | grep -i -e type: | sed 's/^.*type: *//gi' > ~/.agmrc
printf "\n.bashrc updated. All other sessions will need to reload the .bashrc to get the new prompt setting.\n"
else
printf "\nTo add the prompt at login, add the following lines to your login script (.profile, .bash_profile, .bashrc, ...):\n----------\n"
cat $PRPT | grep -i -e type: | sed 's/^.*type: *//gi'
echo "----------"
fi
printf "\nAGM Configuration done.\n"
}
function agm-help
{
if [ "$1" = "" ]
then
echo "
agm-help [<AgmCommand>]: This help. Recall it with AGM function name to have more details on this AGM function.
agm-create [<options>] : Register a new AGM ID.
agm [<options>] : Interactive helper to register a new AGM ID.
agm-attach <Number> : To attach an AGM ID to the current repository/directory.
agm-detach : To detach the attached AGM ID of the current repository/directory.
agm-done [<Number>] : To close the current or another opened AGM ID.
agm-list [<Number>] : To get list of active AGM ID or get detail on an AGM ID.
agm-cd <options> : To move to another tracked branched repository.
agm-update : Reload agm tool in your shell if AGM version has been updated.
cd :<options> : alias to agm-cd features.
agm-pushd <options> : like cd, but uses 'bash' pushd/popd.
pushd :<options> : alias to agm-pushd features.
agm-configure [--force]: To configure your prompt."
return
fi
case "$1" in
agm-setup)
echo "Use this function to configure your local repository with the agm hook for git."
;;
agm-configure)
typeset GIT=True
__git_ps1 2>/dev/null >/dev/null
if [ $? -ne 0 ]
then
typeset GIT=False
fi
echo "To gain on agm, I strongly suggest you to update your dynamic prompt (PROMPT_COMMAND) and add it to your ~/.bashrc.
Some prompt has been pre-defined. Using '[1magm-configure[0m', you will be able to choose the one you want and activate it.
You can contribute and share your prompt. simply create a .prpt.
The content of that file is shown as is, without prefixed 'type: ' string.
Lines prefixed by 'type: ' are going to be saved to the user's .bashrc file.
For details, see : https://lacws.emea.hpqcorp.net/tikiwiki/tiki-index.php?page=GIT+Configuration
"
;;
agm-help)
echo "$1 [<AgmCommand>]
where
- [<AgmCommand>] : Optional. Provide the agm function help."
;;
agm-detach)
echo "$1
This will simply detach your current repository/branch or simple directory to the current AGM ID."
;;
agm-attach)
echo "$1 ['All'] <Number>
where:
- ['All'] : This string inform that, all repository current branch are tracked by this AGM ID.
- <Number> : Save the AGM Number (AGM ID) to become the active one for this branched repository"
;;
agm-done)
echo "$1 [<AGM ID>]
where:
- [<AGM ID>] : Optional. Is a string representing 1 AGM ID. Regular expression is used. So, you can write $1 1375 if this string is uniq.
By default, this function will close the current AGM ID on all linked repositories/branches/directories."
;;
agm-list)
echo "$1 [<Number>]
This function provides the list of AGM ID currently active/open.
where
- <Number> : Optional. When an AGM number is provided, agm-list will provide details on this tracked AGM ID."
;;
popd)
echo "$1
This is an agm internal alias. When agm is configured, popd with refresh your AGM variable, from the new path."
;;
agm-cd|cd|pushd)
echo "agm-cd|agm-popd <'?'|'??'|'root'|'$'|'next'|'+'|'prev'|'previous'|'-'|'1'..'9'|RegEx> ['.'|RelativePath]
OR
cd|pushd [1m:[0m<'?'|'??'|'root'|'$'|'next'|'+'|'prev'|'previous'|'-'|'1'..'9'|RegEx> ['.'|RelativePath]
where
- '?' : Provides the list of repository index available for the current AGM ID.
- '??' : Provides the list of all AGM ID attached to repositories.
- 'root'|'$' : It will change to the repository root directory.
- 'next'|'+' : It will search for the next repo in the list of tracked repo and change to its root directory.
- 'prev'|'previous'|'-' : It will search for the previous repo in the list of tracked repo and change to its root directory.
- '1'..'9' : It will search the indexed repo in the list of tracked repo and change to its root directory.
- RegEx : Regular expression to find the tracked repo. Only the first found is used to change to its root directory.
- '.' : Optional. if you are staying on the same repository, Request to stay in the current directory.
- RelativePath : Optional. agm-cd will change to the subdirectories of the selected repository."
;;
agm-create)
echo "$1 [--attach] [--attach-all] <D|U|I> <Number> <Summary> [Project]
where:
--attach : To attach the new created AGM to the current repository.
--attach-all : To attach the new created AGM to ALL repositories. I do not recommend it, but it is possible to do it. :-)
<D|U|I> : This is the type of AGM ID to create. D stands for Defect, U for UserStory and I for Innovative.
An innovative artifact is not a work tracked by the AGM solution.
Use it if there is no AGM UserStory or Defect for the change you are doing. The Innovative number can be any number >=1
<Number> : This is the number of this AGM ID to create.
<Summary> : This is the short AGM description. Defect/UserStory list is kept in ~/.agm_db file. For a new defect/UserStory, this summary is required.
<Project> : For some cases, the real AGM ID is prefixed with the project ID. In that case, the string injected in git commit message will be <type> <project>-<Number>. Otherwise, it will be <type>#<Number>.
NOTE: git commit won't add any Innovative#Number to the commit message. But the Summary prefixed by 'Innovation: ' is added to the commit message.
NOTE: There is currently no links to the AGM service. So, you need to maintain the AGM service manually yourself."
;;
agm)
echo "$1 [--attach] [--attach-all]
where:
--attach : To attach the new created AGM to the current repository.
--attach-all : To attach the new created AGM to ALL repositories. I do not recommend it, but it is possible to do it. :-)
This is an interactive creation of a Agile Manager artifact (Defect or UserStory). You just need to answer to questions.
NOTE: There is currently no links to the AGM service. So, you need to maintain the AGM service manually yourself.
If you want to create an agm directly without interaction, uses agm-create. Type agm-help agm-create for more information."
;;
esac
}
function AGM_check_update
{
CUR_REPO_BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
if [ "$CUR_REPO_BRANCH" != "$AGM_CUR_REPO_BRANCH" ]
then
AGM_refresh
fi
for i in $AGM_TRIGGER/check_update/post/*.sh
do
source $i
done
}
function agm_awk_escape
{
echo $1 | sed 's|/|\/|g'
}
function AGM_refresh
{
# This function loads some important AGM variables used for prompt and some functions.
# List of Variables to set:
# - AGM_CUR_REPO_PATH : Repository PATH. This means, AGM_CUR_PATH to be empty.
# - AGM_CUR_REPO_NAME : Repository Name. basename of AGM_CUR_REPO_PATH. This means, AGM_CUR_PATH to be empty.
# - AGM_CUR_REPO_BRANCH: Repository Branch Name. This means, AGM_CUR_PATH to be empty.
# - AGM_CUR_REPO : Repository index from the list of Repository Name/branch/name.
# - AGM_ID : AGM ID
# - AGM_Subject : AGM Summary
# - AGM_CUR_PATH : AGM ID registered PATH. This means, AGM_CUR_REPO_PATH, AGM_CUR_REPO_NAME and AGM_CUR_REPO_BRANCH to be empty.
# Re-initializing published variables to re-update. AGM_ID and AGM_Subject are kept, refreshed only if ID changed.
export AGM_CUR_REPO_PATH="$(git rev-parse --show-toplevel 2>/dev/null)"
AGM_CUR_REPO_NAME=""
AGM_CUR_REPO_BRANCH=""
AGM_CUR_REPO=""
AGM_CUR_PATH=""
if [ "$AGM_CUR_REPO_PATH" != "" ]
then
export AGM_CUR_REPO_NAME="$(basename "$AGM_CUR_REPO_PATH")"
export AGM_CUR_REPO_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
fi
AGM_REPO=~/.agm.repo
if [ -f $AGM_REPO ]
then
if [ "$AGM_CUR_REPO_NAME" = "" ]
then # Search for AGM_CUR_PATH and AGM_ID from agm.repo data file
AGM_PATH_SEARCH="$(awk -F'|' '$1 ~ /^$/ && $3 ~ /^$/ { printf "%s\n",$4 }' $AGM_REPO )"
for CHECK_PATH in $AGM_PATH_SEARCH
do
if [ "$(pwd | grep "$CHECK_PATH")" != "" ]
then
break
fi
done
if [ "$(pwd | grep "$CHECK_PATH")" != "" ]
then
export AGM_CUR_PATH="$CHECK_PATH"
CHECK_PATH="$(echo "$CHECK_PATH" | sed 's|/|\\/|g')"
AGM_ID_SEARCH="$(awk -F'|' '$1 ~ /^$/ && $3 ~ /^$/ && $4 ~/^'"$CHECK_PATH"'/ { printf "%s\n",$2 }' $AGM_REPO )"
fi
unset CHECK_PATH AGM_PATH_SEARCH
else # Search for AGM_ID from agm.repo data file
AGM_CUR_REPO_BRANCH="$(echo "$AGM_CUR_REPO_BRANCH" | sed 's|/|\\/|g')"
AGM_ID_SEARCH="$(awk -F'|' '$1 ~ /^'"$AGM_CUR_REPO_NAME"'$/ && $3 ~ /^'"$(agm_awk_escape $AGM_CUR_REPO_BRANCH)"'$/ { printf "%s\n",$2 }' $AGM_REPO | head -n 1)"
if [ "$AGM_ID_SEARCH" = "" ]
then
AGM_ID_SEARCH="$(awk -F'|' '$1 ~ /^A$/ { printf "%s\n",$2 }' $AGM_REPO | head -n 1)"
fi
fi
if [ "$AGM_ID_SEARCH" != "" ]
then
if [ "$AGM_ID_SEARCH" != "$AGM_ID" ]
then
AGM_load "$AGM_ID_SEARCH"
fi
if [ "$AGM_ID" != "" ]
then
typeset -i REPO_MAX
REPO_MAX="$(awk -F'|' '$2 ~ /^'"$AGM_ID"'$/ { printf "%s\n",$1 }' $AGM_REPO | wc -l )"
if [ $REPO_MAX -gt 0 ]
then
export AGM_CUR_REPO=":$(awk -F'|' '$2 ~ /^'"$AGM_ID"'$/ { printf "%s/%s|%s\n",$4,$1,$3 }' $AGM_REPO | awk -F'|' '$1 ~ /^'"$(echo $AGM_CUR_REPO_PATH | sed 's|/|\\/|g')"'$/ && $2 ~ /'"$AGM_CUR_REPO_BRANCH"'/ { printf "%i\n",FNR }' ) "
fi
fi
else
export AGM_ID=""
export AGM_Subject=""
fi
fi
unset AGM_ID_SEARCH AGM_REPO REPO_MAX AGM_PATH_SEARCH
}
function AGM_check_repo
{
printf "$MSG"
if [ "$AGM_CUR_REPO_NAME" != "" ]
then
if [ ! -r "$AGM_CUR_REPO_PATH/.git/hooks/prepare-commit-msg.ignore" ]
then
if [ ! -r "$AGM_CUR_REPO_PATH/.git/hooks/prepare-commit-msg" ]
then
echo "GIT hook for AGM is not installed. To get power on call agm-setup to install it."
else
touch /tmp/$$-prepare-commit-msg
$AGM_CUR_REPO_PATH/.git/hooks/prepare-commit-msg "/tmp/$$-prepare-commit-msg" "hook" "version"
AGM_VER_FOUND="$(awk -F'|' '$1 ~ /version$/ { printf "%s\n",$2 }' /tmp/$$-prepare-commit-msg)"
if [ "$AGM_VER_FOUND" = "" ]
then
echo "GIT hook for AGM is not installed. To get power on call agm-setup to install it."
else
if [ "$AGM_MODULE_VERSION" != "$AGM_VER_FOUND" ]
then
echo "GIT hook for AGM is not updated. Please call agm-setup to update it."
fi
fi
fi
fi
fi
}
function AGM_set_MSG_file
{
if [ "$AGM_CUR_REPO_PATH" != "" ]
then
AGM_MSG_FILE=$AGM_CUR_REPO_PATH/.git/agm.msg
else
if [ "$AGM_CUR_PATH" != "" ]
then
AGM_MSG_FILE=$AGM_CUR_PATH/.agm.msg
else
AGM_MSG_FILE=~/.agm.msg
fi
fi
}
function AGM_show
{
# With 3 parameters provided, AGM_show will set the PS1. Usually, this is the call to use for setting PROMPT_COMMAND.
# It is composed as follow: $1=PrefixPrompt, $2=Prefix AGM prompt, $3=Suffix Prompt.
# With 2 parameters, AGM_Show will print out to the default output. AGM_show will behave like having 2 parameters if having less or more parameters to the call.
# It is composed as follow: $1=Prefix AGM prompt, $2=Suffix AGM Prompt.
AGM_check_agm
AGM_set_MSG_file
MSG="$(AGM_check_repo "$MSG")"
if [ "$MSG" != "" ]
then
INFO="call agm-msg"
else
INFO=""
fi
echo "$MSG" > "$AGM_MSG_FILE"
case $# in
3)
FORMAT="%i%a%s"
AGM_PRPT_INFO='\e[1;31m!!! %s !!!\e[0m\n'
AGM_PRPT_AGMID="$1$2"'\e[31m%s : '
AGM_PRPT_AGMSUB='%s\e[0m'"$3"
AGM_PRPT_PS1=1
;;
0)
# The new format is based on 5 AGM_PRPT env Variables.
# FORMAT defined the complete format of the prompt. %a, %s %i and %i will be replace by field format
# %a is testing AGM_ID value. The AGM_PRPT_AGMID printf format will be set. The %s will be replaced by the value.
# %I is testing INFO value. The AGM_PRPT_INFO printf format will be set. The %s will be replaced by the value.
# %s is testing AGM_Subject value. The AGM_PRPT_AGMSUB printf format will be set. The %s will be replaced by the value.
# %s is testing AGM_stats() value. The AGM_PRPT_STAT printf format will be set. The %s will be replaced by the value.
# if AGM_PRPT_PS1 = 1, the result of this data wil be set in PS1 variable. every \e[[0-9;]m string will be encapsulated by \[ and \] for PS1 support.
if [ "$AGM_PRPT_FORMAT" = "" ]
then
FORMAT="%i%a%s"
else
FORMAT="$AGM_PRPT_FORMAT"
fi
;;
*)
FORMAT="%i%a%s"
AGM_PRPT_INFO='\e[1;31m!!! %s !!!\e[0m\n'
AGM_PRPT_AGMID="$1"'\e[31m%s : '
AGM_PRPT_AGMSUB='%s\e[0m'"$2"
;;
esac
if [ "$AGM_ID" != "" ] ; then dA="$(printf "$(echo "$AGM_PRPT_AGMID" | sed 's/\\/\\\\/g')" "$AGM_ID")" ; fi
if [ "$INFO" != "" ] ; then dI="$(printf "$(echo "$AGM_PRPT_INFO" | sed 's/\\/\\\\/g')" "$INFO")" ; fi
if [ "$AGM_Subject" != "" ] ; then dS="$(printf "$(echo "$AGM_PRPT_AGMSUB"| sed 's/\\/\\\\/g')" "$AGM_Subject")"; fi
if [ "$AGM_PRPT_STAT" != "" ]
then
STAT="$(AGM_stats %s)"
unset dSt # I do not know why thjs variable is set. unset it.
if [ "$STAT" != "" ] ; then dSt="$(printf "$(echo "$AGM_PRPT_STAT" | sed 's/\\/\\\\/g')" "$STAT")" ; fi
fi
PRINT_FORMAT="$(echo "$FORMAT" |cut -d'|' -f1 | sed 's/%i/$dI/g
s/%a/$dA/g
s/%S/$dSt/g
s/%s/$dS/g')"
# Get field values to print out.
eval "PRINT_FORMAT=\"$PRINT_FORMAT\""
if [ "$AGM_PRPT_ESC_PS1_COLOR" = 1 ]
then # Propose to add \[ and \] for recognized Color ansi code. This helps the prompt management (cursor position at prompt)
PRINT_FORMAT="$(echo "$PRINT_FORMAT" | sed 's/\(\\e\[[0-9;]*m\)/\\[\1\\]/g')"
fi
if [ "$AGM_PRPT_FOR_PS1" = 1 ]
then
# AGM_PRPT_FOR_PS1 must be set to 1 if escape caracters have to be kept. This is required for PS1 setting, like those done by __git_ps1
# ex: __git_ps1 "$(AGM_show)" "%s"
# or if you write something like PS1="$(AGM_show)"
PRINT_FORMAT="$(echo "$PRINT_FORMAT" | sed 's/\\/\\\\/g')"
fi
if [ "$AGM_PRPT_PS1" = 1 ]
then
eval "PS1=\"$PRINT_FORMAT\""
else
printf "$PRINT_FORMAT"
fi
unset MSG AGM_MSG_FILE FORMAT dA dI dS dSt PRINT_FORMAT
}
function agm-msg
{
AGM_set_MSG_file
if [ -r "$AGM_MSG_FILE" ]
then
cat "$AGM_MSG_FILE"
rm -f "$AGM_MSG_FILE"
fi
unset AGM_MSG_FILE
}
function AGM_load
{ # Function to load some basic information on an AGM (ID + Subject). Regular expression on the AGM ID is possible.
AGM_DB=~/.agm_db
if [ ! -f $AGM_DB ]
then
AGM_ID=$1
AGM_Subject=""
return
fi
if [ "$1" = "" ]
then
AGM_Subject=""
else
AGM_ID_Found="$(awk -F'|' '$1 ~ /'"$1"'/ { printf "%s\n",$1 }' $AGM_DB)"
if [ "$(echo "$AGM_ID_Found" | wc -w)" -gt 1 ]
then
echo "Warning! You have selected several AGM ID . You need to refine your request to select one only.
Selected AGM ID:
$AGM_ID_Found"
export AGM_ID=""
AGM_Subject=""
unset AGM_ID_Found
else
AGM_ID="$AGM_ID_Found"
unset AGM_ID_Found
fi
AGM_Subject="$(awk -F'|' '$1 ~ /'$1'/ { printf "%s\n",$2 }' $AGM_DB)"
fi
if [ "$AGM_Subject" = "" ]
then
AGM_ID=""
fi
export AGM_ID
unset AGM_DB
}
function agm-list
{
AGM_DB=~/.agm_db
if [ "$1" = "" ]
then
awk -F'|' '{ printf("%-25s %s\n",$1,$2) }' $AGM_DB
unset AGM_DB
return
fi
echo "Getting information:"
awk -F'|' '$1 ~ /'"$1"'/ { printf ("%-25s - %s\n",$1,$2) }' $AGM_DB
printf "\nLinked to:\n"
AGM_REPO=~/.agm.repo
awk -F'|' '$2 ~ /'"$1"'/ { if ($3 != "")
{
printf ("Repo %-20s B:%-20s (%s)\n",$1,$3,$4)
}
else
{
printf ("Dir %s\n",$4)
}
}' $AGM_REPO | awk '{ printf ":%d %s\n",FNR,$0}'
unset AGM_DB AGM_REPO
}
function agm_alias_popd
{
\popd
AGM_refresh
}
function agm_alias_pushd
{
if [ "$(echo "$1" | cut -c1)" = : ]
then
agm-cd pushd "$(echo $1 | sed 's/^://g')" "$2"
else
if [ "$1" = "" ]
then
\pushd
else
\pushd "$1"
fi
AGM_refresh
fi
}
function agm_alias_cd
{
if [ "$(echo "$1" | cut -c1)" = : ]
then
agm-cd "$(echo $1 | sed 's/^://g')" "$2"
else
if [ "$1" = "" ]
then
\cd
else
\cd "$1"
fi
AGM_refresh
fi
}
function agm-cd
{
CD=cd
if [ "$1" = pushd ]
then
CD=pushd
shift
fi
AGM_refresh
typeset -i REPO_MAX
AGM_REPO=~/.agm.repo
case "$1" in
'')
if [ "$AGM_CUR_REPO_PATH" != "" ]
then
echo "Opened AGM artifacts:"
AGM_DB=~/.agm_db
awk -F'|' '{
Cmd=sprintf("awk -F\"|\" \"\\$1 ~ /^%s$/ { print(\\$2) }\" %s",$2,"'"$AGM_DB"'")
Cmd | getline TITLE
close(Cmd)
if ($1 == "" )
{
printf "%-20s %s - [1m%s[0m\n",$2,$4,TITLE
}
else
{
printf "%-20s %s (%s-%s) - [1m%s[0m\n",$2,$4,$1,$3,TITLE
}
}' $AGM_REPO | sort
REPO_MAX="$(awk -F'|' '$2 ~ /^'"$AGM_ID"'$/ { printf "%s\n",$1 }' $AGM_REPO | wc -l )"
if [ $REPO_MAX -gt 1 ]
then
printf "\nList of attached repository/directories to '$AGM_ID':\n"
awk -F'|' '$2 ~ /^'"$AGM_ID"'$/ { if ($3 != "")
{
printf "%-20s: %s/%s (%s)\n",$2,$4,$1,$3
}
else
{
printf "%-20s: %s \n",$2,$4
}
}' $AGM_REPO | awk '{ printf ":%d %s\n",FNR,$0} '
else
printf "\nNo more attached repository/directories to '$AGM_ID'.\n"
fi
return
fi
;;
'??' | '')
awk -F'|' '{ if ($1 == "" )
{
printf "%-20s %s\n",$2,$4
}
else
{
printf "%-20s %s (%s-%s)\n",$2,$4,$1,$3
}
}' $AGM_REPO
return
;;
'?')
awk -F'|' '$2 ~ /^'"$AGM_ID"'$/ { if ($3 != "")
{
printf "%-20s: %s/%s (%s)\n",$2,$4,$1,$3
}
else
{
printf "%-20s: %s \n",$2,$4
}
}' $AGM_REPO | awk '{ printf ":%d %s\n",FNR,$0} '
return
;;
root | $)
NEW_PATH="$AGM_CUR_REPO_PATH"
shift
;;
next | + )
REPO_MAX="$(awk -F'|' '$2 ~ /^'"$AGM_ID"'$/ { printf "%s\n",$1 }' $AGM_REPO | wc -l )"
if [ $REPO_MAX -gt 0 ]
then
if [ $AGM_CUR_REPO -eq $REPO_MAX ]
then
AGM_CUR_REPO=1
else
let AGM_CUR_REPO++
fi
fi
NEW_PATH="$(awk -F'|' '$2 ~ /'"$AGM_ID"'/ { printf "%s/%s\n",$4,$1 }' $AGM_REPO | awk '{ if ( FNR == '$AGM_CUR_REPO') printf "%s\n",$0} ')"
NEW_BRANCH="$(awk -F'|' '$2 ~ /'"$AGM_ID"'/ { printf "%s\n",$3 }' $AGM_REPO | awk '{ if ( FNR == '$AGM_CUR_REPO') printf "%s\n",$0} ')"
shift
;;
prev | previous | -)
REPO_MAX="$(awk -F'|' '$2 ~ /^'"$AGM_ID"'$/ { printf "%s\n",$1 }' $AGM_REPO | wc -l )"
if [ $REPO_MAX -gt 0 ]
then
if [ $AGM_CUR_REPO -eq 1 ] && [ $REPO_MAX -gt 1 ]
then
AGM_CUR_REPO=$REPO_MAX
else
let AGM_CUR_REPO--
fi
fi
NEW_PATH="$(awk -F'|' '$2 ~ /'"$AGM_ID"'/ { printf "%s/%s\n",$4,$1 }' $AGM_REPO | awk '{ if ( FNR == '$AGM_CUR_REPO') printf "%s\n",$0} ')"
NEW_BRANCH="$(awk -F'|' '$2 ~ /'"$AGM_ID"'/ { printf "%s\n",$3 }' $AGM_REPO | awk '{ if ( FNR == '$AGM_CUR_REPO') printf "%s\n",$0} ')"
shift
;;
[1-9])
REPO_MAX="$(awk -F'|' '$2 ~ /^'"$AGM_ID"'$/ { printf "%s\n",$1 }' $AGM_REPO | wc -l )"
if [ $REPO_MAX -lt $1 ]
then
echo "AGM Repo Index $1 is not defined"
unset REPO_MAX AGM_REPO
return
fi
NEW_PATH="$(awk -F'|' '$2 ~ /'"$AGM_ID"'/ { printf "%s/%s\n",$4,$1 }' $AGM_REPO | awk '{ if ( FNR == '$1') printf "%s\n",$0} ')"
NEW_BRANCH="$(awk -F'|' '$2 ~ /'"$AGM_ID"'/ { printf "%s\n",$3 }' $AGM_REPO | awk '{ if ( FNR == '$1') printf "%s\n",$0} ')"
shift
;;
*)
if [ "$(awk -F'|' '$0 ~ /'$1'/ { printf "%s/%s\n",$4,$1}' $AGM_REPO | wc -l)" -gt 1 ]
then
echo "Warning! You may need to refine your query, as several path has been found:
$(awk -F'|' '$0 ~ /'$1'/ { if ($3 != "")
{
printf "%-20s: %s/%s (%s)\n",$2,$4,$1,$3
}
else
{
printf "%-20s: %s \n",$2,$4
}
}' $AGM_REPO)
Selecting the first one."
fi
NEW_PATH="$(awk -F'|' '$0 ~ /'$1'/ { printf "%s/%s\n",$4,$1}' $AGM_REPO | head -n1)"
NEW_BRANCH="$(awk -F'|' '$0 ~ /'$1'/ { printf "%s\n",$3 }' $AGM_REPO | head -n1)"
shift
;;
esac
if [ -d "$NEW_PATH" ]
then
PWD_SAVED="$(pwd)"
if [ "$1" = "." ] && [ "$NEW_PATH" = "$AGM_CUR_REPO_PATH" ]
then
NEW_PATH="."
CUR_PATH=True
fi
if [ "$1" != "" ] && [ "$1" != "." ]
then
if [ -d "$NEW_PATH/$1" ]
then
NEW_PATH="$NEW_PATH/$1"
fi
fi
if [ "$NEW_PATH" != "." ] && [ "$PWD_SAVED" != "$NEW_PATH" ]
then
$CD "$NEW_PATH"
if [ $? -ne 0 ]
then
return $?
fi
else
NEW_PATH="$PWD_SAVED (no change)"
fi
if [ "$NEW_BRANCH" != "" ]
then
CUR_REPO_BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
if [ "$NEW_BRANCH" != "$CUR_REPO_BRANCH" ]
then
git checkout $NEW_BRANCH
if [ $? -ne 0 ]
then
echo "now: ${NEW_PATH}. But failed to activate the branch '$NEW_BRANCH'."
else
if [ "$CUR_PATH" = "" ]
then
echo "now: branch $NEW_BRANCH"
else
echo "now: $NEW_PATH, branch $NEW_BRANCH"
fi
fi
else
echo "now: $NEW_PATH"
fi
else
echo "now: $NEW_PATH"
fi
unset PWD_SAVED
else
echo "Unable to change to '$NEW_PATH'"
fi
AGM_refresh
unset REPO_MAX AGM_REPO CUR_REPO_BRANCH CUR_PATH NEW_PATH NEW_BRANCH
}
function agm-done
{
if [ "$AGM_ID" = "" ] && [ "$1" = "" ]
then
echo "Nothing to do. If you want to close another agm, just call agm-done <AGM-ID>."
return
fi
if [ "$1" != "" ]
then
AGM_load "$1"
fi
if [ "$AGM_ID" = "" ]
then
if [ "$1" != "" ]
then
echo "Unable to identify an opened AGM ID with '$1'."
agm-list
else
echo "Nothing to do. If you want to close another agm, just call agm-done <AGM-ID>."
fi
return
fi
AGM_REPO_FILE=~/.agm.repo
AGM_DB=~/.agm_db
sed --in-place=.bak '/|'"$AGM_ID"'|/d' $AGM_REPO_FILE
sed --in-place=.bak '/'"$AGM_ID"'/d' $AGM_DB
unset AGM_REPO_FILE AGM_DB
if [ "$(echo "$AGM_ID" | grep '^Innovation')" = "" ]
then
echo "$AGM_ID is closed. Remember to report it to AGM website."
else
echo "$AGM_ID is closed."
fi
export AGM_ID=""
AGM_refresh
}
function agm-detach
{
if [ "$1" = All ]
then
CUR_AGM_REPO="A"
shift
fi
if [ "$1" != "" ]
then
AGM_load "$1"
fi
AGM_REPO_FILE=~/.agm.repo
if [ "$AGM_ID" != "" ]
then
if [ "$CUR_AGM_REPO" = A ]
then
sed --in-place=.bak '/^'$AGM_CUR_REPO_NAME'|.*|'$AGM_CUR_REPO_BRANCH'/d' $AGM_REPO_FILE
echo "'$AGM_ID' detached for All repositories branches"
else
if [ "$AGM_CUR_REPO_PATH" != "" ]
then
CUR_AGM_REPO_PATH="$(dirname "$AGM_CUR_REPO_PATH" 2>/dev/null)"
sed --in-place=.bak '/^'"$AGM_CUR_REPO_NAME"'|'"$AGM_ID"'|'"$AGM_CUR_REPO_BRANCH"'|'"$(echo "$CUR_AGM_REPO_PATH" | sed 's/\//\\\//g')"'/d' $AGM_REPO_FILE
echo "'$AGM_ID' detached for '$AGM_CUR_REPO_NAME' repository branch '$AGM_CUR_REPO_BRANCH'"
else
CUR_AGM_REPO_PATH="$(pwd)"
sed --in-place=.bak '/^|'"$AGM_ID"'||'"$(echo "$CUR_AGM_REPO_PATH" | sed 's/\//\\\//g')"'/d' $AGM_REPO_FILE
echo "'$AGM_ID' detached for '$CUR_AGM_REPO_PATH' path"
fi
fi
fi
AGM_refresh
unset AGM_REPO_FILE CUR_AGM_REPO_PATH
}
function AGM_stats
{ # Report stats on current opened AGMs. No colors.
AGM_REPO_FILE=~/.agm.repo
AGM_DB=~/.agm_db
if [ "$1" = "" ]
then
FORMAT="(%s)"
else
FORMAT="$1"
fi
RESULT="$(awk -F "|" '$1 ~ /UserStory#/ { UserStory++ }
$1 ~ /^Defect#/ { Defect++ }
$1 ~ /^Innovation#/ { Innovative++ }
BEGIN { UserStory=0; Defect=0; Innovative=0; }
END {
sSep=""
if (UserStory>0)
{
sOut=sprintf("U:%d",UserStory);
sSep=" "
}
if (Defect>0)
{
sOut=sprintf("%s%sD:%d",sOut,sSep,Defect);
sSep=" "
}
if (Innovative>0)
{
sOut=sprintf("%s%sI:%d",sOut,sSep,Innovative);
}
if (sOut != "")
{
printf("%s",sOut)
}
}' $AGM_DB)"
if [ "$RESULT" != "" ]