github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/cmd/noms/noms_diff.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package main
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  
    28  	flag "github.com/juju/gnuflag"
    29  
    30  	"github.com/dolthub/dolt/go/store/cmd/noms/util"
    31  	"github.com/dolthub/dolt/go/store/config"
    32  	"github.com/dolthub/dolt/go/store/d"
    33  	"github.com/dolthub/dolt/go/store/diff"
    34  	"github.com/dolthub/dolt/go/store/util/outputpager"
    35  	"github.com/dolthub/dolt/go/store/util/verbose"
    36  )
    37  
    38  var stat bool
    39  
    40  var nomsDiff = &util.Command{
    41  	Run:       runDiff,
    42  	UsageLine: "diff [--stat] <object1> <object2>",
    43  	Short:     "Shows the difference between two objects",
    44  	Long:      "See Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spelling.md for details on the object arguments.",
    45  	Flags:     setupDiffFlags,
    46  	Nargs:     2,
    47  }
    48  
    49  func setupDiffFlags() *flag.FlagSet {
    50  	diffFlagSet := flag.NewFlagSet("diff", flag.ExitOnError)
    51  	diffFlagSet.BoolVar(&stat, "stat", false, "Writes a summary of the changes instead")
    52  	outputpager.RegisterOutputpagerFlags(diffFlagSet)
    53  	verbose.RegisterVerboseFlags(diffFlagSet)
    54  
    55  	return diffFlagSet
    56  }
    57  
    58  func runDiff(ctx context.Context, args []string) int {
    59  	cfg := config.NewResolver()
    60  	db1, value1, err := cfg.GetPath(ctx, args[0])
    61  	util.CheckErrorNoUsage(err)
    62  	if value1 == nil {
    63  		util.CheckErrorNoUsage(fmt.Errorf("Object not found: %s", args[0]))
    64  	}
    65  	defer db1.Close()
    66  
    67  	db2, value2, err := cfg.GetPath(ctx, args[1])
    68  	util.CheckErrorNoUsage(err)
    69  	if value2 == nil {
    70  		util.CheckErrorNoUsage(fmt.Errorf("Object not found: %s", args[1]))
    71  	}
    72  	defer db2.Close()
    73  
    74  	d.PanicIfFalse(db1.Format() == db2.Format())
    75  
    76  	if stat {
    77  		diff.Summary(ctx, value1, value2)
    78  		return 0
    79  	}
    80  
    81  	pgr := outputpager.Start()
    82  	defer pgr.Stop()
    83  
    84  	diff.PrintDiff(ctx, pgr.Writer, value1, value2, false)
    85  	return 0
    86  }