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

     1  package kmgGit
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/bronze1man/kmg/kmgCmd"
     6  )
     7  
     8  /*
     9  // 返回最小变化的提交的名字
    10  // 找到当前文件和commitId里面变化最小的版本
    11  // localCommit 是当前文件的提交
    12  // targetCommit 是需要寻找最小版本的commit
    13   无.git文件恢复
    14   git clone xxx ./tmp1
    15   mv ./tmp1/.git ./
    16   git checkout --orphan current
    17   kmg GitSmallestChange -local=current -target=master
    18   git checkout xx //返回的那个分支地址
    19  */
    20  func (repo *Repository) MustSmallestChange(localCommit string, targetCommit string) string {
    21  	commitList := repo.MustGetAllParentCommitId(targetCommit)
    22  	minNum := 2 << 31
    23  	minCommit := ""
    24  	lineBreak := []byte("\n")
    25  	for _, commitName := range commitList {
    26  		output := kmgCmd.MustCombinedOutput("git diff " + localCommit + " " + commitName)
    27  		num := bytes.Count(output, lineBreak)
    28  		if minNum > num {
    29  			minNum = num
    30  			minCommit = commitName
    31  			if num == 0 {
    32  				break
    33  			}
    34  		}
    35  	}
    36  	return minCommit
    37  }