mirror of
https://github.com/dolthub/dolt.git
synced 2026-02-14 01:07:08 -06:00
Merge pull request #5564 from solipsis-project/show-command
`dolt show` command
This commit is contained in:
@@ -98,20 +98,28 @@ The {{.EmphasisLeft}}--diff-mode{{.EmphasisRight}} argument controls how modifie
|
||||
},
|
||||
}
|
||||
|
||||
type diffArgs struct {
|
||||
type diffDisplaySettings struct {
|
||||
diffParts diffPart
|
||||
diffOutput diffOutput
|
||||
diffMode diff.Mode
|
||||
fromRoot *doltdb.RootValue
|
||||
toRoot *doltdb.RootValue
|
||||
fromRef string
|
||||
toRef string
|
||||
tableSet *set.StrSet
|
||||
limit int
|
||||
where string
|
||||
skinny bool
|
||||
}
|
||||
|
||||
type diffDatasets struct {
|
||||
fromRoot *doltdb.RootValue
|
||||
toRoot *doltdb.RootValue
|
||||
fromRef string
|
||||
toRef string
|
||||
}
|
||||
|
||||
type diffArgs struct {
|
||||
*diffDisplaySettings
|
||||
*diffDatasets
|
||||
tableSet *set.StrSet
|
||||
}
|
||||
|
||||
type DiffCmd struct{}
|
||||
|
||||
// Name is returns the name of the Dolt cli command. This is what is used on the command line to invoke the command
|
||||
@@ -167,9 +175,6 @@ func (cmd DiffCmd) Exec(ctx context.Context, commandStr string, args []string, d
|
||||
}
|
||||
|
||||
verr = diffUserTables(ctx, dEnv, dArgs)
|
||||
if verr != nil {
|
||||
return HandleVErrAndExitCode(verr, usage)
|
||||
}
|
||||
return HandleVErrAndExitCode(verr, usage)
|
||||
}
|
||||
|
||||
@@ -190,69 +195,89 @@ func (cmd DiffCmd) validateArgs(apr *argparser.ArgParseResults) errhand.VerboseE
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseDiffArgs(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgParseResults) (*diffArgs, error) {
|
||||
dArgs := &diffArgs{}
|
||||
func parseDiffDisplaySettings(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgParseResults) *diffDisplaySettings {
|
||||
displaySettings := &diffDisplaySettings{}
|
||||
|
||||
dArgs.diffParts = SchemaAndDataDiff
|
||||
displaySettings.diffParts = SchemaAndDataDiff
|
||||
if apr.Contains(DataFlag) && !apr.Contains(SchemaFlag) {
|
||||
dArgs.diffParts = DataOnlyDiff
|
||||
displaySettings.diffParts = DataOnlyDiff
|
||||
} else if apr.Contains(SchemaFlag) && !apr.Contains(DataFlag) {
|
||||
dArgs.diffParts = SchemaOnlyDiff
|
||||
displaySettings.diffParts = SchemaOnlyDiff
|
||||
} else if apr.Contains(StatFlag) {
|
||||
dArgs.diffParts = Stat
|
||||
displaySettings.diffParts = Stat
|
||||
} else if apr.Contains(SummaryFlag) {
|
||||
dArgs.diffParts = Summary
|
||||
displaySettings.diffParts = Summary
|
||||
}
|
||||
|
||||
dArgs.skinny = apr.Contains(SkinnyFlag)
|
||||
displaySettings.skinny = apr.Contains(SkinnyFlag)
|
||||
|
||||
f := apr.GetValueOrDefault(FormatFlag, "tabular")
|
||||
switch strings.ToLower(f) {
|
||||
case "tabular":
|
||||
dArgs.diffOutput = TabularDiffOutput
|
||||
displaySettings.diffOutput = TabularDiffOutput
|
||||
switch strings.ToLower(apr.GetValueOrDefault(DiffMode, "context")) {
|
||||
case "row":
|
||||
dArgs.diffMode = diff.ModeRow
|
||||
displaySettings.diffMode = diff.ModeRow
|
||||
case "line":
|
||||
dArgs.diffMode = diff.ModeLine
|
||||
displaySettings.diffMode = diff.ModeLine
|
||||
case "in-place":
|
||||
dArgs.diffMode = diff.ModeInPlace
|
||||
displaySettings.diffMode = diff.ModeInPlace
|
||||
case "context":
|
||||
dArgs.diffMode = diff.ModeContext
|
||||
displaySettings.diffMode = diff.ModeContext
|
||||
}
|
||||
case "sql":
|
||||
dArgs.diffOutput = SQLDiffOutput
|
||||
displaySettings.diffOutput = SQLDiffOutput
|
||||
case "json":
|
||||
dArgs.diffOutput = JsonDiffOutput
|
||||
displaySettings.diffOutput = JsonDiffOutput
|
||||
}
|
||||
|
||||
dArgs.limit, _ = apr.GetInt(limitParam)
|
||||
dArgs.where = apr.GetValueOrDefault(whereParam, "")
|
||||
displaySettings.limit, _ = apr.GetInt(limitParam)
|
||||
displaySettings.where = apr.GetValueOrDefault(whereParam, "")
|
||||
|
||||
return displaySettings
|
||||
}
|
||||
|
||||
func parseDiffArgs(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgParseResults) (*diffArgs, error) {
|
||||
dArgs := &diffArgs{
|
||||
diffDisplaySettings: parseDiffDisplaySettings(ctx, dEnv, apr),
|
||||
}
|
||||
|
||||
tableNames, err := dArgs.applyDiffRoots(ctx, dEnv, apr.Args, apr.Contains(cli.CachedFlag), apr.Contains(MergeBase))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dArgs.tableSet = set.NewStrSet(nil)
|
||||
tableSet, err := parseDiffTableSet(ctx, dEnv, dArgs.diffDatasets, tableNames)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dArgs.tableSet = tableSet
|
||||
|
||||
return dArgs, nil
|
||||
}
|
||||
|
||||
func parseDiffTableSet(ctx context.Context, dEnv *env.DoltEnv, datasets *diffDatasets, tableNames []string) (*set.StrSet, error) {
|
||||
|
||||
tableSet := set.NewStrSet(nil)
|
||||
|
||||
for _, tableName := range tableNames {
|
||||
// verify table args exist in at least one root
|
||||
_, ok, err := dArgs.fromRoot.GetTable(ctx, tableName)
|
||||
_, ok, err := datasets.fromRoot.GetTable(ctx, tableName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ok {
|
||||
dArgs.tableSet.Add(tableName)
|
||||
tableSet.Add(tableName)
|
||||
continue
|
||||
}
|
||||
|
||||
_, ok, err = dArgs.toRoot.GetTable(ctx, tableName)
|
||||
_, ok, err = datasets.toRoot.GetTable(ctx, tableName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ok {
|
||||
dArgs.tableSet.Add(tableName)
|
||||
tableSet.Add(tableName)
|
||||
continue
|
||||
}
|
||||
if !ok {
|
||||
@@ -262,14 +287,14 @@ func parseDiffArgs(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgPar
|
||||
|
||||
// if no tables or docs were specified as args, diff all tables and docs
|
||||
if len(tableNames) == 0 {
|
||||
utn, err := doltdb.UnionTableNames(ctx, dArgs.fromRoot, dArgs.toRoot)
|
||||
utn, err := doltdb.UnionTableNames(ctx, datasets.fromRoot, datasets.toRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dArgs.tableSet.Add(utn...)
|
||||
tableSet.Add(utn...)
|
||||
}
|
||||
|
||||
return dArgs, nil
|
||||
return tableSet, nil
|
||||
}
|
||||
|
||||
// applyDiffRoots applies the appropriate |from| and |to| root values to the receiver and returns the table names
|
||||
@@ -290,10 +315,13 @@ func (dArgs *diffArgs) applyDiffRoots(ctx context.Context, dEnv *env.DoltEnv, ar
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dArgs.fromRoot = stagedRoot
|
||||
dArgs.fromRef = doltdb.Staged
|
||||
dArgs.toRoot = workingRoot
|
||||
dArgs.toRef = doltdb.Working
|
||||
dArgs.diffDatasets = &diffDatasets{
|
||||
fromRoot: stagedRoot,
|
||||
fromRef: doltdb.Staged,
|
||||
toRoot: workingRoot,
|
||||
toRef: doltdb.Working,
|
||||
}
|
||||
|
||||
if isCached {
|
||||
dArgs.fromRoot = headRoot
|
||||
dArgs.fromRef = "HEAD"
|
||||
|
||||
@@ -276,6 +276,54 @@ func getCommitSpec(commit string) (*doltdb.CommitSpec, error) {
|
||||
return cs, nil
|
||||
}
|
||||
|
||||
func getHashToRefs(ctx context.Context, dEnv *env.DoltEnv, decorationLevel string) (map[hash.Hash][]string, error) {
|
||||
cHashToRefs := map[hash.Hash][]string{}
|
||||
|
||||
// Get all branches
|
||||
branches, err := dEnv.DoltDB.GetBranchesWithHashes(ctx)
|
||||
if err != nil {
|
||||
return cHashToRefs, fmt.Errorf(color.HiRedString("Fatal error: cannot get Branch information."))
|
||||
}
|
||||
|
||||
for _, b := range branches {
|
||||
refName := b.Ref.String()
|
||||
if decorationLevel != "full" {
|
||||
refName = b.Ref.GetPath() // trim out "refs/heads/"
|
||||
}
|
||||
refName = fmt.Sprintf("\033[32;1m%s\033[0m", refName) // branch names are bright green (32;1m)
|
||||
cHashToRefs[b.Hash] = append(cHashToRefs[b.Hash], refName)
|
||||
}
|
||||
|
||||
// Get all remote branches
|
||||
remotes, err := dEnv.DoltDB.GetRemotesWithHashes(ctx)
|
||||
if err != nil {
|
||||
return cHashToRefs, fmt.Errorf(color.HiRedString("Fatal error: cannot get Remotes information."))
|
||||
}
|
||||
for _, r := range remotes {
|
||||
refName := r.Ref.String()
|
||||
if decorationLevel != "full" {
|
||||
refName = r.Ref.GetPath() // trim out "refs/remotes/"
|
||||
}
|
||||
refName = fmt.Sprintf("\033[31;1m%s\033[0m", refName) // remote names are bright red (31;1m)
|
||||
cHashToRefs[r.Hash] = append(cHashToRefs[r.Hash], refName)
|
||||
}
|
||||
|
||||
// Get all tags
|
||||
tags, err := dEnv.DoltDB.GetTagsWithHashes(ctx)
|
||||
if err != nil {
|
||||
return cHashToRefs, fmt.Errorf(color.HiRedString("Fatal error: cannot get Tag information."))
|
||||
}
|
||||
for _, t := range tags {
|
||||
tagName := t.Tag.GetDoltRef().String()
|
||||
if decorationLevel != "full" {
|
||||
tagName = t.Tag.Name // trim out "refs/tags/"
|
||||
}
|
||||
tagName = fmt.Sprintf("\033[33;1mtag: %s\033[0m", tagName) // tags names are bright yellow (33;1m)
|
||||
cHashToRefs[t.Hash] = append(cHashToRefs[t.Hash], tagName)
|
||||
}
|
||||
return cHashToRefs, nil
|
||||
}
|
||||
|
||||
func logCommits(ctx context.Context, dEnv *env.DoltEnv, opts *logOpts) int {
|
||||
hashes := make([]hash.Hash, len(opts.commitSpecs))
|
||||
|
||||
@@ -295,51 +343,10 @@ func logCommits(ctx context.Context, dEnv *env.DoltEnv, opts *logOpts) int {
|
||||
hashes[i] = h
|
||||
}
|
||||
|
||||
cHashToRefs := map[hash.Hash][]string{}
|
||||
cHashToRefs, err := getHashToRefs(ctx, dEnv, opts.decoration)
|
||||
|
||||
// Get all branches
|
||||
branches, err := dEnv.DoltDB.GetBranchesWithHashes(ctx)
|
||||
if err != nil {
|
||||
cli.PrintErrln(color.HiRedString("Fatal error: cannot get Branch information."))
|
||||
return 1
|
||||
}
|
||||
for _, b := range branches {
|
||||
refName := b.Ref.String()
|
||||
if opts.decoration != "full" {
|
||||
refName = b.Ref.GetPath() // trim out "refs/heads/"
|
||||
}
|
||||
refName = fmt.Sprintf("\033[32;1m%s\033[0m", refName) // branch names are bright green (32;1m)
|
||||
cHashToRefs[b.Hash] = append(cHashToRefs[b.Hash], refName)
|
||||
}
|
||||
|
||||
// Get all remote branches
|
||||
remotes, err := dEnv.DoltDB.GetRemotesWithHashes(ctx)
|
||||
if err != nil {
|
||||
cli.PrintErrln(color.HiRedString("Fatal error: cannot get Remotes information."))
|
||||
return 1
|
||||
}
|
||||
for _, r := range remotes {
|
||||
refName := r.Ref.String()
|
||||
if opts.decoration != "full" {
|
||||
refName = r.Ref.GetPath() // trim out "refs/remotes/"
|
||||
}
|
||||
refName = fmt.Sprintf("\033[31;1m%s\033[0m", refName) // remote names are bright red (31;1m)
|
||||
cHashToRefs[r.Hash] = append(cHashToRefs[r.Hash], refName)
|
||||
}
|
||||
|
||||
// Get all tags
|
||||
tags, err := dEnv.DoltDB.GetTagsWithHashes(ctx)
|
||||
if err != nil {
|
||||
cli.PrintErrln(color.HiRedString("Fatal error: cannot get Tag information."))
|
||||
return 1
|
||||
}
|
||||
for _, t := range tags {
|
||||
tagName := t.Tag.GetDoltRef().String()
|
||||
if opts.decoration != "full" {
|
||||
tagName = t.Tag.Name // trim out "refs/tags/"
|
||||
}
|
||||
tagName = fmt.Sprintf("\033[33;1mtag: %s\033[0m", tagName) // tags names are bright yellow (33;1m)
|
||||
cHashToRefs[t.Hash] = append(cHashToRefs[t.Hash], tagName)
|
||||
return handleErrAndExit(err)
|
||||
}
|
||||
|
||||
matchFunc := func(c *doltdb.Commit) (bool, error) {
|
||||
@@ -379,6 +386,11 @@ func logCommits(ctx context.Context, dEnv *env.DoltEnv, opts *logOpts) int {
|
||||
headRef := dEnv.RepoStateReader().CWBHeadRef()
|
||||
cwbHash, err := dEnv.DoltDB.GetHashForRefStr(ctx, headRef.String())
|
||||
|
||||
if err != nil {
|
||||
cli.PrintErrln(err)
|
||||
return 1
|
||||
}
|
||||
|
||||
var commitsInfo []logNode
|
||||
for _, comm := range commits {
|
||||
meta, mErr := comm.GetCommitMeta(ctx)
|
||||
@@ -569,41 +581,45 @@ func logCompact(pager *outputpager.Pager, opts *logOpts, commits []logNode) {
|
||||
}
|
||||
}
|
||||
|
||||
func PrintCommit(pager *outputpager.Pager, minParents int, showParents bool, decoration string, comm logNode) {
|
||||
if len(comm.parentHashes) < minParents {
|
||||
return
|
||||
}
|
||||
|
||||
chStr := comm.commitHash.String()
|
||||
if showParents {
|
||||
for _, h := range comm.parentHashes {
|
||||
chStr += " " + h.String()
|
||||
}
|
||||
}
|
||||
|
||||
// Write commit hash
|
||||
pager.Writer.Write([]byte(fmt.Sprintf("\033[33mcommit %s \033[0m", chStr))) // Use Dim Yellow (33m)
|
||||
|
||||
// Show decoration
|
||||
if decoration != "no" {
|
||||
logRefs(pager, comm)
|
||||
}
|
||||
|
||||
if len(comm.parentHashes) > 1 {
|
||||
pager.Writer.Write([]byte(fmt.Sprintf("\nMerge:")))
|
||||
for _, h := range comm.parentHashes {
|
||||
pager.Writer.Write([]byte(fmt.Sprintf(" " + h.String())))
|
||||
}
|
||||
}
|
||||
|
||||
pager.Writer.Write([]byte(fmt.Sprintf("\nAuthor: %s <%s>", comm.commitMeta.Name, comm.commitMeta.Email)))
|
||||
|
||||
timeStr := comm.commitMeta.FormatTS()
|
||||
pager.Writer.Write([]byte(fmt.Sprintf("\nDate: %s", timeStr)))
|
||||
|
||||
formattedDesc := "\n\n\t" + strings.Replace(comm.commitMeta.Description, "\n", "\n\t", -1) + "\n\n"
|
||||
pager.Writer.Write([]byte(fmt.Sprintf("%s", formattedDesc)))
|
||||
}
|
||||
|
||||
func logDefault(pager *outputpager.Pager, opts *logOpts, commits []logNode) {
|
||||
for _, comm := range commits {
|
||||
if len(comm.parentHashes) < opts.minParents {
|
||||
return
|
||||
}
|
||||
|
||||
chStr := comm.commitHash.String()
|
||||
if opts.showParents {
|
||||
for _, h := range comm.parentHashes {
|
||||
chStr += " " + h.String()
|
||||
}
|
||||
}
|
||||
|
||||
// Write commit hash
|
||||
pager.Writer.Write([]byte(fmt.Sprintf("\033[33mcommit %s \033[0m", chStr))) // Use Dim Yellow (33m)
|
||||
|
||||
// Show decoration
|
||||
if opts.decoration != "no" {
|
||||
logRefs(pager, comm)
|
||||
}
|
||||
|
||||
if len(comm.parentHashes) > 1 {
|
||||
pager.Writer.Write([]byte(fmt.Sprintf("\nMerge:")))
|
||||
for _, h := range comm.parentHashes {
|
||||
pager.Writer.Write([]byte(fmt.Sprintf(" " + h.String())))
|
||||
}
|
||||
}
|
||||
|
||||
pager.Writer.Write([]byte(fmt.Sprintf("\nAuthor: %s <%s>", comm.commitMeta.Name, comm.commitMeta.Email)))
|
||||
|
||||
timeStr := comm.commitMeta.FormatTS()
|
||||
pager.Writer.Write([]byte(fmt.Sprintf("\nDate: %s", timeStr)))
|
||||
|
||||
formattedDesc := "\n\n\t" + strings.Replace(comm.commitMeta.Description, "\n", "\n\t", -1) + "\n\n"
|
||||
pager.Writer.Write([]byte(fmt.Sprintf("%s", formattedDesc)))
|
||||
PrintCommit(pager, opts.minParents, opts.showParents, opts.decoration, comm)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
262
go/cmd/dolt/commands/show.go
Normal file
262
go/cmd/dolt/commands/show.go
Normal file
@@ -0,0 +1,262 @@
|
||||
// Copyright 2023 Dolthub, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
|
||||
"github.com/dolthub/dolt/go/cmd/dolt/cli"
|
||||
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
|
||||
eventsapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi/v1alpha1"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
|
||||
"github.com/dolthub/dolt/go/libraries/doltcore/env"
|
||||
"github.com/dolthub/dolt/go/libraries/utils/argparser"
|
||||
"github.com/dolthub/dolt/go/store/util/outputpager"
|
||||
)
|
||||
|
||||
type showOpts struct {
|
||||
showParents bool
|
||||
decoration string
|
||||
specRefs []string
|
||||
|
||||
*diffDisplaySettings
|
||||
}
|
||||
|
||||
var showDocs = cli.CommandDocumentationContent{
|
||||
ShortDesc: `Show information about a specific commit`,
|
||||
LongDesc: `Show information about a specific commit`,
|
||||
Synopsis: []string{
|
||||
`[{{.LessThan}}revision{{.GreaterThan}}]`,
|
||||
},
|
||||
}
|
||||
|
||||
type ShowCmd struct{}
|
||||
|
||||
// Name returns the name of the Dolt cli command. This is what is used on the command line to invoke the command
|
||||
func (cmd ShowCmd) Name() string {
|
||||
return "show"
|
||||
}
|
||||
|
||||
// Description returns a description of the command
|
||||
func (cmd ShowCmd) Description() string {
|
||||
return "Show information about a specific commit."
|
||||
}
|
||||
|
||||
// EventType returns the type of the event to log
|
||||
func (cmd ShowCmd) EventType() eventsapi.ClientEventType {
|
||||
return eventsapi.ClientEventType_SHOW
|
||||
}
|
||||
|
||||
func (cmd ShowCmd) Docs() *cli.CommandDocumentation {
|
||||
ap := cmd.ArgParser()
|
||||
return cli.NewCommandDocumentation(showDocs, ap)
|
||||
}
|
||||
|
||||
func (cmd ShowCmd) ArgParser() *argparser.ArgParser {
|
||||
ap := argparser.NewArgParser()
|
||||
// Flags inherited from Log
|
||||
ap.SupportsFlag(cli.ParentsFlag, "", "Shows all parents of each commit in the log.")
|
||||
ap.SupportsString(cli.DecorateFlag, "", "decorate_fmt", "Shows refs next to commits. Valid options are short, full, no, and auto")
|
||||
|
||||
// Flags inherited from Diff
|
||||
ap.SupportsFlag(DataFlag, "d", "Show only the data changes, do not show the schema changes (Both shown by default).")
|
||||
ap.SupportsFlag(SchemaFlag, "s", "Show only the schema changes, do not show the data changes (Both shown by default).")
|
||||
ap.SupportsFlag(StatFlag, "", "Show stats of data changes")
|
||||
ap.SupportsFlag(SummaryFlag, "", "Show summary of data and schema changes")
|
||||
ap.SupportsString(FormatFlag, "r", "result output format", "How to format diff output. Valid values are tabular, sql, json. Defaults to tabular.")
|
||||
ap.SupportsString(whereParam, "", "column", "filters columns based on values in the diff. See {{.EmphasisLeft}}dolt diff --help{{.EmphasisRight}} for details.")
|
||||
ap.SupportsInt(limitParam, "", "record_count", "limits to the first N diffs.")
|
||||
ap.SupportsFlag(cli.CachedFlag, "c", "Show only the staged data changes.")
|
||||
ap.SupportsFlag(SkinnyFlag, "sk", "Shows only primary key columns and any columns with data changes.")
|
||||
ap.SupportsFlag(MergeBase, "", "Uses merge base of the first commit and second commit (or HEAD if not supplied) as the first commit")
|
||||
ap.SupportsString(DiffMode, "", "diff mode", "Determines how to display modified rows with tabular output. Valid values are row, line, in-place, context. Defaults to context.")
|
||||
return ap
|
||||
}
|
||||
|
||||
// Exec executes the command
|
||||
func (cmd ShowCmd) Exec(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEnv) int {
|
||||
ap := cmd.ArgParser()
|
||||
help, usage := cli.HelpAndUsagePrinters(cli.CommandDocsForCommandString(commandStr, showDocs, ap))
|
||||
apr := cli.ParseArgsOrDie(ap, args, help)
|
||||
|
||||
opts, err := parseShowArgs(ctx, dEnv, apr)
|
||||
if err != nil {
|
||||
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
opts.diffDisplaySettings = parseDiffDisplaySettings(ctx, dEnv, apr)
|
||||
|
||||
err = showCommits(ctx, dEnv, opts)
|
||||
|
||||
return handleErrAndExit(err)
|
||||
}
|
||||
|
||||
func (cmd ShowCmd) validateArgs(apr *argparser.ArgParseResults) errhand.VerboseError {
|
||||
if apr.Contains(StatFlag) || apr.Contains(SummaryFlag) {
|
||||
if apr.Contains(SchemaFlag) || apr.Contains(DataFlag) {
|
||||
return errhand.BuildDError("invalid Arguments: --stat and --summary cannot be combined with --schema or --data").Build()
|
||||
}
|
||||
}
|
||||
|
||||
f, _ := apr.GetValue(FormatFlag)
|
||||
switch strings.ToLower(f) {
|
||||
case "tabular", "sql", "json", "":
|
||||
default:
|
||||
return errhand.BuildDError("invalid output format: %s", f).Build()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseShowArgs(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgParseResults) (*showOpts, error) {
|
||||
|
||||
decorateOption := apr.GetValueOrDefault(cli.DecorateFlag, "auto")
|
||||
switch decorateOption {
|
||||
case "short", "full", "auto", "no":
|
||||
default:
|
||||
return nil, fmt.Errorf("fatal: invalid --decorate option: %s", decorateOption)
|
||||
}
|
||||
|
||||
return &showOpts{
|
||||
showParents: apr.Contains(cli.ParentsFlag),
|
||||
decoration: decorateOption,
|
||||
specRefs: apr.Args,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func showCommits(ctx context.Context, dEnv *env.DoltEnv, opts *showOpts) error {
|
||||
if len(opts.specRefs) == 0 {
|
||||
return showCommit(ctx, dEnv, opts, dEnv.RepoStateReader().CWBHeadSpec())
|
||||
}
|
||||
|
||||
for _, specRef := range opts.specRefs {
|
||||
commitSpec, err := getCommitSpec(specRef)
|
||||
|
||||
if err != nil {
|
||||
cli.PrintErrln(color.HiRedString("Fatal error: invalid commit spec %s", specRef))
|
||||
return err
|
||||
}
|
||||
|
||||
err = showCommit(ctx, dEnv, opts, commitSpec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func showCommit(ctx context.Context, dEnv *env.DoltEnv, opts *showOpts, commitSpec *doltdb.CommitSpec) error {
|
||||
|
||||
comm, err := dEnv.DoltDB.Resolve(ctx, commitSpec, dEnv.RepoStateReader().CWBHeadRef())
|
||||
if err != nil {
|
||||
cli.PrintErrln(color.HiRedString("Fatal error: cannot resolve commit spec."))
|
||||
return err
|
||||
}
|
||||
|
||||
cHashToRefs, err := getHashToRefs(ctx, dEnv, opts.decoration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
meta, mErr := comm.GetCommitMeta(ctx)
|
||||
if mErr != nil {
|
||||
cli.PrintErrln("error: failed to get commit metadata")
|
||||
return err
|
||||
}
|
||||
pHashes, pErr := comm.ParentHashes(ctx)
|
||||
if pErr != nil {
|
||||
cli.PrintErrln("error: failed to get parent hashes")
|
||||
return err
|
||||
}
|
||||
cmHash, cErr := comm.HashOf()
|
||||
if cErr != nil {
|
||||
cli.PrintErrln("error: failed to get commit hash")
|
||||
return err
|
||||
}
|
||||
|
||||
headRef := dEnv.RepoStateReader().CWBHeadRef()
|
||||
cwbHash, err := dEnv.DoltDB.GetHashForRefStr(ctx, headRef.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cli.ExecuteWithStdioRestored(func() {
|
||||
pager := outputpager.Start()
|
||||
defer pager.Stop()
|
||||
|
||||
PrintCommit(pager, 0, opts.showParents, opts.decoration, logNode{
|
||||
commitMeta: meta,
|
||||
commitHash: cmHash,
|
||||
parentHashes: pHashes,
|
||||
branchNames: cHashToRefs[cmHash],
|
||||
isHead: cmHash == *cwbHash})
|
||||
})
|
||||
|
||||
if comm.NumParents() == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if comm.NumParents() > 1 {
|
||||
return fmt.Errorf("Requested commit is a merge commit. 'dolt show' currently only supports viewing non-merge commits.")
|
||||
}
|
||||
|
||||
commitRoot, err := comm.GetRootValue(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parent, err := comm.GetParent(ctx, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parentRoot, err := parent.GetRootValue(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parentHash, err := parent.HashOf()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
datasets := &diffDatasets{
|
||||
fromRoot: parentRoot,
|
||||
toRoot: commitRoot,
|
||||
fromRef: parentHash.String(),
|
||||
toRef: cmHash.String(),
|
||||
}
|
||||
|
||||
// An empty string will cause all tables to be printed.
|
||||
var tableNames []string
|
||||
|
||||
tableSet, err := parseDiffTableSet(ctx, dEnv, datasets, tableNames)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dArgs := &diffArgs{
|
||||
diffDisplaySettings: opts.diffDisplaySettings,
|
||||
diffDatasets: datasets,
|
||||
tableSet: tableSet,
|
||||
}
|
||||
|
||||
return diffUserTables(ctx, dEnv, dArgs)
|
||||
}
|
||||
@@ -75,6 +75,7 @@ var doltCommand = cli.NewSubCommandHandler("dolt", "it's git for data", []cli.Co
|
||||
sqlserver.SqlServerCmd{VersionStr: Version},
|
||||
sqlserver.SqlClientCmd{VersionStr: Version},
|
||||
commands.LogCmd{},
|
||||
commands.ShowCmd{},
|
||||
commands.BranchCmd{},
|
||||
commands.CheckoutCmd{},
|
||||
commands.MergeCmd{},
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.28.1
|
||||
// protoc v4.22.0
|
||||
// protoc-gen-go v1.29.0
|
||||
// protoc v4.22.2
|
||||
// source: dolt/services/eventsapi/v1alpha1/event_constants.proto
|
||||
|
||||
package eventsapi
|
||||
@@ -152,6 +152,7 @@ const (
|
||||
ClientEventType_STASH_DROP ClientEventType = 58
|
||||
ClientEventType_STASH_LIST ClientEventType = 59
|
||||
ClientEventType_STASH_POP ClientEventType = 60
|
||||
ClientEventType_SHOW ClientEventType = 61
|
||||
)
|
||||
|
||||
// Enum value maps for ClientEventType.
|
||||
@@ -218,6 +219,7 @@ var (
|
||||
58: "STASH_DROP",
|
||||
59: "STASH_LIST",
|
||||
60: "STASH_POP",
|
||||
61: "SHOW",
|
||||
}
|
||||
ClientEventType_value = map[string]int32{
|
||||
"TYPE_UNSPECIFIED": 0,
|
||||
@@ -281,6 +283,7 @@ var (
|
||||
"STASH_DROP": 58,
|
||||
"STASH_LIST": 59,
|
||||
"STASH_POP": 60,
|
||||
"SHOW": 61,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -468,7 +471,7 @@ var file_dolt_services_eventsapi_v1alpha1_event_constants_proto_rawDesc = []byte
|
||||
0x52, 0x4d, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00,
|
||||
0x12, 0x09, 0x0a, 0x05, 0x4c, 0x49, 0x4e, 0x55, 0x58, 0x10, 0x01, 0x12, 0x0b, 0x0a, 0x07, 0x57,
|
||||
0x49, 0x4e, 0x44, 0x4f, 0x57, 0x53, 0x10, 0x02, 0x12, 0x0a, 0x0a, 0x06, 0x44, 0x41, 0x52, 0x57,
|
||||
0x49, 0x4e, 0x10, 0x03, 0x2a, 0xe6, 0x07, 0x0a, 0x0f, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x45,
|
||||
0x49, 0x4e, 0x10, 0x03, 0x2a, 0xf0, 0x07, 0x0a, 0x0f, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x45,
|
||||
0x76, 0x65, 0x6e, 0x74, 0x54, 0x79, 0x70, 0x65, 0x12, 0x14, 0x0a, 0x10, 0x54, 0x59, 0x50, 0x45,
|
||||
0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x08,
|
||||
0x0a, 0x04, 0x49, 0x4e, 0x49, 0x54, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x53, 0x54, 0x41, 0x54,
|
||||
@@ -530,27 +533,28 @@ var file_dolt_services_eventsapi_v1alpha1_event_constants_proto_rawDesc = []byte
|
||||
0x0b, 0x53, 0x54, 0x41, 0x53, 0x48, 0x5f, 0x43, 0x4c, 0x45, 0x41, 0x52, 0x10, 0x39, 0x12, 0x0e,
|
||||
0x0a, 0x0a, 0x53, 0x54, 0x41, 0x53, 0x48, 0x5f, 0x44, 0x52, 0x4f, 0x50, 0x10, 0x3a, 0x12, 0x0e,
|
||||
0x0a, 0x0a, 0x53, 0x54, 0x41, 0x53, 0x48, 0x5f, 0x4c, 0x49, 0x53, 0x54, 0x10, 0x3b, 0x12, 0x0d,
|
||||
0x0a, 0x09, 0x53, 0x54, 0x41, 0x53, 0x48, 0x5f, 0x50, 0x4f, 0x50, 0x10, 0x3c, 0x2a, 0x6a, 0x0a,
|
||||
0x08, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x49, 0x44, 0x12, 0x16, 0x0a, 0x12, 0x4d, 0x45, 0x54,
|
||||
0x52, 0x49, 0x43, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10,
|
||||
0x00, 0x12, 0x14, 0x0a, 0x10, 0x42, 0x59, 0x54, 0x45, 0x53, 0x5f, 0x44, 0x4f, 0x57, 0x4e, 0x4c,
|
||||
0x4f, 0x41, 0x44, 0x45, 0x44, 0x10, 0x01, 0x12, 0x17, 0x0a, 0x13, 0x44, 0x4f, 0x57, 0x4e, 0x4c,
|
||||
0x4f, 0x41, 0x44, 0x5f, 0x4d, 0x53, 0x5f, 0x45, 0x4c, 0x41, 0x50, 0x53, 0x45, 0x44, 0x10, 0x02,
|
||||
0x12, 0x17, 0x0a, 0x13, 0x52, 0x45, 0x4d, 0x4f, 0x54, 0x45, 0x41, 0x50, 0x49, 0x5f, 0x52, 0x50,
|
||||
0x43, 0x5f, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x10, 0x03, 0x2a, 0x45, 0x0a, 0x0b, 0x41, 0x74, 0x74,
|
||||
0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x49, 0x44, 0x12, 0x19, 0x0a, 0x15, 0x41, 0x54, 0x54, 0x52,
|
||||
0x49, 0x42, 0x55, 0x54, 0x45, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45,
|
||||
0x44, 0x10, 0x00, 0x12, 0x15, 0x0a, 0x11, 0x52, 0x45, 0x4d, 0x4f, 0x54, 0x45, 0x5f, 0x55, 0x52,
|
||||
0x4c, 0x5f, 0x53, 0x43, 0x48, 0x45, 0x4d, 0x45, 0x10, 0x02, 0x22, 0x04, 0x08, 0x01, 0x10, 0x01,
|
||||
0x2a, 0x2d, 0x0a, 0x05, 0x41, 0x70, 0x70, 0x49, 0x44, 0x12, 0x16, 0x0a, 0x12, 0x41, 0x50, 0x50,
|
||||
0x5f, 0x49, 0x44, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10,
|
||||
0x00, 0x12, 0x0c, 0x0a, 0x08, 0x41, 0x50, 0x50, 0x5f, 0x44, 0x4f, 0x4c, 0x54, 0x10, 0x01, 0x42,
|
||||
0x51, 0x5a, 0x4f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x64, 0x6f,
|
||||
0x6c, 0x74, 0x68, 0x75, 0x62, 0x2f, 0x64, 0x6f, 0x6c, 0x74, 0x2f, 0x67, 0x6f, 0x2f, 0x67, 0x65,
|
||||
0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x64, 0x6f, 0x6c, 0x74, 0x2f, 0x73, 0x65, 0x72,
|
||||
0x76, 0x69, 0x63, 0x65, 0x73, 0x2f, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x61, 0x70, 0x69, 0x2f,
|
||||
0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x3b, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x61,
|
||||
0x70, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x0a, 0x09, 0x53, 0x54, 0x41, 0x53, 0x48, 0x5f, 0x50, 0x4f, 0x50, 0x10, 0x3c, 0x12, 0x08, 0x0a,
|
||||
0x04, 0x53, 0x48, 0x4f, 0x57, 0x10, 0x3d, 0x2a, 0x6a, 0x0a, 0x08, 0x4d, 0x65, 0x74, 0x72, 0x69,
|
||||
0x63, 0x49, 0x44, 0x12, 0x16, 0x0a, 0x12, 0x4d, 0x45, 0x54, 0x52, 0x49, 0x43, 0x5f, 0x55, 0x4e,
|
||||
0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x14, 0x0a, 0x10, 0x42,
|
||||
0x59, 0x54, 0x45, 0x53, 0x5f, 0x44, 0x4f, 0x57, 0x4e, 0x4c, 0x4f, 0x41, 0x44, 0x45, 0x44, 0x10,
|
||||
0x01, 0x12, 0x17, 0x0a, 0x13, 0x44, 0x4f, 0x57, 0x4e, 0x4c, 0x4f, 0x41, 0x44, 0x5f, 0x4d, 0x53,
|
||||
0x5f, 0x45, 0x4c, 0x41, 0x50, 0x53, 0x45, 0x44, 0x10, 0x02, 0x12, 0x17, 0x0a, 0x13, 0x52, 0x45,
|
||||
0x4d, 0x4f, 0x54, 0x45, 0x41, 0x50, 0x49, 0x5f, 0x52, 0x50, 0x43, 0x5f, 0x45, 0x52, 0x52, 0x4f,
|
||||
0x52, 0x10, 0x03, 0x2a, 0x45, 0x0a, 0x0b, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65,
|
||||
0x49, 0x44, 0x12, 0x19, 0x0a, 0x15, 0x41, 0x54, 0x54, 0x52, 0x49, 0x42, 0x55, 0x54, 0x45, 0x5f,
|
||||
0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x15, 0x0a,
|
||||
0x11, 0x52, 0x45, 0x4d, 0x4f, 0x54, 0x45, 0x5f, 0x55, 0x52, 0x4c, 0x5f, 0x53, 0x43, 0x48, 0x45,
|
||||
0x4d, 0x45, 0x10, 0x02, 0x22, 0x04, 0x08, 0x01, 0x10, 0x01, 0x2a, 0x2d, 0x0a, 0x05, 0x41, 0x70,
|
||||
0x70, 0x49, 0x44, 0x12, 0x16, 0x0a, 0x12, 0x41, 0x50, 0x50, 0x5f, 0x49, 0x44, 0x5f, 0x55, 0x4e,
|
||||
0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x41,
|
||||
0x50, 0x50, 0x5f, 0x44, 0x4f, 0x4c, 0x54, 0x10, 0x01, 0x42, 0x51, 0x5a, 0x4f, 0x67, 0x69, 0x74,
|
||||
0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x64, 0x6f, 0x6c, 0x74, 0x68, 0x75, 0x62, 0x2f,
|
||||
0x64, 0x6f, 0x6c, 0x74, 0x2f, 0x67, 0x6f, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x2f, 0x64, 0x6f, 0x6c, 0x74, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2f,
|
||||
0x65, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68,
|
||||
0x61, 0x31, 0x3b, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
86
integration-tests/bats/show.bats
Normal file
86
integration-tests/bats/show.bats
Normal file
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env bats
|
||||
load $BATS_TEST_DIRNAME/helper/common.bash
|
||||
|
||||
setup() {
|
||||
setup_common
|
||||
}
|
||||
|
||||
teardown() {
|
||||
assert_feature_version
|
||||
teardown_common
|
||||
}
|
||||
|
||||
@test "show: on initialized repo" {
|
||||
run dolt show
|
||||
[ "$status" -eq "0" ]
|
||||
[[ "$output" =~ "Initialize data repository" ]] || false
|
||||
}
|
||||
|
||||
@test "show: log zero refs" {
|
||||
dolt commit --allow-empty -m "Commit One"
|
||||
dolt tag v1
|
||||
run dolt show
|
||||
[ $status -eq 0 ]
|
||||
[[ "$output" =~ "Commit One" ]] || false
|
||||
[[ "$output" =~ "tag: v1" ]] || false
|
||||
|
||||
dolt commit --allow-empty -m "Commit Two"
|
||||
dolt tag v2
|
||||
run dolt show
|
||||
[ $status -eq 0 ]
|
||||
[[ "$output" =~ "Commit Two" ]] || false
|
||||
[[ "$output" =~ "tag: v2" ]] || false
|
||||
}
|
||||
|
||||
@test "show: log one ref" {
|
||||
dolt commit --allow-empty -m "Commit One"
|
||||
dolt tag v1
|
||||
|
||||
dolt commit --allow-empty -m "Commit Two"
|
||||
dolt tag v2
|
||||
|
||||
run dolt show v1
|
||||
[ $status -eq 0 ]
|
||||
[[ "$output" =~ "Commit One" ]] || false
|
||||
[[ "$output" =~ "tag: v1" ]] || false
|
||||
}
|
||||
|
||||
@test "show: log two refs" {
|
||||
dolt commit --allow-empty -m "Commit One"
|
||||
dolt tag v1
|
||||
|
||||
dolt commit --allow-empty -m "Commit Two"
|
||||
dolt tag v2
|
||||
|
||||
run dolt show v1 v2
|
||||
[ $status -eq 0 ]
|
||||
[[ "$output" =~ "Commit One" ]] || false
|
||||
[[ "$output" =~ "tag: v1" ]] || false
|
||||
[[ "$output" =~ "Commit Two" ]] || false
|
||||
[[ "$output" =~ "tag: v2" ]] || false
|
||||
}
|
||||
|
||||
@test "show: log and diff" {
|
||||
dolt sql -q "create table testtable (pk int PRIMARY KEY)"
|
||||
dolt add .
|
||||
dolt commit -m "commit: add table"
|
||||
|
||||
run dolt show
|
||||
[ $status -eq 0 ]
|
||||
[[ "$output" =~ "commit: add table" ]] || false
|
||||
[[ "$output" =~ "diff --dolt a/testtable b/testtable" ]] || false
|
||||
[[ "$output" =~ "added table" ]] || false
|
||||
[[ "$output" =~ "+CREATE TABLE \`testtable\` (" ]] || false
|
||||
[[ "$output" =~ "+ \`pk\` int NOT NULL," ]] || false
|
||||
[[ "$output" =~ "+ PRIMARY KEY (\`pk\`)" ]] || false
|
||||
|
||||
dolt sql -q 'insert into testtable values (4)'
|
||||
dolt add .
|
||||
dolt commit -m "commit: add values"
|
||||
|
||||
run dolt show
|
||||
[ $status -eq 0 ]
|
||||
[[ "$output" =~ "commit: add values" ]] || false
|
||||
[[ "$output" =~ "| | pk |" ]] || false
|
||||
[[ "$output" =~ "| + | 4 |" ]] || false
|
||||
}
|
||||
@@ -90,6 +90,7 @@ enum ClientEventType {
|
||||
STASH_DROP = 58;
|
||||
STASH_LIST = 59;
|
||||
STASH_POP = 60;
|
||||
SHOW = 61;
|
||||
}
|
||||
|
||||
enum MetricID {
|
||||
|
||||
Reference in New Issue
Block a user