|
5 | 5 | package git |
6 | 6 |
|
7 | 7 | import ( |
8 | | - "bufio" |
9 | | - "bytes" |
10 | 8 | "context" |
11 | 9 | "errors" |
12 | 10 | "io" |
13 | 11 | "os/exec" |
14 | | - "strconv" |
15 | 12 | "strings" |
16 | 13 |
|
17 | 14 | "code.gitea.io/gitea/modules/git/gitcmd" |
18 | | - "code.gitea.io/gitea/modules/log" |
19 | 15 | "code.gitea.io/gitea/modules/util" |
20 | 16 | ) |
21 | 17 |
|
@@ -130,65 +126,6 @@ func CommitChanges(ctx context.Context, repoPath string, opts CommitChangesOptio |
130 | 126 | return err |
131 | 127 | } |
132 | 128 |
|
133 | | -// AllCommitsCount returns count of all commits in repository |
134 | | -func AllCommitsCount(ctx context.Context, repoPath string, hidePRRefs bool, files ...string) (int64, error) { |
135 | | - cmd := gitcmd.NewCommand("rev-list") |
136 | | - if hidePRRefs { |
137 | | - cmd.AddArguments("--exclude=" + PullPrefix + "*") |
138 | | - } |
139 | | - cmd.AddArguments("--all", "--count") |
140 | | - if len(files) > 0 { |
141 | | - cmd.AddDashesAndList(files...) |
142 | | - } |
143 | | - |
144 | | - stdout, _, err := cmd.WithDir(repoPath).RunStdString(ctx) |
145 | | - if err != nil { |
146 | | - return 0, err |
147 | | - } |
148 | | - |
149 | | - return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64) |
150 | | -} |
151 | | - |
152 | | -// CommitsCountOptions the options when counting commits |
153 | | -type CommitsCountOptions struct { |
154 | | - RepoPath string |
155 | | - Not string |
156 | | - Revision []string |
157 | | - RelPath []string |
158 | | - Since string |
159 | | - Until string |
160 | | -} |
161 | | - |
162 | | -// CommitsCount returns number of total commits of until given revision. |
163 | | -func CommitsCount(ctx context.Context, opts CommitsCountOptions) (int64, error) { |
164 | | - cmd := gitcmd.NewCommand("rev-list", "--count") |
165 | | - |
166 | | - cmd.AddDynamicArguments(opts.Revision...) |
167 | | - |
168 | | - if opts.Not != "" { |
169 | | - cmd.AddOptionValues("--not", opts.Not) |
170 | | - } |
171 | | - |
172 | | - if len(opts.RelPath) > 0 { |
173 | | - cmd.AddDashesAndList(opts.RelPath...) |
174 | | - } |
175 | | - |
176 | | - stdout, _, err := cmd.WithDir(opts.RepoPath).RunStdString(ctx) |
177 | | - if err != nil { |
178 | | - return 0, err |
179 | | - } |
180 | | - |
181 | | - return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64) |
182 | | -} |
183 | | - |
184 | | -// CommitsCount returns number of total commits of until current revision. |
185 | | -func (c *Commit) CommitsCount() (int64, error) { |
186 | | - return CommitsCount(c.repo.Ctx, CommitsCountOptions{ |
187 | | - RepoPath: c.repo.Path, |
188 | | - Revision: []string{c.ID.String()}, |
189 | | - }) |
190 | | -} |
191 | | - |
192 | 129 | // CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize |
193 | 130 | func (c *Commit) CommitsByRange(page, pageSize int, not, since, until string) ([]*Commit, error) { |
194 | 131 | return c.repo.commitsByRangeWithTime(c.ID, page, pageSize, not, since, until) |
@@ -371,85 +308,6 @@ func (c *Commit) GetBranchName() (string, error) { |
371 | 308 | return strings.SplitN(strings.TrimSpace(data), "~", 2)[0], nil |
372 | 309 | } |
373 | 310 |
|
374 | | -// CommitFileStatus represents status of files in a commit. |
375 | | -type CommitFileStatus struct { |
376 | | - Added []string |
377 | | - Removed []string |
378 | | - Modified []string |
379 | | -} |
380 | | - |
381 | | -// NewCommitFileStatus creates a CommitFileStatus |
382 | | -func NewCommitFileStatus() *CommitFileStatus { |
383 | | - return &CommitFileStatus{ |
384 | | - []string{}, []string{}, []string{}, |
385 | | - } |
386 | | -} |
387 | | - |
388 | | -func parseCommitFileStatus(fileStatus *CommitFileStatus, stdout io.Reader) { |
389 | | - rd := bufio.NewReader(stdout) |
390 | | - peek, err := rd.Peek(1) |
391 | | - if err != nil { |
392 | | - if err != io.EOF { |
393 | | - log.Error("Unexpected error whilst reading from git log --name-status. Error: %v", err) |
394 | | - } |
395 | | - return |
396 | | - } |
397 | | - if peek[0] == '\n' || peek[0] == '\x00' { |
398 | | - _, _ = rd.Discard(1) |
399 | | - } |
400 | | - for { |
401 | | - modifier, err := rd.ReadString('\x00') |
402 | | - if err != nil { |
403 | | - if err != io.EOF { |
404 | | - log.Error("Unexpected error whilst reading from git log --name-status. Error: %v", err) |
405 | | - } |
406 | | - return |
407 | | - } |
408 | | - file, err := rd.ReadString('\x00') |
409 | | - if err != nil { |
410 | | - if err != io.EOF { |
411 | | - log.Error("Unexpected error whilst reading from git log --name-status. Error: %v", err) |
412 | | - } |
413 | | - return |
414 | | - } |
415 | | - file = file[:len(file)-1] |
416 | | - switch modifier[0] { |
417 | | - case 'A': |
418 | | - fileStatus.Added = append(fileStatus.Added, file) |
419 | | - case 'D': |
420 | | - fileStatus.Removed = append(fileStatus.Removed, file) |
421 | | - case 'M': |
422 | | - fileStatus.Modified = append(fileStatus.Modified, file) |
423 | | - } |
424 | | - } |
425 | | -} |
426 | | - |
427 | | -// GetCommitFileStatus returns file status of commit in given repository. |
428 | | -func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*CommitFileStatus, error) { |
429 | | - stdout, w := io.Pipe() |
430 | | - done := make(chan struct{}) |
431 | | - fileStatus := NewCommitFileStatus() |
432 | | - go func() { |
433 | | - parseCommitFileStatus(fileStatus, stdout) |
434 | | - close(done) |
435 | | - }() |
436 | | - |
437 | | - stderr := new(bytes.Buffer) |
438 | | - err := gitcmd.NewCommand("log", "--name-status", "-m", "--pretty=format:", "--first-parent", "--no-renames", "-z", "-1"). |
439 | | - AddDynamicArguments(commitID). |
440 | | - WithDir(repoPath). |
441 | | - WithStdout(w). |
442 | | - WithStderr(stderr). |
443 | | - Run(ctx) |
444 | | - w.Close() // Close writer to exit parsing goroutine |
445 | | - if err != nil { |
446 | | - return nil, gitcmd.ConcatenateError(err, stderr.String()) |
447 | | - } |
448 | | - |
449 | | - <-done |
450 | | - return fileStatus, nil |
451 | | -} |
452 | | - |
453 | 311 | // GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository. |
454 | 312 | func GetFullCommitID(ctx context.Context, repoPath, shortID string) (string, error) { |
455 | 313 | commitID, _, err := gitcmd.NewCommand("rev-parse"). |
|
0 commit comments