github.com/benchkram/bob@v0.0.0-20240314204020-b7a57f2f9be9/test/setup/reposetup/fill.go (about)

     1  package reposetup
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"time"
     8  
     9  	git "github.com/go-git/go-git/v5"
    10  	"github.com/go-git/go-git/v5/plumbing"
    11  	"github.com/go-git/go-git/v5/plumbing/object"
    12  
    13  	"github.com/benchkram/errz"
    14  )
    15  
    16  const (
    17  	fileContents = "hello world\n"
    18  )
    19  
    20  func fillRepo(repo git.Repository, basePath string) error {
    21  	wt, err := repo.Worktree()
    22  	if err != nil {
    23  		return fmt.Errorf("failed to get worktree: %w", err)
    24  	}
    25  
    26  	if err := createAndCommitNewFile(wt, basePath, "initial", fileContents); err != nil {
    27  		return err
    28  	}
    29  
    30  	if err := createAndSwitchToBranch(wt, "branch1"); err != nil {
    31  		return err
    32  	}
    33  	if err := createAndCommitNewFile(wt, basePath, "file1", fileContents); err != nil {
    34  		return err
    35  	}
    36  
    37  	if err := createAndSwitchToBranch(wt, "branch2"); err != nil {
    38  		return err
    39  	}
    40  	if err := createAndCommitNewFile(wt, basePath, "file2", fileContents); err != nil {
    41  		return err
    42  	}
    43  
    44  	if err := createAndSwitchToBranch(wt, "branch3"); err != nil {
    45  		return err
    46  	}
    47  	if err := createAndCommitNewFile(wt, basePath, "file3", fileContents); err != nil {
    48  		return err
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  func createNewFile(basePath, name, contents string) error {
    55  	file, err := os.Create(filepath.Join(basePath, name))
    56  	if err != nil {
    57  		return fmt.Errorf("failed to create file %q: %w", name, err)
    58  	}
    59  	defer file.Close()
    60  	if _, err := file.WriteString(contents); err != nil {
    61  		return fmt.Errorf("failed to write to new file %q: %w", name, err)
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func createAndCommitNewFile(wt *git.Worktree, basePath, name, contents string) error {
    68  	if err := createNewFile(basePath, name, contents); err != nil {
    69  		return err
    70  	}
    71  
    72  	if _, err := wt.Add(name); err != nil {
    73  		return fmt.Errorf("failed to add file %q: %w", name, err)
    74  	}
    75  
    76  	msg := fmt.Sprintf("Add %s", name)
    77  	err := commit(wt, msg)
    78  	errz.Fatal(err)
    79  
    80  	return nil
    81  }
    82  
    83  func commit(wt *git.Worktree, msg string) error {
    84  	author := &object.Signature{
    85  		Name:  "Bob The Builder",
    86  		Email: "bob@thebuilder.com",
    87  		When:  time.Now(),
    88  	}
    89  
    90  	if _, err := wt.Commit(msg, &git.CommitOptions{Author: author}); err != nil {
    91  		return fmt.Errorf("failed to commit: %w", err)
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func createAndSwitchToBranch(wt *git.Worktree, name string) error {
    98  	if err := wt.Checkout(&git.CheckoutOptions{
    99  		Create: true,
   100  		Branch: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", name)),
   101  	}); err != nil {
   102  		return fmt.Errorf("failed to create and checkout branch %q: %w", name, err)
   103  	}
   104  
   105  	return nil
   106  }