Class: GitThin::Hook

Inherits:
Thin
  • Object
show all
Includes:
GitThinUtils
Defined in:
lib/git-thin/command/hook.rb

Constant Summary

Constants included from GitThinUtils

GitThinUtils::LOGA, GitThinUtils::LOGC, GitThinUtils::LOGN, GitThinUtils::LOGNone, GitThinUtils::LOGPRUNE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GitThinUtils

#logC, #logE, #logInner, #logN, #logP, #logW, #print_console, #run_shell, #set_progress

Methods inherited from Thin

run

Constructor Details

#initialize(argv) ⇒ Hook

Returns a new instance of Hook.



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
# File 'lib/git-thin/command/hook.rb', line 162

def initialize(argv)
    super
    @pwd = Dir.pwd
    if argv.arguments.length > 0 &&( argv.arguments[0] == 'install' || argv.arguments[0] == 'uninstall')
        @action = argv.shift_argument
    end
    @source_root = argv.option('source_root')
    @file_size_limit = argv.option('file_size_limit','2048')
    @commit_limit = argv.option('commit_limit','5000')
    @master_name = argv.option('master_name','develop')
    @names = argv.option('names','commands')
    @names = @names.split('|')
    @names = @names.map { |item|item.downcase }
    @support_names = ['push_count_checker','commit_lfs_checker','commands']
    if @names.include? 'all'
        @names = @support_names
    end
    check_name

    if not @source_root
        run_shell 'git rev-parse --show-toplevel',false ,LOGNone do |out,err,status|
            if status == 0
                @source_root = out[0].strip
            end
        end

        if not File.exist? @source_root+'/.git'
            UI.puts "git repository not found"
            exit 1
        end
    else
        if @source_root[-1] == '/'
            @source_root = @source_root[0..-2 ]
        end
    end
    if not Dir.exist? @source_root
        @source_root = nil
        return
    end
    @hook_path = "#{@source_root}/.git/hooks"
    if not Dir.exist? @hook_path
        FileUtils.mkdir_p @hook_path
    end
    @start_flag = '>>>>>>>>>>>>>>>>>>>'
    @end_flag   = '<<<<<<<<<<<<<<<<<<<'
end

Class Method Details

.argumentsObject



13
14
15
16
17
# File 'lib/git-thin/command/hook.rb', line 13

def self.arguments
    [
      CLAide::Argument.new('install|unstall', false )
    ]
end

.optionsObject



21
22
23
24
25
26
27
28
29
# File 'lib/git-thin/command/hook.rb', line 21

def self.options
    [
      ['--source_root', 'Specify the warehouse address manually if necessary.'],
      ['--names', 'set hook name,include "push_count_checker|commit_lfs_checker|commands|all",defaule commands'],
      ['--commit_limit', 'Upper count limit checked in the prepush hook. default 5000'],
      ['--master_name', 'The name of the repository main branch,defalut is develop'],
      ['--file_size_limit', 'Upper file size limit checked in the prepush hook.default 2097152B (2MB)'],
    ].concat(super)
end

Instance Method Details

#append_hook(name, content, hook) ⇒ Object



242
243
244
245
246
247
248
249
# File 'lib/git-thin/command/hook.rb', line 242

def append_hook(name,content,hook)
    start_flag = '# '+@start_flag+' git thin '+name+@start_flag+"\n"
    end_flag = '# '+@end_flag+' git thin '+name+@end_flag+"\n"
    content += "\n\n #{start_flag}\n"
    content += hook
    content += "\n #{end_flag}"

end

#check_nameObject



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/git-thin/command/hook.rb', line 148

def check_name
    pass = true
    for name in @names
        name = name.downcase
        if !@support_names.include?(name) && name.downcase != 'all'
            logE "name [#{name}] is not supported"
            pass = false
        end
    end
    if false
        @names = []
    end
end

#commands_hookObject



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
# File 'lib/git-thin/command/hook.rb', line 31

