|
| 1 | +// Copyright 2025 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 | +package createchunk |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + |
| 21 | + "github.com/dolthub/dolt/go/cmd/dolt/cli" |
| 22 | + "github.com/dolthub/dolt/go/cmd/dolt/errhand" |
| 23 | + "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" |
| 24 | + "github.com/dolthub/dolt/go/libraries/doltcore/env" |
| 25 | + "github.com/dolthub/dolt/go/libraries/doltcore/ref" |
| 26 | + "github.com/dolthub/dolt/go/libraries/utils/argparser" |
| 27 | + "github.com/dolthub/dolt/go/store/datas" |
| 28 | + "github.com/dolthub/dolt/go/store/hash" |
| 29 | +) |
| 30 | + |
| 31 | +// CreateCommitCmd creates a new commit chunk, printing the new chunk's hash on success. |
| 32 | +// The user must supply a branch name, which will be set to this new commit. |
| 33 | +// This is only required for the CLI command, and is optional when invoking the equivalent stored procedure. |
| 34 | +// This is because the journal must end with a root hash, and is only flushed when there is a new root hash. |
| 35 | +// Thus, we must update the root hash before the command finishes, or else changes will not be persisted. |
| 36 | +type CreateCommitCmd struct{} |
| 37 | + |
| 38 | +// Name is returns the name of the Dolt cli command. This is what is used on the command line to invoke the command |
| 39 | +func (cmd CreateCommitCmd) Name() string { |
| 40 | + return "commit" |
| 41 | +} |
| 42 | + |
| 43 | +// Description returns a description of the command |
| 44 | +func (cmd CreateCommitCmd) Description() string { |
| 45 | + return "Creates a new commit chunk in the dolt storage" |
| 46 | +} |
| 47 | + |
| 48 | +// RequiresRepo should return false if this interface is implemented, and the command does not have the requirement |
| 49 | +// that it be run from within a data repository directory |
| 50 | +func (cmd CreateCommitCmd) RequiresRepo() bool { |
| 51 | + return true |
| 52 | +} |
| 53 | + |
| 54 | +func (cmd CreateCommitCmd) Docs() *cli.CommandDocumentation { |
| 55 | + // Admin commands are undocumented |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +func (cmd CreateCommitCmd) ArgParser() *argparser.ArgParser { |
| 60 | + return cli.CreateCreateCommitParser() |
| 61 | +} |
| 62 | + |
| 63 | +func (cmd CreateCommitCmd) Exec(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEnv, cliCtx cli.CliContext) int { |
| 64 | + ap := cmd.ArgParser() |
| 65 | + usage, _ := cli.HelpAndUsagePrinters(cli.CommandDocsForCommandString(commandStr, cli.CommandDocumentationContent{}, ap)) |
| 66 | + |
| 67 | + // Ensure that the CLI args parse, but only check that a branch was supplied. |
| 68 | + // All other args will be validated in the system procedure, but the branch is only required in the CLI. |
| 69 | + apr := cli.ParseArgsOrDie(ap, args, usage) |
| 70 | + if !apr.Contains(cli.BranchParam) { |
| 71 | + cli.PrintErrf("the --%s flag is required when creating a chunk using the CLI", cli.BranchParam) |
| 72 | + return 1 |
| 73 | + } |
| 74 | + |
| 75 | + desc, _ := apr.GetValue("desc") |
| 76 | + root, _ := apr.GetValue("root") |
| 77 | + parents, _ := apr.GetValueList("parents") |
| 78 | + branch, isBranchSet := apr.GetValue(cli.BranchParam) |
| 79 | + force := apr.Contains(cli.ForceFlag) |
| 80 | + |
| 81 | + var name, email string |
| 82 | + var err error |
| 83 | + if authorStr, ok := apr.GetValue(cli.AuthorParam); ok { |
| 84 | + name, email, err = cli.ParseAuthor(authorStr) |
| 85 | + if err != nil { |
| 86 | + cli.PrintErrln(errhand.VerboseErrorFromError(err)) |
| 87 | + return 1 |
| 88 | + } |
| 89 | + } else { |
| 90 | + name, email, err = env.GetNameAndEmail(cliCtx.Config()) |
| 91 | + if err != nil { |
| 92 | + cli.PrintErrln(errhand.VerboseErrorFromError(err)) |
| 93 | + return 1 |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + db := dEnv.DbData(ctx).Ddb |
| 98 | + commitRootHash, ok := hash.MaybeParse(root) |
| 99 | + if !ok { |
| 100 | + cli.PrintErrf("invalid root value hash") |
| 101 | + return 1 |
| 102 | + } |
| 103 | + |
| 104 | + var parentCommits []hash.Hash |
| 105 | + for _, parent := range parents { |
| 106 | + commitSpec, err := doltdb.NewCommitSpec(parent) |
| 107 | + if err != nil { |
| 108 | + cli.PrintErrln(errhand.VerboseErrorFromError(err)) |
| 109 | + return 1 |
| 110 | + } |
| 111 | + |
| 112 | + headRef := dEnv.RepoState.CWBHeadRef() |
| 113 | + |
| 114 | + optionalCommit, err := db.Resolve(ctx, commitSpec, headRef) |
| 115 | + if err != nil { |
| 116 | + cli.PrintErrln(errhand.VerboseErrorFromError(err)) |
| 117 | + return 1 |
| 118 | + } |
| 119 | + parentCommits = append(parentCommits, optionalCommit.Addr) |
| 120 | + } |
| 121 | + |
| 122 | + commitMeta, err := datas.NewCommitMeta(name, email, desc) |
| 123 | + if err != nil { |
| 124 | + cli.PrintErrln(errhand.VerboseErrorFromError(err)) |
| 125 | + return 1 |
| 126 | + } |
| 127 | + |
| 128 | + // This isn't technically an amend, but the Amend field controls whether the commit must be a child of the ref's current commit (if any) |
| 129 | + commitOpts := datas.CommitOptions{ |
| 130 | + Parents: parentCommits, |
| 131 | + Meta: commitMeta, |
| 132 | + Amend: force, |
| 133 | + } |
| 134 | + |
| 135 | + rootVal, err := db.ValueReadWriter().ReadValue(ctx, commitRootHash) |
| 136 | + if err != nil { |
| 137 | + cli.PrintErrln(errhand.VerboseErrorFromError(err)) |
| 138 | + return 1 |
| 139 | + } |
| 140 | + |
| 141 | + var commit *doltdb.Commit |
| 142 | + if isBranchSet { |
| 143 | + commit, err = db.CommitValue(ctx, ref.NewBranchRef(branch), rootVal, commitOpts) |
| 144 | + if errors.Is(err, datas.ErrMergeNeeded) { |
| 145 | + cli.PrintErrf("branch %s already exists. If you wish to overwrite it, add the --force flag", branch) |
| 146 | + return 1 |
| 147 | + } |
| 148 | + } else { |
| 149 | + commit, err = db.CommitDangling(ctx, rootVal, commitOpts) |
| 150 | + } |
| 151 | + if err != nil { |
| 152 | + cli.PrintErrln(errhand.VerboseErrorFromError(err)) |
| 153 | + return 1 |
| 154 | + } |
| 155 | + |
| 156 | + commitHash, err := commit.HashOf() |
| 157 | + if err != nil { |
| 158 | + cli.PrintErrln(errhand.VerboseErrorFromError(err)) |
| 159 | + return 1 |
| 160 | + } |
| 161 | + |
| 162 | + cli.Println(commitHash.String()) |
| 163 | + |
| 164 | + return 0 |
| 165 | +} |
0 commit comments