@@ -69,16 +69,22 @@ func runHookPreReceive(c *cli.Context) error {
69
69
return nil
70
70
}
71
71
72
+ if c .IsSet ("config" ) {
73
+ setting .CustomConf = c .String ("config" )
74
+ } else if c .GlobalIsSet ("config" ) {
75
+ setting .CustomConf = c .GlobalString ("config" )
76
+ }
77
+
72
78
if err := setup ("hooks/pre-receive.log" ); err != nil {
73
79
fail ("Hook pre-receive init failed" , fmt .Sprintf ("setup: %v" , err ))
74
80
}
75
81
76
82
// the environment setted on serv command
77
83
repoID , _ := strconv .ParseInt (os .Getenv (models .ProtectedBranchRepoID ), 10 , 64 )
78
84
isWiki := (os .Getenv (models .EnvRepoIsWiki ) == "true" )
79
- username := os .Getenv (models .EnvRepoUsername )
80
- reponame := os .Getenv (models .EnvRepoName )
81
- repoPath := models .RepoPath (username , reponame )
85
+ // username := os.Getenv(models.EnvRepoUsername)
86
+ // reponame := os.Getenv(models.EnvRepoName)
87
+ // repoPath := models.RepoPath(username, reponame)
82
88
83
89
buf := bytes .NewBuffer (nil )
84
90
scanner := bufio .NewScanner (os .Stdin )
@@ -96,31 +102,36 @@ func runHookPreReceive(c *cli.Context) error {
96
102
continue
97
103
}
98
104
99
- oldCommitID := string (fields [0 ])
105
+ // oldCommitID := string(fields[0])
100
106
newCommitID := string (fields [1 ])
101
107
refFullName := string (fields [2 ])
102
108
109
+ // FIXME: when we add feature to protected branch to deny force push, then uncomment below
110
+ /*var isForce bool
111
+ // detect force push
112
+ if git.EmptySHA != oldCommitID {
113
+ output, err := git.NewCommand("rev-list", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
114
+ if err != nil {
115
+ fail("Internal error", "Fail to detect force push: %v", err)
116
+ } else if len(output) > 0 {
117
+ isForce = true
118
+ }
119
+ }*/
120
+
103
121
branchName := strings .TrimPrefix (refFullName , git .BranchPrefix )
104
122
protectBranch , err := models .GetProtectedBranchBy (repoID , branchName )
105
123
if err != nil {
106
124
log .GitLogger .Fatal (2 , "retrieve protected branches information failed" )
107
125
}
108
126
109
127
if protectBranch != nil {
110
- fail (fmt .Sprintf ("protected branch %s can not be pushed to" , branchName ), "" )
111
- }
112
-
113
- // check and deletion
114
- if newCommitID == git .EmptySHA {
115
- fail (fmt .Sprintf ("Branch '%s' is protected from deletion" , branchName ), "" )
116
- }
117
-
118
- // Check force push
119
- output , err := git .NewCommand ("rev-list" , oldCommitID , "^" + newCommitID ).RunInDir (repoPath )
120
- if err != nil {
121
- fail ("Internal error" , "Fail to detect force push: %v" , err )
122
- } else if len (output ) > 0 {
123
- fail (fmt .Sprintf ("Branch '%s' is protected from force push" , branchName ), "" )
128
+ // check and deletion
129
+ if newCommitID == git .EmptySHA {
130
+ fail (fmt .Sprintf ("branch %s is protected from deletion" , branchName ), "" )
131
+ } else {
132
+ fail (fmt .Sprintf ("protected branch %s can not be pushed to" , branchName ), "" )
133
+ //fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
134
+ }
124
135
}
125
136
}
126
137
@@ -132,6 +143,12 @@ func runHookUpdate(c *cli.Context) error {
132
143
return nil
133
144
}
134
145
146
+ if c .IsSet ("config" ) {
147
+ setting .CustomConf = c .String ("config" )
148
+ } else if c .GlobalIsSet ("config" ) {
149
+ setting .CustomConf = c .GlobalString ("config" )
150
+ }
151
+
135
152
if err := setup ("hooks/update.log" ); err != nil {
136
153
fail ("Hook update init failed" , fmt .Sprintf ("setup: %v" , err ))
137
154
}
@@ -144,6 +161,12 @@ func runHookPostReceive(c *cli.Context) error {
144
161
return nil
145
162
}
146
163
164
+ if c .IsSet ("config" ) {
165
+ setting .CustomConf = c .String ("config" )
166
+ } else if c .GlobalIsSet ("config" ) {
167
+ setting .CustomConf = c .GlobalString ("config" )
168
+ }
169
+
147
170
if err := setup ("hooks/post-receive.log" ); err != nil {
148
171
fail ("Hook post-receive init failed" , fmt .Sprintf ("setup: %v" , err ))
149
172
}
0 commit comments