|
| 1 | +<?php |
| 2 | +$repo = git_repository_open("."); |
| 3 | + |
| 4 | +$a = array( |
| 5 | + // attributes are white space separated string. |
| 6 | + // e.g) .gitattribute |
| 7 | + // *.php filter=chobie |
| 8 | + // |
| 9 | + // will return $attr[0] = false, $attr[1] = "chobie" with *.php files. see check callback. |
| 10 | + "attributes" => "chobie filter", |
| 11 | + "initialize" => function () { |
| 12 | + echo "\e[32m# Initialize\e[m\n"; |
| 13 | + }, |
| 14 | + "check" => function ($payload, $src, $attr) { |
| 15 | + echo "\e[32m# Check\e[m\n"; |
| 16 | + var_dump($src); |
| 17 | + var_dump($attr); |
| 18 | + |
| 19 | + // return true means apply filter to this file. |
| 20 | + return true; |
| 21 | + }, |
| 22 | + "apply" => function ($payload, $from, $src) { |
| 23 | + echo "\e[32m# Apply\e[m\n"; |
| 24 | + // apply function should return string or GIT_PASSTHROUGH |
| 25 | + return preg_replace("/\s/", "", $from); |
| 26 | + }, |
| 27 | + "shutdown" => function () { |
| 28 | + echo "\n\e[32m# Shutdown\e[m\n"; |
| 29 | + }, |
| 30 | + "cleanup" => function () { |
| 31 | + echo "\e[32m# clean up\e[m\n"; |
| 32 | + } |
| 33 | +); |
| 34 | + |
| 35 | +$v = git_filter_new($a); |
| 36 | +git_filter_register("chobie", $v, 100); |
| 37 | + |
| 38 | +$blob = git_blob_lookup($repo, "74f5770df516cbbef16372a7628a9528277637d6"); |
| 39 | +$l = git_filter_list_load($repo, $blob, "example/diff.php", GIT_FILTER_SMUDGE); |
| 40 | + |
| 41 | +echo "\e[32m# <<< ORIGINAL CONTENT >>>\e[m\n"; |
| 42 | +echo git_blob_rawcontent($blob); |
| 43 | + |
| 44 | +echo "\e[32m# <<< FILTERED CONTENT >>>\e[m\n"; |
| 45 | +$out = git_filter_list_apply_to_blob($l, $blob); |
| 46 | +echo $out; |
0 commit comments