github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_commit.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  // Copyright 2015 The Gogs Authors. All rights reserved.
     7  
     8  package git
     9  
    10  import (
    11  	"bytes"
    12  	"io"
    13  	"strconv"
    14  	"strings"
    15  
    16  	"github.com/gitbundle/modules/setting"
    17  )
    18  
    19  // GetBranchCommitID returns last commit ID string of given branch.
    20  func (repo *Repository) GetBranchCommitID(name string) (string, error) {
    21  	return repo.GetRefCommitID(BranchPrefix + name)
    22  }
    23  
    24  // GetTagCommitID returns last commit ID string of given tag.
    25  func (repo *Repository) GetTagCommitID(name string) (string, error) {
    26  	return repo.GetRefCommitID(TagPrefix + name)
    27  }
    28  
    29  // GetCommit returns commit object of by ID string.
    30  func (repo *Repository) GetCommit(commitID string) (*Commit, error) {
    31  	id, err := repo.ConvertToSHA1(commitID)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	return repo.getCommit(id)
    37  }
    38  
    39  // GetBranchCommit returns the last commit of given branch.
    40  func (repo *Repository) GetBranchCommit(name string) (*Commit, error) {
    41  	commitID, err := repo.GetBranchCommitID(name)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return repo.GetCommit(commitID)
    46  }
    47  
    48  // GetTagCommit get the commit of the specific tag via name
    49  func (repo *Repository) GetTagCommit(name string) (*Commit, error) {
    50  	commitID, err := repo.GetTagCommitID(name)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return repo.GetCommit(commitID)
    55  }
    56  
    57  func (repo *Repository) getCommitByPathWithID(id SHA1, relpath string) (*Commit, error) {
    58  	// File name starts with ':' must be escaped.
    59  	if relpath[0] == ':' {
    60  		relpath = `\` + relpath
    61  	}
    62  
    63  	stdout, _, runErr := NewCommand(repo.Ctx, "log", "-1", prettyLogFormat, id.String(), "--", relpath).RunStdString(&RunOpts{Dir: repo.Path})
    64  	if runErr != nil {
    65  		return nil, runErr
    66  	}
    67  
    68  	id, err := NewIDFromString(stdout)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	return repo.getCommit(id)
    74  }
    75  
    76  // GetCommitByPath returns the last commit of relative path.
    77  func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
    78  	stdout, _, runErr := NewCommand(repo.Ctx, "log", "-1", prettyLogFormat, "--", relpath).RunStdBytes(&RunOpts{Dir: repo.Path})
    79  	if runErr != nil {
    80  		return nil, runErr
    81  	}
    82  
    83  	commits, err := repo.parsePrettyFormatLogToList(stdout)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	return commits[0], nil
    88  }
    89  
    90  func (repo *Repository) commitsByRange(id SHA1, page, pageSize int) ([]*Commit, error) {
    91  	stdout, _, err := NewCommand(repo.Ctx, "log", id.String(), "--skip="+strconv.Itoa((page-1)*pageSize),
    92  		"--max-count="+strconv.Itoa(pageSize), prettyLogFormat).RunStdBytes(&RunOpts{Dir: repo.Path})
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	return repo.parsePrettyFormatLogToList(stdout)
    97  }
    98  
    99  func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Commit, error) {
   100  	// create new git log command with limit of 100 commis
   101  	cmd := NewCommand(repo.Ctx, "log", id.String(), "-100", prettyLogFormat)
   102  	// ignore case
   103  	args := []string{"-i"}
   104  
   105  	// add authors if present in search query
   106  	if len(opts.Authors) > 0 {
   107  		for _, v := range opts.Authors {
   108  			args = append(args, "--author="+v)
   109  		}
   110  	}
   111  
   112  	// add committers if present in search query
   113  	if len(opts.Committers) > 0 {
   114  		for _, v := range opts.Committers {
   115  			args = append(args, "--committer="+v)
   116  		}
   117  	}
   118  
   119  	// add time constraints if present in search query
   120  	if len(opts.After) > 0 {
   121  		args = append(args, "--after="+opts.After)
   122  	}
   123  	if len(opts.Before) > 0 {
   124  		args = append(args, "--before="+opts.Before)
   125  	}
   126  
   127  	// pretend that all refs along with HEAD were listed on command line as <commis>
   128  	// https://git-scm.com/docs/git-log#Documentation/git-log.txt---all
   129  	// note this is done only for command created above
   130  	if opts.All {
   131  		cmd.AddArguments("--all")
   132  	}
   133  
   134  	// add remaining keywords from search string
   135  	// note this is done only for command created above
   136  	if len(opts.Keywords) > 0 {
   137  		for _, v := range opts.Keywords {
   138  			cmd.AddArguments("--grep=" + v)
   139  		}
   140  	}
   141  
   142  	// search for commits matching given constraints and keywords in commit msg
   143  	cmd.AddArguments(args...)
   144  	stdout, _, err := cmd.RunStdBytes(&RunOpts{Dir: repo.Path})
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	if len(stdout) != 0 {
   149  		stdout = append(stdout, '\n')
   150  	}
   151  
   152  	// if there are any keywords (ie not committer:, author:, time:)
   153  	// then let's iterate over them
   154  	if len(opts.Keywords) > 0 {
   155  		for _, v := range opts.Keywords {
   156  			// ignore anything below 4 characters as too unspecific
   157  			if len(v) >= 4 {
   158  				// create new git log command with 1 commit limit
   159  				hashCmd := NewCommand(repo.Ctx, "log", "-1", prettyLogFormat)
   160  				// add previous arguments except for --grep and --all
   161  				hashCmd.AddArguments(args...)
   162  				// add keyword as <commit>
   163  				hashCmd.AddArguments(v)
   164  
   165  				// search with given constraints for commit matching sha hash of v
   166  				hashMatching, _, err := hashCmd.RunStdBytes(&RunOpts{Dir: repo.Path})
   167  				if err != nil || bytes.Contains(stdout, hashMatching) {
   168  					continue
   169  				}
   170  				stdout = append(stdout, hashMatching...)
   171  				stdout = append(stdout, '\n')
   172  			}
   173  		}
   174  	}
   175  
   176  	return repo.parsePrettyFormatLogToList(bytes.TrimSuffix(stdout, []byte{'\n'}))
   177  }
   178  
   179  func (repo *Repository) getFilesChanged(id1, id2 string) ([]string, error) {
   180  	stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only", id1, id2).RunStdBytes(&RunOpts{Dir: repo.Path})
   181  	if err != nil {
   182  		return nil, err
   183  	}
   184  	return strings.Split(string(stdout), "\n"), nil
   185  }
   186  
   187  // FileChangedBetweenCommits Returns true if the file changed between commit IDs id1 and id2
   188  // You must ensure that id1 and id2 are valid commit ids.
   189  func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bool, error) {
   190  	stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only", "-z", id1, id2, "--", filename).RunStdBytes(&RunOpts{Dir: repo.Path})
   191  	if err != nil {
   192  		return false, err
   193  	}
   194  	return len(strings.TrimSpace(string(stdout))) > 0, nil
   195  }
   196  
   197  // FileCommitsCount return the number of files at a revision
   198  func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
   199  	return CommitsCountFiles(repo.Ctx, repo.Path, []string{revision}, []string{file})
   200  }
   201  
   202  // CommitsByFileAndRange return the commits according revision file and the page
   203  func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) ([]*Commit, error) {
   204  	skip := (page - 1) * setting.Git.CommitsRangeSize
   205  
   206  	stdoutReader, stdoutWriter := io.Pipe()
   207  	defer func() {
   208  		_ = stdoutReader.Close()
   209  		_ = stdoutWriter.Close()
   210  	}()
   211  	go func() {
   212  		stderr := strings.Builder{}
   213  		err := NewCommand(repo.Ctx, "log", revision, "--follow",
   214  			"--max-count="+strconv.Itoa(setting.Git.CommitsRangeSize*page),
   215  			prettyLogFormat, "--", file).
   216  			Run(&RunOpts{
   217  				Dir:    repo.Path,
   218  				Stdout: stdoutWriter,
   219  				Stderr: &stderr,
   220  			})
   221  		if err != nil {
   222  			_ = stdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
   223  		} else {
   224  			_ = stdoutWriter.Close()
   225  		}
   226  	}()
   227  
   228  	if skip > 0 {
   229  		_, err := io.CopyN(io.Discard, stdoutReader, int64(skip*41))
   230  		if err != nil {
   231  			if err == io.EOF {
   232  				return []*Commit{}, nil
   233  			}
   234  			_ = stdoutReader.CloseWithError(err)
   235  			return nil, err
   236  		}
   237  	}
   238  
   239  	stdout, err := io.ReadAll(stdoutReader)
   240  	if err != nil {
   241  		return nil, err
   242  	}
   243  	return repo.parsePrettyFormatLogToList(stdout)
   244  }
   245  
   246  // CommitsByFileAndRangeNoFollow return the commits according revision file and the page
   247  func (repo *Repository) CommitsByFileAndRangeNoFollow(revision, file string, page int) ([]*Commit, error) {
   248  	stdout, _, err := NewCommand(repo.Ctx, "log", revision, "--skip="+strconv.Itoa((page-1)*50),
   249  		"--max-count="+strconv.Itoa(setting.Git.CommitsRangeSize), prettyLogFormat, "--", file).RunStdBytes(&RunOpts{Dir: repo.Path})
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  	return repo.parsePrettyFormatLogToList(stdout)
   254  }
   255  
   256  // FilesCountBetween return the number of files changed between two commits
   257  func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (int, error) {
   258  	stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only", startCommitID+"..."+endCommitID).RunStdString(&RunOpts{Dir: repo.Path})
   259  	if err != nil && strings.Contains(err.Error(), "no merge base") {
   260  		// git >= 2.28 now returns an error if startCommitID and endCommitID have become unrelated.
   261  		// previously it would return the results of git diff --name-only startCommitID endCommitID so let's try that...
   262  		stdout, _, err = NewCommand(repo.Ctx, "diff", "--name-only", startCommitID, endCommitID).RunStdString(&RunOpts{Dir: repo.Path})
   263  	}
   264  	if err != nil {
   265  		return 0, err
   266  	}
   267  	return len(strings.Split(stdout, "\n")) - 1, nil
   268  }
   269  
   270  // CommitsBetween returns a list that contains commits between [before, last).
   271  // If before is detached (removed by reset + push) it is not included.
   272  func (repo *Repository) CommitsBetween(last, before *Commit) ([]*Commit, error) {
   273  	var stdout []byte
   274  	var err error
   275  	if before == nil {
   276  		stdout, _, err = NewCommand(repo.Ctx, "rev-list", last.ID.String()).RunStdBytes(&RunOpts{Dir: repo.Path})
   277  	} else {
   278  		stdout, _, err = NewCommand(repo.Ctx, "rev-list", before.ID.String()+".."+last.ID.String()).RunStdBytes(&RunOpts{Dir: repo.Path})
   279  		if err != nil && strings.Contains(err.Error(), "no merge base") {
   280  			// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
   281  			// previously it would return the results of git rev-list before last so let's try that...
   282  			stdout, _, err = NewCommand(repo.Ctx, "rev-list", before.ID.String(), last.ID.String()).RunStdBytes(&RunOpts{Dir: repo.Path})
   283  		}
   284  	}
   285  	if err != nil {
   286  		return nil, err
   287  	}
   288  	return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
   289  }
   290  
   291  // CommitsBetweenLimit returns a list that contains at most limit commits skipping the first skip commits between [before, last)
   292  func (repo *Repository) CommitsBetweenLimit(last, before *Commit, limit, skip int) ([]*Commit, error) {
   293  	var stdout []byte
   294  	var err error
   295  	if before == nil {
   296  		stdout, _, err = NewCommand(repo.Ctx, "rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), last.ID.String()).RunStdBytes(&RunOpts{Dir: repo.Path})
   297  	} else {
   298  		stdout, _, err = NewCommand(repo.Ctx, "rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), before.ID.String()+".."+last.ID.String()).RunStdBytes(&RunOpts{Dir: repo.Path})
   299  		if err != nil && strings.Contains(err.Error(), "no merge base") {
   300  			// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
   301  			// previously it would return the results of git rev-list --max-count n before last so let's try that...
   302  			stdout, _, err = NewCommand(repo.Ctx, "rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), before.ID.String(), last.ID.String()).RunStdBytes(&RunOpts{Dir: repo.Path})
   303  		}
   304  	}
   305  	if err != nil {
   306  		return nil, err
   307  	}
   308  	return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
   309  }
   310  
   311  // CommitsBetweenIDs return commits between twoe commits
   312  func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error) {
   313  	lastCommit, err := repo.GetCommit(last)
   314  	if err != nil {
   315  		return nil, err
   316  	}
   317  	if before == "" {
   318  		return repo.CommitsBetween(lastCommit, nil)
   319  	}
   320  	beforeCommit, err := repo.GetCommit(before)
   321  	if err != nil {
   322  		return nil, err
   323  	}
   324  	return repo.CommitsBetween(lastCommit, beforeCommit)
   325  }
   326  
   327  // CommitsCountBetween return numbers of commits between two commits
   328  func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
   329  	count, err := CommitsCountFiles(repo.Ctx, repo.Path, []string{start + ".." + end}, []string{})
   330  	if err != nil && strings.Contains(err.Error(), "no merge base") {
   331  		// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
   332  		// previously it would return the results of git rev-list before last so let's try that...
   333  		return CommitsCountFiles(repo.Ctx, repo.Path, []string{start, end}, []string{})
   334  	}
   335  
   336  	return count, err
   337  }
   338  
   339  // commitsBefore the limit is depth, not total number of returned commits.
   340  func (repo *Repository) commitsBefore(id SHA1, limit int) ([]*Commit, error) {
   341  	cmd := NewCommand(repo.Ctx, "log")
   342  	if limit > 0 {
   343  		cmd.AddArguments("-"+strconv.Itoa(limit), prettyLogFormat, id.String())
   344  	} else {
   345  		cmd.AddArguments(prettyLogFormat, id.String())
   346  	}
   347  
   348  	stdout, _, runErr := cmd.RunStdBytes(&RunOpts{Dir: repo.Path})
   349  	if runErr != nil {
   350  		return nil, runErr
   351  	}
   352  
   353  	formattedLog, err := repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
   354  	if err != nil {
   355  		return nil, err
   356  	}
   357  
   358  	commits := make([]*Commit, 0, len(formattedLog))
   359  	for _, commit := range formattedLog {
   360  		branches, err := repo.getBranches(commit, 2)
   361  		if err != nil {
   362  			return nil, err
   363  		}
   364  
   365  		if len(branches) > 1 {
   366  			break
   367  		}
   368  
   369  		commits = append(commits, commit)
   370  	}
   371  
   372  	return commits, nil
   373  }
   374  
   375  func (repo *Repository) getCommitsBefore(id SHA1) ([]*Commit, error) {
   376  	return repo.commitsBefore(id, 0)
   377  }
   378  
   379  func (repo *Repository) getCommitsBeforeLimit(id SHA1, num int) ([]*Commit, error) {
   380  	return repo.commitsBefore(id, num)
   381  }
   382  
   383  func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error) {
   384  	if CheckGitVersionAtLeast("2.7.0") == nil {
   385  		stdout, _, err := NewCommand(repo.Ctx, "for-each-ref", "--count="+strconv.Itoa(limit), "--format=%(refname:strip=2)", "--contains", commit.ID.String(), BranchPrefix).RunStdString(&RunOpts{Dir: repo.Path})
   386  		if err != nil {
   387  			return nil, err
   388  		}
   389  
   390  		branches := strings.Fields(stdout)
   391  		return branches, nil
   392  	}
   393  
   394  	stdout, _, err := NewCommand(repo.Ctx, "branch", "--contains", commit.ID.String()).RunStdString(&RunOpts{Dir: repo.Path})
   395  	if err != nil {
   396  		return nil, err
   397  	}
   398  
   399  	refs := strings.Split(stdout, "\n")
   400  
   401  	var max int
   402  	if len(refs) > limit {
   403  		max = limit
   404  	} else {
   405  		max = len(refs) - 1
   406  	}
   407  
   408  	branches := make([]string, max)
   409  	for i, ref := range refs[:max] {
   410  		parts := strings.Fields(ref)
   411  
   412  		branches[i] = parts[len(parts)-1]
   413  	}
   414  	return branches, nil
   415  }
   416  
   417  // GetCommitsFromIDs get commits from commit IDs
   418  func (repo *Repository) GetCommitsFromIDs(commitIDs []string) []*Commit {
   419  	commits := make([]*Commit, 0, len(commitIDs))
   420  
   421  	for _, commitID := range commitIDs {
   422  		commit, err := repo.GetCommit(commitID)
   423  		if err == nil && commit != nil {
   424  			commits = append(commits, commit)
   425  		}
   426  	}
   427  
   428  	return commits
   429  }
   430  
   431  // IsCommitInBranch check if the commit is on the branch
   432  func (repo *Repository) IsCommitInBranch(commitID, branch string) (r bool, err error) {
   433  	stdout, _, err := NewCommand(repo.Ctx, "branch", "--contains", commitID, branch).RunStdString(&RunOpts{Dir: repo.Path})
   434  	if err != nil {
   435  		return false, err
   436  	}
   437  	return len(stdout) > 0, err
   438  }