github.com/shved/got@v0.0.0-20230322140632-a4bfa1e99685/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"time"
     8  
     9  	"github.com/shved/got/got"
    10  	"github.com/shved/got/object"
    11  	"github.com/shved/got/worktree"
    12  )
    13  
    14  var blankRepoCommands = []string{
    15  	"",
    16  	"init",
    17  	"help",
    18  }
    19  
    20  func init() {
    21  	flag.Parse()
    22  }
    23  
    24  func main() {
    25  	command := flag.Arg(0)
    26  
    27  	if !blankRepoCommand(command) {
    28  		got.SetRepoRoot()
    29  	}
    30  
    31  	switch command {
    32  	case "init":
    33  		got.InitRepo()
    34  		fmt.Println("Repo created in a current working directory")
    35  	case "commit":
    36  		message := flag.Arg(1)
    37  		if message == "" {
    38  			fmt.Println("No commit message provided")
    39  			os.Exit(0)
    40  		}
    41  		worktree.MakeCommit(message, time.Now())
    42  		fmt.Println("Worktree commited:", got.ReadHead())
    43  	case "to":
    44  		shaString := flag.Arg(1)
    45  		if shaString == "" {
    46  			fmt.Println("No commit hash provided")
    47  			os.Exit(0)
    48  		}
    49  		worktree.ToCommit(shaString)
    50  		fmt.Println("Worktree restored from commit:", shaString)
    51  	case "show":
    52  		shaString := flag.Arg(1)
    53  		if shaString == "" {
    54  			fmt.Println("No commit hash provided")
    55  			os.Exit(0)
    56  		}
    57  		fmt.Println(object.Show(shaString))
    58  	case "log":
    59  		fmt.Println(got.ReadLog())
    60  	case "current":
    61  		fmt.Println("Current commit hash:", got.ReadHead())
    62  	case "help":
    63  		printHelpMessage()
    64  		os.Exit(0)
    65  	default:
    66  		printHelpMessage()
    67  		os.Exit(0)
    68  	}
    69  }
    70  
    71  func printHelpMessage() {
    72  	fmt.Println(`got init                                        // to init a repo in current dir
    73  got commit 'initial commit'                     // to commit the state
    74  got log                                         // to see commits list
    75  got to d143528ac209d5d927e485e0f923758a21d0901e // to restore a commit
    76  got current                                     // to see current head commit hash`)
    77  }
    78  
    79  func blankRepoCommand(command string) bool {
    80  	for _, com := range blankRepoCommands {
    81  		if com == command {
    82  			return true
    83  		}
    84  	}
    85  
    86  	return false
    87  }