Files
dolt/cmd/noms/noms_commit.go
Eric Halpern d9715dba0e Support db aliases and default db for noms cli
This patch implements evolving support for configuring aliases and defaults for the noms cli (started with #2131)

For an introduction, please take a look at the sample code here: https://github.com/attic-labs/noms/blob/master/samples/cli/nomsconfig/README.md

Improvements include: 

 - All go samples now work with .nomsconfig
 - Absolute paths in ldb specs are now properly handled 
 - Add -v|--verbose flag to commands to debug expansion
 - Make default just another alias and change [default] section to [db.default]
 - Introduce the `.` shorthand to refer to a previously mentioned dataset/object
2016-09-27 22:21:32 -07:00

85 lines
2.6 KiB
Go

// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package main
import (
"bufio"
"errors"
"fmt"
"os"
"github.com/attic-labs/noms/cmd/util"
"github.com/attic-labs/noms/go/config"
"github.com/attic-labs/noms/go/d"
"github.com/attic-labs/noms/go/datas"
"github.com/attic-labs/noms/go/spec"
"github.com/attic-labs/noms/go/util/verbose"
flag "github.com/juju/gnuflag"
)
var allowDupe bool
var nomsCommit = &util.Command{
Run: runCommit,
UsageLine: "commit [options] [absolute-path] <dataset>",
Short: "Commits a specified value as head of the dataset",
Long: "If absolute-path is not provided, then it is read from stdin. See Spelling Objects at https://github.com/attic-labs/noms/blob/master/doc/spelling.md for details on the dataset and absolute-path arguments.",
Flags: setupCommitFlags,
Nargs: 1, // if absolute-path not present we read it from stdin
}
func setupCommitFlags() *flag.FlagSet {
commitFlagSet := flag.NewFlagSet("commit", flag.ExitOnError)
commitFlagSet.BoolVar(&allowDupe, "allow-dupe", false, "creates a new commit, even if it would be identical (modulo metadata and parents) to the existing HEAD.")
spec.RegisterCommitMetaFlags(commitFlagSet)
verbose.RegisterVerboseFlags(commitFlagSet)
return commitFlagSet
}
func runCommit(args []string) int {
cfg := config.NewResolver()
db, ds, err := cfg.GetDataset(args[len(args)-1])
d.CheckError(err)
defer db.Close()
var path string
if len(args) == 2 {
path = args[0]
} else {
readPath, _, err := bufio.NewReader(os.Stdin).ReadLine()
d.CheckError(err)
path = string(readPath)
}
absPath, err := spec.NewAbsolutePath(path)
d.CheckError(err)
value := absPath.Resolve(db)
if value == nil {
d.CheckErrorNoUsage(errors.New(fmt.Sprintf("Error resolving value: %s", path)))
}
oldCommitRef, oldCommitExists := ds.MaybeHeadRef()
if oldCommitExists {
head := ds.HeadValue()
if head.Hash() == value.Hash() && !allowDupe {
fmt.Fprintf(os.Stdout, "Commit aborted - allow-dupe is set to off and this commit would create a duplicate\n")
return 0
}
}
meta, err := spec.CreateCommitMetaStruct(db, "", "", nil, nil)
d.CheckErrorNoUsage(err)
ds, err = db.Commit(ds, value, datas.CommitOptions{Meta: meta})
d.CheckErrorNoUsage(err)
if oldCommitExists {
fmt.Fprintf(os.Stdout, "New head #%v (was #%v)\n", ds.HeadRef().TargetHash().String(), oldCommitRef.TargetHash().String())
} else {
fmt.Fprintf(os.Stdout, "New head #%v\n", ds.HeadRef().TargetHash().String())
}
return 0
}