@@ -47,46 +47,77 @@ func (ps PatchSeverity) IsValid() error {
47
47
return fmt .Errorf ("Invalid patch severity '%s'\n %s" , ps , guidelinesLink )
48
48
}
49
49
50
- func main () {
51
- subject , err := exec .Command ("git" , "log" , "-1" , "--pretty=format:'%s'" ).Output ()
52
- if err != nil {
53
- log .Fatal (fmt .Errorf ("Unable to get log subject %s" , err ))
54
- }
55
-
56
- if len (subject ) > 0 {
57
- if subject [0 ] == []byte ("'" )[0 ] {
58
- subject = subject [1 :]
59
- }
60
- if subject [len (subject )- 1 ] == []byte ("'" )[0 ] {
61
- subject = subject [:len (subject )- 1 ]
62
- }
63
- }
64
- parts := strings .Split (string (subject ), ":" )
50
+ func checkSubject (subject string ) error {
51
+ parts := strings .Split (subject , ":" )
65
52
if len (parts ) < 2 {
66
- log . Fatal ( fmt .Errorf ("Incorrect message format\n " + guidelinesLink ) )
53
+ return fmt .Errorf ("Incorrect message format '%s' \n %s" , subject , guidelinesLink )
67
54
}
68
55
69
56
// Commit type
70
- commitType := strings .Split (string ( parts [0 ]) , "/" )
57
+ commitType := strings .Split (parts [0 ], "/" )
71
58
switch len (commitType ) {
72
59
case 1 :
73
60
errPs := PatchSeverity (commitType [0 ]).IsValid ()
74
61
errPt := PatchType (commitType [0 ]).IsValid ()
75
62
if errPs != nil && errPt != nil {
76
- log . Fatal ( errPs )
63
+ return errPs
77
64
}
78
65
case 2 :
79
66
if err := PatchType (commitType [0 ]).IsValid (); err != nil {
80
- log . Fatal ( err )
67
+ return err
81
68
}
82
69
if err := PatchSeverity (commitType [1 ]).IsValid (); err != nil {
83
- log . Fatal ( err )
70
+ return err
84
71
}
85
72
default :
86
- log . Fatal ( fmt .Errorf ("Incorrect message format\n " + guidelinesLink ) )
73
+ return fmt .Errorf ("Incorrect message format '%s' \n %s" , subject , guidelinesLink )
87
74
}
88
75
// Commit subject
89
76
if len (parts [1 ]) < 20 || len (strings .Split (parts [1 ], " " )) < 3 {
90
- log .Fatal (fmt .Errorf ("Too short or meaningless commit subject" ))
77
+ return fmt .Errorf ("Too short or meaningless commit subject" )
78
+ }
79
+ return nil
80
+ }
81
+
82
+ func stripQuotes (input string ) string {
83
+ if len (input ) > 0 {
84
+ if input [0 ] == []byte ("'" )[0 ] {
85
+ input = input [1 :]
86
+ }
87
+ if input [len (input )- 1 ] == []byte ("'" )[0 ] {
88
+ input = input [:len (input )- 1 ]
89
+ }
90
+ }
91
+ return input
92
+ }
93
+
94
+ func main () {
95
+ out , err := exec .Command ("git" , "log" , "-1" , "--pretty=format:'%s'" ).Output ()
96
+ if err != nil {
97
+ log .Fatal (fmt .Errorf ("Unable to get log subject '%s'" , err ))
98
+ }
99
+
100
+ // Handle Merge Request where the subject of last commit has the format:
101
+ // "Merge commitA-ID into commitB-ID"
102
+ // TODO: Make this generic by taking IDs as input params
103
+ subject := stripQuotes (string (out ))
104
+ if strings .HasPrefix (subject , "Merge" ) {
105
+ log .Println ("Handling Merge Request:\n " , subject )
106
+ parts := strings .Fields (subject )
107
+ if len (parts ) != 4 {
108
+ log .Fatal (fmt .Errorf ("Unkown Merge commit format '%s'\n " , subject ))
109
+ }
110
+ out , err = exec .Command ("git" , "log" , parts [3 ]+ ".." + parts [1 ], "--pretty=format:'%s'" ).Output ()
111
+ if err != nil {
112
+ log .Fatal (fmt .Errorf ("Unable to get log subject: '%s'" , err ))
113
+ }
114
+ }
115
+
116
+ // Check subject
117
+ for _ , subject = range strings .Split (string (out ), "\n " ) {
118
+ subject = stripQuotes (subject )
119
+ if err := checkSubject (string (subject )); err != nil {
120
+ log .Fatal (err )
121
+ }
91
122
}
92
123
}
0 commit comments