github.com/keysonzzz/kmg@v0.0.0-20151121023212-05317bfd7d39/third/kmgGit/kmgGit.go (about)

     1  package kmgGit
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"sync"
     8  
     9  	"github.com/bronze1man/kmg/errors"
    10  	"github.com/bronze1man/kmg/kmgCmd"
    11  	"github.com/bronze1man/kmg/kmgFile"
    12  	"github.com/bronze1man/kmg/kmgStrings"
    13  )
    14  
    15  func GetRepositoryFromPath(path string) (repo *Repository, err error) {
    16  	p, err := kmgFile.SearchFileInParentDir(path, ".git")
    17  	if err != nil {
    18  		if err == kmgFile.NotFoundError {
    19  			return nil, errors.New("can not found .git folder,do you in a git repository?")
    20  		}
    21  		return
    22  	}
    23  	return &Repository{
    24  		gitPath: p,
    25  	}, nil
    26  }
    27  
    28  func MustGetRepositoryFromPath(path string) (repo *Repository) {
    29  	p, err := kmgFile.SearchFileInParentDir(path, ".git")
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	return &Repository{
    34  		gitPath: p,
    35  	}
    36  }
    37  
    38  func MustIsRepositoryAtPath(path string) bool {
    39  	return kmgFile.MustFileExist(filepath.Join(path, ".git"))
    40  }
    41  
    42  func MustGitClone(url string, path string) {
    43  	kmgCmd.CmdSlice([]string{"git", "clone", url, path}).MustRun()
    44  }
    45  
    46  var defaultRepoOnce sync.Once
    47  var defaultRepo *Repository
    48  
    49  func DefaultRepository() (repo *Repository) {
    50  	defaultRepoOnce.Do(func() {
    51  		wd, err := os.Getwd()
    52  		if err != nil {
    53  			panic(err)
    54  		}
    55  		defaultRepo, err = GetRepositoryFromPath(wd)
    56  		if err != nil {
    57  			panic(err)
    58  		}
    59  	})
    60  	return defaultRepo
    61  }
    62  
    63  type Repository struct {
    64  	gitPath string
    65  }
    66  
    67  func (repo *Repository) GetGitPath() string {
    68  	return repo.gitPath
    69  }
    70  func (repo *Repository) MustGetCurrentBranchName() string {
    71  	output := kmgCmd.CmdString("git rev-parse --abbrev-ref HEAD").SetDir(repo.gitPath).MustCombinedOutput()
    72  	return strings.TrimSpace(string(output))
    73  }
    74  
    75  func (repo *Repository) MustGetIndexFileList() []string {
    76  	output := kmgCmd.CmdString("git ls-files").SetDir(repo.gitPath).MustCombinedOutput()
    77  	outputSlice := []string{}
    78  	for _, s := range strings.Split(string(output), "\n") {
    79  		s = strings.TrimSpace(s)
    80  		if s == "" {
    81  			continue
    82  		}
    83  		outputSlice = append(outputSlice, s)
    84  	}
    85  	return outputSlice
    86  }
    87  
    88  func (repo *Repository) MustGetHeadCommitId() string {
    89  	output := kmgCmd.CmdString("git rev-parse HEAD").SetDir(repo.gitPath).MustCombinedOutput()
    90  	return strings.TrimSpace(string(output))
    91  }
    92  func (repo *Repository) MustResetToCommitId(commitId string) {
    93  	kmgCmd.CmdSlice([]string{"git", "reset", commitId}).SetDir(repo.gitPath).MustStdioRun()
    94  }
    95  func (repo *Repository) MustIndexRemoveByPath(path string) {
    96  	kmgCmd.CmdSlice([]string{"git", "rm", "--cached", "-rf", path}).SetDir(repo.gitPath).MustStdioRun()
    97  }
    98  
    99  func (repo *Repository) MustIndexAddFile(path string) {
   100  	kmgCmd.CmdSlice([]string{"git", "add", path}).SetDir(repo.gitPath).MustStdioRun()
   101  }
   102  
   103  func (repo *Repository) MustIsFileIgnore(path string) bool {
   104  	return kmgCmd.CmdSlice([]string{"git", "check-ignore", path}).SetDir(repo.gitPath).
   105  		MustHiddenRunAndIsSuccess()
   106  }
   107  
   108  // 这个返回自己这个commit的名称,也返回所有父级commit的名称
   109  func (repo *Repository) MustGetAllParentCommitId(commitId string) []string {
   110  	output := kmgCmd.CmdSlice([]string{"git", "log", "--format=%H", commitId}).SetDir(repo.gitPath).MustCombinedOutput()
   111  	outputSlice := []string{}
   112  	for _, s := range strings.Split(string(output), "\n") {
   113  		s = strings.TrimSpace(s)
   114  		if s == "" {
   115  			continue
   116  		}
   117  		outputSlice = append(outputSlice, s)
   118  	}
   119  	return outputSlice
   120  }
   121  
   122  func (repo *Repository) MustIsInParent(commitId string, parentCommitId string) bool {
   123  	allParent := repo.MustGetAllParentCommitId(commitId)
   124  	return kmgStrings.IsInSlice(allParent, parentCommitId)
   125  }
   126  
   127  //是否这个路径指向的东西在index里面存在(确切的文件)
   128  func (repo *Repository) MustIsFileInIndex(path string) bool {
   129  	output := kmgCmd.CmdSlice([]string{"git", "ls-files", path}).SetDir(repo.gitPath).MustCombinedOutput()
   130  	if len(output) == 0 {
   131  		return false
   132  	}
   133  	for _, s := range strings.Split(string(output), "\n") {
   134  		s = strings.TrimSpace(s)
   135  		if s == "" {
   136  			continue
   137  		}
   138  		if s == path {
   139  			return true
   140  		}
   141  	}
   142  	return false
   143  }
   144  
   145  //返回index里面某个文件路径下面是否有文件,如果它本身在index里面返回false
   146  func (repo *Repository) MustHasFilesInDirInIndex(path string) bool {
   147  	output := kmgCmd.CmdSlice([]string{"git", "ls-files", path}).SetDir(repo.gitPath).MustCombinedOutput()
   148  	if len(output) == 0 {
   149  		return false
   150  	}
   151  	hasElem := false
   152  	for _, s := range strings.Split(string(output), "\n") {
   153  		s = strings.TrimSpace(s)
   154  		if s == "" {
   155  			continue
   156  		}
   157  		hasElem = true
   158  		if s == path {
   159  			return false
   160  		}
   161  	}
   162  	return hasElem
   163  }
   164  
   165  func (repo *Repository) MustGetRemoteUrl(branch string) string {
   166  	output := kmgCmd.CmdSlice([]string{"git", "ls-remote", "--get-url", branch}).SetDir(repo.gitPath).MustCombinedOutput()
   167  	return strings.TrimSpace(string(output))
   168  }
   169  
   170  func (repo *Repository) MustSetRemoteUrl(branch string, url string) {
   171  	kmgCmd.CmdSlice([]string{"git", "remote", "add", branch, url}).SetDir(repo.gitPath).MustStdioRun()
   172  }