github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/cmd/gazelle/diff.go (about)

     1  /* Copyright 2016 The Bazel Authors. All rights reserved.
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package main
    17  
    18  import (
    19  	"bytes"
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/bazelbuild/bazel-gazelle/config"
    26  	"github.com/bazelbuild/bazel-gazelle/rule"
    27  	"github.com/pmezard/go-difflib/difflib"
    28  )
    29  
    30  var errExit = fmt.Errorf("encountered changes while running diff")
    31  
    32  func diffFile(c *config.Config, f *rule.File) error {
    33  	rel, err := filepath.Rel(c.RepoRoot, f.Path)
    34  	if err != nil {
    35  		return fmt.Errorf("error getting old path for file %q: %v", f.Path, err)
    36  	}
    37  	rel = filepath.ToSlash(rel)
    38  
    39  	// The epoch timestamp is assumed to represent file creation/deletion events
    40  	// by some tools, so use a dummy timestamp that is one ns past the epoch.
    41  	// See https://github.com/bazelbuild/bazel-gazelle/issues/1528.
    42  	date := "1970-01-01 00:00:00.000000001 +0000"
    43  
    44  	diff := difflib.UnifiedDiff{
    45  		Context:  3,
    46  		FromDate: date,
    47  		ToDate:   date,
    48  	}
    49  
    50  	newContent := f.Format()
    51  	if bytes.Equal(newContent, f.Content) {
    52  		// No change.
    53  		return nil
    54  	}
    55  
    56  	if _, err := os.Stat(f.Path); os.IsNotExist(err) {
    57  		diff.FromFile = "/dev/null"
    58  	} else if err != nil {
    59  		return fmt.Errorf("error reading original file: %v", err)
    60  	} else if c.ReadBuildFilesDir == "" {
    61  		diff.FromFile = rel
    62  	} else {
    63  		diff.FromFile = f.Path
    64  	}
    65  
    66  	if len(f.Content) != 0 {
    67  		diff.A = difflib.SplitLines(string(f.Content))
    68  	}
    69  
    70  	diff.B = difflib.SplitLines(string(newContent))
    71  	outPath := findOutputPath(c, f)
    72  	if c.WriteBuildFilesDir == "" {
    73  		diff.ToFile = rel
    74  	} else {
    75  		diff.ToFile = outPath
    76  	}
    77  
    78  	uc := getUpdateConfig(c)
    79  	var out io.Writer = os.Stdout
    80  	if uc.patchPath != "" {
    81  		out = &uc.patchBuffer
    82  	}
    83  	if err := difflib.WriteUnifiedDiff(out, diff); err != nil {
    84  		return fmt.Errorf("error diffing %s: %v", f.Path, err)
    85  	}
    86  	if ds, _ := difflib.GetUnifiedDiffString(diff); ds != "" {
    87  		return errExit
    88  	}
    89  
    90  	return nil
    91  }