|
| 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 | + "strings" |
| 28 | + |
| 29 | + "github.com/magefile/mage/mg" |
| 30 | + "github.com/magefile/mage/sh" |
| 31 | + |
| 32 | + "github.com/elastic/elastic-agent-libs/processors/dissect" |
| 33 | +) |
| 34 | + |
| 35 | +func CheckNoChanges() error { |
| 36 | + changes, err := GitDiffIndex() |
| 37 | + if err != nil { |
| 38 | + return fmt.Errorf("failed to diff the git index: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + if len(changes) > 0 { |
| 42 | + if mg.Verbose() { |
| 43 | + err = GitDiff() |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("failed to run git diff: %w", err) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return fmt.Errorf("some files are not up-to-date. "+ |
| 50 | + "Run 'mage update' then review and commit the changes. "+ |
| 51 | + "Modified: %v", changes) |
| 52 | + } |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +// GitDiffIndex returns a list of files that differ from what is committed. |
| 57 | +// These could file that were created, deleted, modified, or moved. |
| 58 | +func GitDiffIndex() ([]string, error) { |
| 59 | + // Ensure the index is updated so that diff-index gives accurate results. |
| 60 | + if err := sh.Run("git", "update-index", "-q", "--refresh"); err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + // git diff-index provides a list of modified files. |
| 65 | + // https://www.git-scm.com/docs/git-diff-index |
| 66 | + out, err := sh.Output("git", "diff-index", "HEAD", "--", ".") |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + // Example formats. |
| 72 | + // :100644 100644 bcd1234... 0123456... M file0 |
| 73 | + // :100644 100644 abcd123... 1234567... R86 file1 file3 |
| 74 | + d, err := dissect.New(":%{src_mode} %{dst_mode} %{src_sha1} %{dst_sha1} %{status}\t%{paths}") |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + // Parse lines. |
| 80 | + var modified []string |
| 81 | + s := bufio.NewScanner(bytes.NewBufferString(out)) |
| 82 | + for s.Scan() { |
| 83 | + m, err := d.Dissect(s.Text()) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("failed to dissect git diff-index output: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + paths := strings.Split(m["paths"], "\t") |
| 89 | + if len(paths) > 1 { |
| 90 | + modified = append(modified, paths[1]) |
| 91 | + } else { |
| 92 | + modified = append(modified, paths[0]) |
| 93 | + } |
| 94 | + } |
| 95 | + if err = s.Err(); err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + return modified, nil |
| 100 | +} |
| 101 | + |
| 102 | +// GitDiff runs 'git diff' and writes the output to stdout. |
| 103 | +func GitDiff() error { |
| 104 | + c := exec.Command("git", "--no-pager", "diff", "--minimal") |
| 105 | + c.Stdin = nil |
| 106 | + c.Stdout = os.Stdout |
| 107 | + c.Stderr = os.Stderr |
| 108 | + log.Println("exec:", strings.Join(c.Args, " ")) |
| 109 | + err := c.Run() |
| 110 | + return err |
| 111 | +} |
0 commit comments