def commands_hook
    <<-DESC
        function git(){
            o_git=`whereis git`
            
            if [ -z "$o_git" ] || ! command -v git-thin >/dev/null 2>&1;then
                echo 'git or git-thin command failed, finish hook'
                unset -f git
                git "$@"
            else
                if [[ "$1" == "checkout" ]] || [[ "$1" == "pull" ]] || [[ "$1" == "fetch" ]];then
                   git-thin "$@"
                   ret=$?
                   if [ $ret -gt 100 ];then
                        return $ret-100
                   elif [ $ret -ne 0 ];then
                        echo 'git-thin command error,retry with git'
                        $o_git "$@"
                   else
                        return $ret
                   fi
                else
                    $o_git "$@"
                fi
            fi
            return $?
        }
        export -f git > /dev/null
    DESC
end

#commit_msg_hookObject



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
# File 'lib/git-thin/command/hook.rb', line 72

def commit_msg_hook
    %Q{
        echo ''
        echo '======start lfs check========'
        root=`git rev-parse --show-toplevel`
        START_LINE=`head -n1 $1`
        if [[ -f $root/.git/MERGE_MSG ]] || [[ $START_LINE == "Merge"* ]];then
            echo "Current in merging,skip lfs check"
            exit 0
        fi
        map(){
            function_name=$1
            shift
            while read line
            do
                $function_name "$line" "$@"
            done
        }
        
        white_list=(mm m cpp c cc h hpp java cs cjs js css txt doc docx pdf ppt pptx xls htm html shtm shtml jsp xjsp jspf jsf jsx jspx md pl pm sass sh zsh bash rb json xml yml yaml cjsx uml es6 mjs ejs bat cgi cmd csv jam log opts mpmrc nvmrc vue xhtml xsl xslt elt)
        OLDIFS=$IFS
        IFS=$'\n'
        lfs_files=`git lfs ls-files`
        lfs_files=($lfs_files)
        IFS='*'
        for (( i = 0; i < ${#lfs_files[@]}; i++ )); do
            file_item=(${lfs_files[i]})
            lfs_files[i]=${file_item[1]}
        done
        export has_gitattributes=0
        check_file(){
            echo $1
           if [[ $1 =~ 'gitattributes' ]];then
                export has_gitattributes=1
                echo "has_gitattributes:$has_gitattributes"
           fi
           file_name=$1
           file_ext=${file_name##*.}
           file_ext=$(echo "$file_ext" | tr [A-Z] [a-z])
           for item in ${white_list[@]};do
              if [ "$file_ext" = "$item" ];then
                  return
              fi
           done
            
            for (( i = 0; i < ${#lfs_files[@]}; i++ )); do
                if [[ "${lfs_files[i]}" == " $file_name" ]];then
                     echo "file ${file_name} already in lfs, pass"
                     return
                fi
            done
            
            if [ -f "$file_name" ];then
                size=`ls -l $file_name | awk '{ print $5 }'`
                if [ "$size" -gt #{@file_size_limit} ];then
                    echo "修改的非代码文件超过#{@file_size_limit}KB,请使用git lfs管理,文件名称:$file_name"
                    exit 1
                fi
            fi
        }
        
        git diff --name-only --staged | map check_file
        if [ $? -ne 0 ];then
            echo "lfs check failed"
            exit 1
        fi
        IFS=$OLDIFS
        if [ `git diff --name-only | grep '.gitattributes' -c` -gt 0 ];then
            echo '存在未提交的.gitattributes文件,请主动add改文件。如果确认不需要则请重置该文件'
            echo "lfs check failed"
            exit 1
        fi
        echo "lfs check pass"
        echo ""
   }
end

#installObject



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
# File 'lib/git-thin/command/hook.rb', line 283

def install
    if @names.include? 'push_count_checker'
        Dir.chdir @source_root
        find_branch = false
        run_shell 'git branch -r',true ,true do |outs,errs,status|
            if status == 0
                for out in outs
                    items = out.strip.split '/'
                    if items.length == 2 && items[1] == @master_name
                        find_branch = true
                        break
                    end
                end
            end
        end

        run_shell 'git remote',true ,true do |outs,errs,status|
            if status == 0 && outs.length > 0
                @origin = outs[0]
            else
                @origin = 'origin'
            end
        end
        if !find_branch
            logE "install push_count_checker hook error:the main branch [#{@master_name}] is not exist,please specify an appropriate parameter for master_name"
            exit 1
        end

        install_hook @hook_path+'/pre-push','push_count_checker',pre_push_hook
    end
    if @names.include? 'commit_lfs_checker'
        install_hook @hook_path+'/commit-msg','commit_lfs_checker',commit_msg_hook
    end

    if @names.include? 'commands'
        profile = Dir.home+"/.bash_profile"
        install_hook profile,'commands_hook',commands_hook
        run_shell"cat #{profile}>tmp &&  source tmp && rm tmp",true ,LOGPRUNE
        logN 'This will take effect after restarting the computer'
    end

end

#install_hook(path, name, hook) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/git-thin/command/hook.rb', line 267

def install_hook(path,name,hook)
    origin = []
    if File.exist? path
        File.open(path, "r") do |aFile|
            origin = aFile.readlines
        end
    end

    prune = prune_hook(name,origin).join("")
    content = append_hook name,prune,hook
    File.open(path, "w+") do |aFile|
        aFile.write content
    end
    logN 'success install hook:'+name+' to path:'+path
end

#pre_push_hookObject



62
63
64
65
66
67
68
69
70
# File 'lib/git-thin/command/hook.rb', line 62

def pre_push_hook
    <<-DESC
        count=`git log --oneline #{@origin}/#{@master_name}..HEAD | wc -l`
        if [ $count -gt #{@commit_limit} ];then
            echo "commit count:${count} is greater than #{@commit_limit} (base #{@master_name}), please contact kyle.zhou"
            exit 1
        fi
    DESC
end

#prune_hook(name, content) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/git-thin/command/hook.rb', line 217

def prune_hook(name,content)
    start_flag = '# '+@start_flag+' git thin '+name+@start_flag
    end_flag = '# '+@end_flag+' git thin '+name+@end_flag
    find_start = false
    find_end = false
    lines = []
    content.each do |line|
        if line.index(start_flag)
            find_start = true
        end
        if line.index(end_flag) && find_start
            find_end = true
        end

        if !find_start && !find_end
            lines.push line
        end

        if find_start && find_end
            find_start = find_end = false
        end
    end
    return lines
end

#runObject



348
349
350
351
352
353
354
355
# File 'lib/git-thin/command/hook.rb', line 348

def run
    Dir.chdir @source_root
    if @action == 'install'
        install
    elsif @action =='uninstall'
        uninstall
    end
end

#run_configObject



342
343
344
345
346
# File 'lib/git-thin/command/hook.rb', line 342

def run_config
    values=[]

    return values
end

#uninstallObject



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/git-thin/command/hook.rb', line 326

def uninstall
    if @names.include? 'push_count_checker'
        uninstall_hook @hook_path+'/pre-push','push_count_checker'
    end
    if @names.include? 'commit_lfs_checker'
        uninstall_hook @hook_path+'/commit-msg','commit_lfs_checker'
    end
    if @names.include? 'commands'
        profile = Dir.home+"/.bash_profile"
        uninstall_hook profile,'commands_hook'
        run_shell'echo "unset -f git" >tmp &&  source tmp && rm tmp',true ,LOGPRUNE
        logN 'This will take effect after restarting the computer'
    end
    logN 'finish uninstall'
end

#uninstall_hook(path, name) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/git-thin/command/hook.rb', line 250

def uninstall_hook(path,name)
    origin = []
    if File.exist? path
        File.open(path, "r") do |aFile|
            origin = aFile.readlines
        end
    end

    prune = prune_hook(name,origin)
    content = prune.join("")
    File.open(path, "w+") do |aFile|
        aFile.write content
    end
    if prune.length != origin.length
        logN 'success uninstall hook:'+name+' to path:'+path
    end
end

#validate!Object



209
210
211
212
213
214
215
# File 'lib/git-thin/command/hook.rb', line 209

def validate!
    super
    help! 'validate SOURCE_ROOT is required' unless @source_root
    help! 'action must install/uninstall' unless @action
    help! 'validate names is required' unless @names.length

end