github.com/sqlitebrowser/dio@v0.0.0-20240125125356-b587368e5c6b/cmd/branchCreate.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  var branchCreateBranch, branchCreateCommit, branchCreateMsg string
    11  
    12  // Creates a branch for a database
    13  var branchCreateCmd = &cobra.Command{
    14  	Use:   "create [database name] --branch xxx --commit yyy",
    15  	Short: "Create a branch for a database",
    16  	RunE: func(cmd *cobra.Command, args []string) error {
    17  		return branchCreate(args)
    18  	},
    19  }
    20  
    21  func init() {
    22  	branchCmd.AddCommand(branchCreateCmd)
    23  	branchCreateCmd.Flags().StringVar(&branchCreateBranch, "branch", "", "Name of remote branch to create")
    24  	branchCreateCmd.Flags().StringVar(&branchCreateCommit, "commit", "", "Commit ID for the new branch head")
    25  	branchCreateCmd.Flags().StringVar(&branchCreateMsg, "description", "", "Description of the branch")
    26  }
    27  
    28  func branchCreate(args []string) error {
    29  	// Ensure a database file was given
    30  	var db string
    31  	var err error
    32  	var meta metaData
    33  	if len(args) == 0 {
    34  		db, err = getDefaultDatabase()
    35  		if err != nil {
    36  			return err
    37  		}
    38  		if db == "" {
    39  			// No database name was given on the command line, and we don't have a default database selected
    40  			return errors.New("No database file specified")
    41  		}
    42  	} else {
    43  		db = args[0]
    44  	}
    45  	if len(args) > 1 {
    46  		return errors.New("Only one database can be changed at a time (for now)")
    47  	}
    48  
    49  	// Ensure a new branch name and commit ID were given
    50  	if branchCreateBranch == "" {
    51  		return errors.New("No branch name given")
    52  	}
    53  	if branchCreateCommit == "" {
    54  		return errors.New("No commit ID given")
    55  	}
    56  
    57  	// Load the metadata
    58  	meta, err = loadMetadata(db)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	// Ensure a branch with the same name doesn't already exist
    64  	if _, ok := meta.Branches[branchCreateBranch]; ok == true {
    65  		return errors.New("A branch with that name already exists")
    66  	}
    67  
    68  	// Make sure the target commit exists in our commit list
    69  	c, ok := meta.Commits[branchCreateCommit]
    70  	if ok != true {
    71  		return errors.New("That commit isn't in the database commit list")
    72  	}
    73  
    74  	// Count the number of commits in the new branch
    75  	numCommits := 1
    76  	for c.Parent != "" {
    77  		numCommits++
    78  		c = meta.Commits[c.Parent]
    79  	}
    80  
    81  	// Generate the new branch info locally
    82  	newBranch := branchEntry{
    83  		Commit:      branchCreateCommit,
    84  		CommitCount: numCommits,
    85  		Description: branchCreateMsg,
    86  	}
    87  
    88  	// Add the new branch to the local metadata cache
    89  	meta.Branches[branchCreateBranch] = newBranch
    90  
    91  	// Save the updated metadata back to disk
    92  	err = saveMetadata(db, meta)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	_, err = fmt.Fprintf(fOut, "Branch '%s' created\n", branchCreateBranch)
    98  	return err
    99  }