github.com/wtfutil/wtf@v0.43.0/modules/git/git_repo.go (about)

     1  package git
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strings"
     7  
     8  	"github.com/wtfutil/wtf/utils"
     9  )
    10  
    11  type GitRepo struct {
    12  	Branch       string
    13  	ChangedFiles []string
    14  	Commits      []string
    15  	Repository   string
    16  	Path         string
    17  }
    18  
    19  func NewGitRepo(repoPath string, commitCount int, commitFormat, dateFormat string) *GitRepo {
    20  	repo := GitRepo{Path: repoPath}
    21  
    22  	repo.Branch = repo.branch()
    23  	repo.ChangedFiles = repo.changedFiles()
    24  	repo.Commits = repo.commits(commitCount, commitFormat, dateFormat)
    25  	repo.Repository = strings.TrimSpace(repo.repository())
    26  
    27  	return &repo
    28  }
    29  
    30  /* -------------------- Unexported Functions -------------------- */
    31  
    32  func (repo *GitRepo) branch() string {
    33  	arg := []string{repo.gitDir(), repo.workTree(), "rev-parse", "--abbrev-ref", "HEAD"}
    34  
    35  	cmd := exec.Command(__go_cmd, arg...)
    36  	str := utils.ExecuteCommand(cmd)
    37  
    38  	return str
    39  }
    40  
    41  func (repo *GitRepo) changedFiles() []string {
    42  	arg := []string{repo.gitDir(), repo.workTree(), "status", "--porcelain"}
    43  
    44  	cmd := exec.Command(__go_cmd, arg...)
    45  	str := utils.ExecuteCommand(cmd)
    46  
    47  	data := strings.Split(str, "\n")
    48  
    49  	return data
    50  }
    51  
    52  func (repo *GitRepo) commits(commitCount int, commitFormat, dateFormat string) []string {
    53  	dateStr := fmt.Sprintf("--date=format:\"%s\"", dateFormat)
    54  	numStr := fmt.Sprintf("-n %d", commitCount)
    55  	commitStr := fmt.Sprintf("--pretty=format:\"%s\"", commitFormat)
    56  
    57  	arg := []string{repo.gitDir(), repo.workTree(), "log", dateStr, numStr, commitStr}
    58  
    59  	cmd := exec.Command(__go_cmd, arg...)
    60  	str := utils.ExecuteCommand(cmd)
    61  
    62  	data := strings.Split(str, "\n")
    63  
    64  	return data
    65  }
    66  
    67  func (repo *GitRepo) repository() string {
    68  	arg := []string{repo.gitDir(), repo.workTree(), "rev-parse", "--show-toplevel"}
    69  	cmd := exec.Command(__go_cmd, arg...)
    70  	str := utils.ExecuteCommand(cmd)
    71  
    72  	return str
    73  }
    74  func (repo *GitRepo) pull() string {
    75  	arg := []string{repo.gitDir(), repo.workTree(), "pull"}
    76  	cmd := exec.Command(__go_cmd, arg...)
    77  	str := utils.ExecuteCommand(cmd)
    78  	return str
    79  }
    80  
    81  func (repo *GitRepo) checkout(branch string) string {
    82  	arg := []string{repo.gitDir(), repo.workTree(), "checkout", branch}
    83  	cmd := exec.Command(__go_cmd, arg...)
    84  	str := utils.ExecuteCommand(cmd)
    85  	return str
    86  }
    87  
    88  func (repo *GitRepo) gitDir() string {
    89  	return fmt.Sprintf("--git-dir=%s/.git", repo.Path)
    90  }
    91  
    92  func (repo *GitRepo) workTree() string {
    93  	return fmt.Sprintf("--work-tree=%s", repo.Path)
    94  }