github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/cmd/noms/noms_commit.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package main
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  	"os"
    11  
    12  	"github.com/attic-labs/kingpin"
    13  	"github.com/attic-labs/noms/cmd/util"
    14  	"github.com/attic-labs/noms/go/config"
    15  	"github.com/attic-labs/noms/go/d"
    16  	"github.com/attic-labs/noms/go/datas"
    17  	"github.com/attic-labs/noms/go/spec"
    18  )
    19  
    20  func nomsCommit(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
    21  	commit := noms.Command("commit", "Commits a value to a dataset.")
    22  	allowDupe := commit.Flag("allow-dupe", "creates a new commit, even if it would be identical (modulo metadata and parents) to the existing HEAD").Bool()
    23  	message := commit.Flag("message", "commit message").String()
    24  	date := commit.Flag("date", "commit date formatted as 2019-08-08T21:52:46Z - defaults to current date").String()
    25  	path := commit.Arg("absolute-path", "absolute path to value to commit - see See Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spelling.md").Required().String()
    26  	ds := commit.Arg("dataset", "dataset spec to commit to - see Spelling Datasets at https://github.com/attic-labs/noms/blob/master/doc/spelling.md").Required().String()
    27  
    28  	return commit, func(input string) int {
    29  		cfg := config.NewResolver()
    30  		db, ds, err := cfg.GetDataset(*ds)
    31  		d.CheckError(err)
    32  		defer db.Close()
    33  
    34  		absPath, err := spec.NewAbsolutePath(*path)
    35  		d.CheckError(err)
    36  
    37  		value := absPath.Resolve(db)
    38  		if value == nil {
    39  			d.CheckErrorNoUsage(errors.New(fmt.Sprintf("Error resolving value: %s", *path)))
    40  		}
    41  
    42  		oldCommitRef, oldCommitExists := ds.MaybeHeadRef()
    43  		if oldCommitExists {
    44  			head := ds.HeadValue()
    45  			if head.Hash() == value.Hash() && !*allowDupe {
    46  				fmt.Fprintf(os.Stdout, "Commit aborted - allow-dupe is set to off and this commit would create a duplicate\n")
    47  				return 0
    48  			}
    49  		}
    50  
    51  		meta, err := spec.CreateCommitMetaStruct(db, *date, *message, nil, nil)
    52  		d.CheckErrorNoUsage(err)
    53  
    54  		ds, err = db.Commit(ds, value, datas.CommitOptions{Meta: meta})
    55  		d.CheckErrorNoUsage(err)
    56  
    57  		if oldCommitExists {
    58  			fmt.Fprintf(os.Stdout, "New head #%v (was #%v)\n", ds.HeadRef().TargetHash().String(), oldCommitRef.TargetHash().String())
    59  		} else {
    60  			fmt.Fprintf(os.Stdout, "New head #%v\n", ds.HeadRef().TargetHash().String())
    61  		}
    62  		return 0
    63  	}
    64  }