|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package mage |
| 19 | + |
| 20 | +import ( |
| 21 | + "bufio" |
| 22 | + "bytes" |
| 23 | + "fmt" |
| 24 | + "log" |
| 25 | + "os" |
| 26 | + "os/exec" |
| 27 | + "path/filepath" |
| 28 | + "runtime" |
| 29 | + "strings" |
| 30 | + |
| 31 | + "github.com/magefile/mage/mg" |
| 32 | + "github.com/magefile/mage/sh" |
| 33 | + "github.com/pkg/errors" |
| 34 | + |
| 35 | + "github.com/elastic/beats/libbeat/processors/dissect" |
| 36 | +) |
| 37 | + |
| 38 | +// Check looks for created/modified/deleted/renamed files and returns an error |
| 39 | +// if it finds any modifications. If executed in in verbose mode it will write |
| 40 | +// the results of 'git diff' to stdout to indicate what changes have been made. |
| 41 | +// |
| 42 | +// It also checks the file permissions of nosetests test cases and YAML files. |
| 43 | +func Check() error { |
| 44 | + fmt.Println(">> check: Checking for modified files or incorrect permissions") |
| 45 | + |
| 46 | + mg.Deps(CheckNosetestsNotExecutable, CheckYAMLNotExecutable) |
| 47 | + |
| 48 | + changes, err := GitDiffIndex() |
| 49 | + if err != nil { |
| 50 | + return errors.Wrap(err, "failed to diff the git index") |
| 51 | + } |
| 52 | + |
| 53 | + if len(changes) > 0 { |
| 54 | + if mg.Verbose() { |
| 55 | + GitDiff() |
| 56 | + } |
| 57 | + |
| 58 | + return errors.Errorf("some files are not up-to-date. "+ |
| 59 | + "Run 'mage fmt update' then review and commit the changes. "+ |
| 60 | + "Modified: %v", changes) |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +// GitDiffIndex returns a list of files that differ from what is committed. |
| 66 | +// These could file that were created, deleted, modified, or moved. |
| 67 | +func GitDiffIndex() ([]string, error) { |
| 68 | + // Ensure the index is updated so that diff-index gives accurate results. |
| 69 | + if err := sh.Run("git", "update-index", "-q", "--refresh"); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + // git diff-index provides a list of modified files. |
| 74 | + // https://www.git-scm.com/docs/git-diff-index |
| 75 | + out, err := sh.Output("git", "diff-index", "HEAD", "--", ".") |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + // Example formats. |
| 81 | + // :100644 100644 bcd1234... 0123456... M file0 |
| 82 | + // :100644 100644 abcd123... 1234567... R86 file1 file3 |
| 83 | + d, err := dissect.New(":%{src_mode} %{dst_mode} %{src_sha1} %{dst_sha1} %{status}\t%{paths}") |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + // Parse lines. |
| 89 | + var modified []string |
| 90 | + s := bufio.NewScanner(bytes.NewBufferString(out)) |
| 91 | + for s.Scan() { |
| 92 | + m, err := d.Dissect(s.Text()) |
| 93 | + if err != nil { |
| 94 | + return nil, errors.Wrap(err, "failed to dissect git diff-index output") |
| 95 | + } |
| 96 | + |
| 97 | + paths := strings.Split(m["paths"], "\t") |
| 98 | + if len(paths) > 1 { |
| 99 | + modified = append(modified, paths[1]) |
| 100 | + } else { |
| 101 | + modified = append(modified, paths[0]) |
| 102 | + } |
| 103 | + } |
| 104 | + if err = s.Err(); err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + return modified, nil |
| 109 | +} |
| 110 | + |
| 111 | +// GitDiff runs 'git diff' and writes the output to stdout. |
| 112 | +func GitDiff() error { |
| 113 | + c := exec.Command("git", "--no-pager", "diff", "--minimal") |
| 114 | + c.Stdin = nil |
| 115 | + c.Stdout = os.Stdout |
| 116 | + c.Stderr = os.Stderr |
| 117 | + log.Println("exec:", strings.Join(c.Args, " ")) |
| 118 | + err := c.Run() |
| 119 | + return err |
| 120 | +} |
| 121 | + |
| 122 | +// CheckNosetestsNotExecutable checks that none of the nosetests files are |
| 123 | +// executable. Nosetests silently skips executable .py files and we don't want |
| 124 | +// this to happen. |
| 125 | +func CheckNosetestsNotExecutable() error { |
| 126 | + if runtime.GOOS == "windows" { |
| 127 | + // Skip windows because it doesn't have POSIX permissions. |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + tests, err := FindFiles(nosetestsTestFiles...) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + var executableTestFiles []string |
| 137 | + for _, file := range tests { |
| 138 | + info, err := os.Stat(file) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + if info.Mode().Perm()&0111 > 0 { |
| 144 | + executableTestFiles = append(executableTestFiles, file) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if len(executableTestFiles) > 0 { |
| 149 | + return errors.Errorf("nosetests files cannot be executable because "+ |
| 150 | + "they will be skipped. Fix permissions of %v", executableTestFiles) |
| 151 | + } |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +// CheckYAMLNotExecutable checks that no .yml or .yaml files are executable. |
| 156 | +func CheckYAMLNotExecutable() error { |
| 157 | + if runtime.GOOS == "windows" { |
| 158 | + // Skip windows because it doesn't have POSIX permissions. |
| 159 | + return nil |
| 160 | + } |
| 161 | + |
| 162 | + executableYAMLFiles, err := FindFilesRecursive(func(path string, info os.FileInfo) bool { |
| 163 | + switch filepath.Ext(path) { |
| 164 | + default: |
| 165 | + return false |
| 166 | + case ".yml", ".yaml": |
| 167 | + return info.Mode().Perm()&0111 > 0 |
| 168 | + } |
| 169 | + }) |
| 170 | + if err != nil { |
| 171 | + return errors.Wrap(err, "failed search for YAML files") |
| 172 | + } |
| 173 | + |
| 174 | + if len(executableYAMLFiles) > 0 { |
| 175 | + return errors.Errorf("YAML files cannot be executable. Fix "+ |
| 176 | + "permissions of %v", executableYAMLFiles) |
| 177 | + |
| 178 | + } |
| 179 | + return nil |
| 180 | +} |
0 commit comments