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

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var logBranch string
    12  
    13  // Retrieves the commit history for a database branch
    14  var branchLogCmd = &cobra.Command{
    15  	Use:   "log [database name]",
    16  	Short: "Displays the history for a database branch",
    17  	RunE: func(cmd *cobra.Command, args []string) error {
    18  		return branchLog(args)
    19  	},
    20  }
    21  
    22  func init() {
    23  	RootCmd.AddCommand(branchLogCmd)
    24  	branchLogCmd.Flags().StringVar(&logBranch, "branch", "", "Remote branch to retrieve the "+
    25  		"history of")
    26  }
    27  
    28  func branchLog(args []string) error {
    29  	// Ensure a database file was given
    30  	var db string
    31  	var err error
    32  	if len(args) == 0 {
    33  		db, err = getDefaultDatabase()
    34  		if err != nil {
    35  			return err
    36  		}
    37  		if db == "" {
    38  			// No database name was given on the command line, and we don't have a default database selected
    39  			return errors.New("No database file specified")
    40  		}
    41  	} else {
    42  		db = args[0]
    43  	}
    44  	if len(args) > 1 {
    45  		return errors.New("only one database can be worked with at a time (for now)")
    46  	}
    47  
    48  	// If there is a local metadata cache for the requested database, use that.  Otherwise, retrieve it from the
    49  	// server first (without storing it)
    50  	var meta metaData
    51  	meta, err = localFetchMetadata(db, true)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	// If a branch name was given by the user, check if it exists
    57  	if logBranch != "" {
    58  		if _, ok := meta.Branches[logBranch]; ok == false {
    59  			return errors.New("That branch doesn't exist for the database")
    60  		}
    61  	} else {
    62  		logBranch = meta.ActiveBranch
    63  	}
    64  
    65  	// Retrieve the list of known licences
    66  	l, err := getLicences()
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	// Map the license sha256's to their friendly name for easy lookup
    72  	licList := make(map[string]string)
    73  	for _, j := range l {
    74  		licList[j.Sha256] = j.FullName
    75  	}
    76  
    77  	// Display the commits for the branch
    78  	headID := meta.Branches[logBranch].Commit
    79  	localCommit := meta.Commits[headID]
    80  	_, err = fmt.Fprintf(fOut, "Branch \"%s\" history for %s:\n\n", logBranch, db)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	_, err = fmt.Fprint(fOut, createCommitText(meta.Commits[localCommit.ID], licList))
    85  	if err != nil {
    86  		return err
    87  	}
    88  	for localCommit.Parent != "" {
    89  		localCommit = meta.Commits[localCommit.Parent]
    90  		_, err = fmt.Fprintf(fOut, createCommitText(meta.Commits[localCommit.ID], licList))
    91  		if err != nil {
    92  			return err
    93  		}
    94  	}
    95  	return nil
    96  }
    97  
    98  // Creates the user visible commit text for a commit.
    99  func createCommitText(c commitEntry, licList map[string]string) string {
   100  	s := fmt.Sprintf("  * Commit: %s\n", c.ID)
   101  	s += fmt.Sprintf("    Author: %s <%s>\n", c.AuthorName, c.AuthorEmail)
   102  	s += fmt.Sprintf("    Date: %v\n", c.Timestamp.Local().Format(time.RFC1123))
   103  	if c.Tree.Entries[0].LicenceSHA != "" {
   104  		s += fmt.Sprintf("    Licence: %s\n\n", licList[c.Tree.Entries[0].LicenceSHA])
   105  	} else {
   106  		s += fmt.Sprintf("\n")
   107  	}
   108  	if c.Message != "" {
   109  		s += fmt.Sprintf("      %s\n\n", c.Message)
   110  	}
   111  	return s
   112  }