gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "bufio"
  7. "bytes"
  8. "context"
  9. "errors"
  10. "io"
  11. "os/exec"
  12. "strconv"
  13. "strings"
  14. "code.gitea.io/gitea/modules/git/gitcmd"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/util"
  17. )
  18. // Commit represents a git commit.
  19. type Commit struct {
  20. Tree // FIXME: bad design, this field can be nil if the commit is from "last commit cache"
  21. ID ObjectID
  22. Author *Signature // never nil
  23. Committer *Signature // never nil
  24. CommitMessage string
  25. Signature *CommitSignature
  26. Parents []ObjectID // ID strings
  27. submoduleCache *ObjectCache[*SubModule]
  28. }
  29. // CommitSignature represents a git commit signature part.
  30. type CommitSignature struct {
  31. Signature string
  32. Payload string
  33. }
  34. // Message returns the commit message. Same as retrieving CommitMessage directly.
  35. func (c *Commit) Message() string {
  36. return c.CommitMessage
  37. }
  38. // Summary returns first line of commit message.
  39. // The string is forced to be valid UTF8
  40. func (c *Commit) Summary() string {
  41. return strings.ToValidUTF8(strings.Split(strings.TrimSpace(c.CommitMessage), "\n")[0], "?")
  42. }
  43. // ParentID returns oid of n-th parent (0-based index).
  44. // It returns nil if no such parent exists.
  45. func (c *Commit) ParentID(n int) (ObjectID, error) {
  46. if n >= len(c.Parents) {
  47. return nil, ErrNotExist{"", ""}
  48. }
  49. return c.Parents[n], nil
  50. }
  51. // Parent returns n-th parent (0-based index) of the commit.
  52. func (c *Commit) Parent(n int) (*Commit, error) {
  53. id, err := c.ParentID(n)
  54. if err != nil {
  55. return nil, err
  56. }
  57. parent, err := c.repo.getCommit(id)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return parent, nil
  62. }
  63. // ParentCount returns number of parents of the commit.
  64. // 0 if this is the root commit, otherwise 1,2, etc.
  65. func (c *Commit) ParentCount() int {
  66. return len(c.Parents)
  67. }
  68. // GetCommitByPath return the commit of relative path object.
  69. func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) {
  70. if c.repo.LastCommitCache != nil {
  71. return c.repo.LastCommitCache.GetCommitByPath(c.ID.String(), relpath)
  72. }
  73. return c.repo.getCommitByPathWithID(c.ID, relpath)
  74. }
  75. // AddChanges marks local changes to be ready for commit.
  76. func AddChanges(ctx context.Context, repoPath string, all bool, files ...string) error {
  77. cmd := gitcmd.NewCommand().AddArguments("add")
  78. if all {
  79. cmd.AddArguments("--all")
  80. }
  81. cmd.AddDashesAndList(files...)
  82. _, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
  83. return err
  84. }
  85. // CommitChangesOptions the options when a commit created
  86. type CommitChangesOptions struct {
  87. Committer *Signature
  88. Author *Signature
  89. Message string
  90. }
  91. // CommitChanges commits local changes with given committer, author and message.
  92. // If author is nil, it will be the same as committer.
  93. func CommitChanges(ctx context.Context, repoPath string, opts CommitChangesOptions) error {
  94. cmd := gitcmd.NewCommand()
  95. if opts.Committer != nil {
  96. cmd.AddOptionValues("-c", "user.name="+opts.Committer.Name)
  97. cmd.AddOptionValues("-c", "user.email="+opts.Committer.Email)
  98. }
  99. cmd.AddArguments("commit")
  100. if opts.Author == nil {
  101. opts.Author = opts.Committer
  102. }
  103. if opts.Author != nil {
  104. cmd.AddOptionFormat("--author='%s <%s>'", opts.Author.Name, opts.Author.Email)
  105. }
  106. cmd.AddOptionFormat("--message=%s", opts.Message)
  107. _, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
  108. // No stderr but exit status 1 means nothing to commit.
  109. if err != nil && err.Error() == "exit status 1" {
  110. return nil
  111. }
  112. return err
  113. }
  114. // AllCommitsCount returns count of all commits in repository
  115. func AllCommitsCount(ctx context.Context, repoPath string, hidePRRefs bool, files ...string) (int64, error) {
  116. cmd := gitcmd.NewCommand("rev-list")
  117. if hidePRRefs {
  118. cmd.AddArguments("--exclude=" + PullPrefix + "*")
  119. }
  120. cmd.AddArguments("--all", "--count")
  121. if len(files) > 0 {
  122. cmd.AddDashesAndList(files...)
  123. }
  124. stdout, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
  125. if err != nil {
  126. return 0, err
  127. }
  128. return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
  129. }
  130. // CommitsCountOptions the options when counting commits
  131. type CommitsCountOptions struct {
  132. RepoPath string
  133. Not string
  134. Revision []string
  135. RelPath []string
  136. Since string
  137. Until string
  138. }
  139. // CommitsCount returns number of total commits of until given revision.
  140. func CommitsCount(ctx context.Context, opts CommitsCountOptions) (int64, error) {
  141. cmd := gitcmd.NewCommand("rev-list", "--count")
  142. cmd.AddDynamicArguments(opts.Revision...)
  143. if opts.Not != "" {
  144. cmd.AddOptionValues("--not", opts.Not)
  145. }
  146. if len(opts.RelPath) > 0 {
  147. cmd.AddDashesAndList(opts.RelPath...)
  148. }
  149. stdout, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: opts.RepoPath})
  150. if err != nil {
  151. return 0, err
  152. }
  153. return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
  154. }
  155. // CommitsCount returns number of total commits of until current revision.
  156. func (c *Commit) CommitsCount() (int64, error) {
  157. return CommitsCount(c.repo.Ctx, CommitsCountOptions{
  158. RepoPath: c.repo.Path,
  159. Revision: []string{c.ID.String()},
  160. })
  161. }
  162. // CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize
  163. func (c *Commit) CommitsByRange(page, pageSize int, not, since, until string) ([]*Commit, error) {
  164. return c.repo.commitsByRangeWithTime(c.ID, page, pageSize, not, since, until)
  165. }
  166. // CommitsBefore returns all the commits before current revision
  167. func (c *Commit) CommitsBefore() ([]*Commit, error) {
  168. return c.repo.getCommitsBefore(c.ID)
  169. }
  170. // HasPreviousCommit returns true if a given commitHash is contained in commit's parents
  171. func (c *Commit) HasPreviousCommit(objectID ObjectID) (bool, error) {
  172. this := c.ID.String()
  173. that := objectID.String()
  174. if this == that {
  175. return false, nil
  176. }
  177. _, _, err := gitcmd.NewCommand("merge-base", "--is-ancestor").AddDynamicArguments(that, this).RunStdString(c.repo.Ctx, &gitcmd.RunOpts{Dir: c.repo.Path})
  178. if err == nil {
  179. return true, nil
  180. }
  181. var exitError *exec.ExitError
  182. if errors.As(err, &exitError) {
  183. if exitError.ProcessState.ExitCode() == 1 && len(exitError.Stderr) == 0 {
  184. return false, nil
  185. }
  186. }
  187. return false, err
  188. }
  189. // IsForcePush returns true if a push from oldCommitHash to this is a force push
  190. func (c *Commit) IsForcePush(oldCommitID string) (bool, error) {
  191. objectFormat, err := c.repo.GetObjectFormat()
  192. if err != nil {
  193. return false, err
  194. }
  195. if oldCommitID == objectFormat.EmptyObjectID().String() {
  196. return false, nil
  197. }
  198. oldCommit, err := c.repo.GetCommit(oldCommitID)
  199. if err != nil {
  200. return false, err
  201. }
  202. hasPreviousCommit, err := c.HasPreviousCommit(oldCommit.ID)
  203. return !hasPreviousCommit, err
  204. }
  205. // CommitsBeforeLimit returns num commits before current revision
  206. func (c *Commit) CommitsBeforeLimit(num int) ([]*Commit, error) {
  207. return c.repo.getCommitsBeforeLimit(c.ID, num)
  208. }
  209. // CommitsBeforeUntil returns the commits between commitID to current revision
  210. func (c *Commit) CommitsBeforeUntil(commitID string) ([]*Commit, error) {
  211. endCommit, err := c.repo.GetCommit(commitID)
  212. if err != nil {
  213. return nil, err
  214. }
  215. return c.repo.CommitsBetween(c, endCommit)
  216. }
  217. // SearchCommitsOptions specify the parameters for SearchCommits
  218. type SearchCommitsOptions struct {
  219. Keywords []string
  220. Authors, Committers []string
  221. After, Before string
  222. All bool
  223. }
  224. // NewSearchCommitsOptions construct a SearchCommitsOption from a space-delimited search string
  225. func NewSearchCommitsOptions(searchString string, forAllRefs bool) SearchCommitsOptions {
  226. var keywords, authors, committers []string
  227. var after, before string
  228. fields := strings.FieldsSeq(searchString)
  229. for k := range fields {
  230. switch {
  231. case strings.HasPrefix(k, "author:"):
  232. authors = append(authors, strings.TrimPrefix(k, "author:"))
  233. case strings.HasPrefix(k, "committer:"):
  234. committers = append(committers, strings.TrimPrefix(k, "committer:"))
  235. case strings.HasPrefix(k, "after:"):
  236. after = strings.TrimPrefix(k, "after:")
  237. case strings.HasPrefix(k, "before:"):
  238. before = strings.TrimPrefix(k, "before:")
  239. default:
  240. keywords = append(keywords, k)
  241. }
  242. }
  243. return SearchCommitsOptions{
  244. Keywords: keywords,
  245. Authors: authors,
  246. Committers: committers,
  247. After: after,
  248. Before: before,
  249. All: forAllRefs,
  250. }
  251. }
  252. // SearchCommits returns the commits match the keyword before current revision
  253. func (c *Commit) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) {
  254. return c.repo.searchCommits(c.ID, opts)
  255. }
  256. // GetFilesChangedSinceCommit get all changed file names between pastCommit to current revision
  257. func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) {
  258. return c.repo.GetFilesChangedBetween(pastCommit, c.ID.String())
  259. }
  260. // FileChangedSinceCommit Returns true if the file given has changed since the past commit
  261. // YOU MUST ENSURE THAT pastCommit is a valid commit ID.
  262. func (c *Commit) FileChangedSinceCommit(filename, pastCommit string) (bool, error) {
  263. return c.repo.FileChangedBetweenCommits(filename, pastCommit, c.ID.String())
  264. }
  265. // HasFile returns true if the file given exists on this commit
  266. // This does only mean it's there - it does not mean the file was changed during the commit.
  267. func (c *Commit) HasFile(filename string) (bool, error) {
  268. _, err := c.GetBlobByPath(filename)
  269. if err != nil {
  270. return false, err
  271. }
  272. return true, nil
  273. }
  274. // GetFileContent reads a file content as a string or returns false if this was not possible
  275. func (c *Commit) GetFileContent(filename string, limit int) (string, error) {
  276. entry, err := c.GetTreeEntryByPath(filename)
  277. if err != nil {
  278. return "", err
  279. }
  280. r, err := entry.Blob().DataAsync()
  281. if err != nil {
  282. return "", err
  283. }
  284. defer r.Close()
  285. if limit > 0 {
  286. bs := make([]byte, limit)
  287. n, err := util.ReadAtMost(r, bs)
  288. if err != nil {
  289. return "", err
  290. }
  291. return string(bs[:n]), nil
  292. }
  293. bytes, err := io.ReadAll(r)
  294. if err != nil {
  295. return "", err
  296. }
  297. return string(bytes), nil
  298. }
  299. // GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only')
  300. func (c *Commit) GetBranchName() (string, error) {
  301. cmd := gitcmd.NewCommand("name-rev")
  302. if DefaultFeatures().CheckVersionAtLeast("2.13.0") {
  303. cmd.AddArguments("--exclude", "refs/tags/*")
  304. }
  305. cmd.AddArguments("--name-only", "--no-undefined").AddDynamicArguments(c.ID.String())
  306. data, _, err := cmd.RunStdString(c.repo.Ctx, &gitcmd.RunOpts{Dir: c.repo.Path})
  307. if err != nil {
  308. // handle special case where git can not describe commit
  309. if strings.Contains(err.Error(), "cannot describe") {
  310. return "", nil
  311. }
  312. return "", err
  313. }
  314. // name-rev commitID output will be "master" or "master~12"
  315. return strings.SplitN(strings.TrimSpace(data), "~", 2)[0], nil
  316. }
  317. // CommitFileStatus represents status of files in a commit.
  318. type CommitFileStatus struct {
  319. Added []string
  320. Removed []string
  321. Modified []string
  322. }
  323. // NewCommitFileStatus creates a CommitFileStatus
  324. func NewCommitFileStatus() *CommitFileStatus {
  325. return &CommitFileStatus{
  326. []string{}, []string{}, []string{},
  327. }
  328. }
  329. func parseCommitFileStatus(fileStatus *CommitFileStatus, stdout io.Reader) {
  330. rd := bufio.NewReader(stdout)
  331. peek, err := rd.Peek(1)
  332. if err != nil {
  333. if err != io.EOF {
  334. log.Error("Unexpected error whilst reading from git log --name-status. Error: %v", err)
  335. }
  336. return
  337. }
  338. if peek[0] == '\n' || peek[0] == '\x00' {
  339. _, _ = rd.Discard(1)
  340. }
  341. for {
  342. modifier, err := rd.ReadString('\x00')
  343. if err != nil {
  344. if err != io.EOF {
  345. log.Error("Unexpected error whilst reading from git log --name-status. Error: %v", err)
  346. }
  347. return
  348. }
  349. file, err := rd.ReadString('\x00')
  350. if err != nil {
  351. if err != io.EOF {
  352. log.Error("Unexpected error whilst reading from git log --name-status. Error: %v", err)
  353. }
  354. return
  355. }
  356. file = file[:len(file)-1]
  357. switch modifier[0] {
  358. case 'A':
  359. fileStatus.Added = append(fileStatus.Added, file)
  360. case 'D':
  361. fileStatus.Removed = append(fileStatus.Removed, file)
  362. case 'M':
  363. fileStatus.Modified = append(fileStatus.Modified, file)
  364. }
  365. }
  366. }
  367. // GetCommitFileStatus returns file status of commit in given repository.
  368. func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*CommitFileStatus, error) {
  369. stdout, w := io.Pipe()
  370. done := make(chan struct{})
  371. fileStatus := NewCommitFileStatus()
  372. go func() {
  373. parseCommitFileStatus(fileStatus, stdout)
  374. close(done)
  375. }()
  376. stderr := new(bytes.Buffer)
  377. err := gitcmd.NewCommand("log", "--name-status", "-m", "--pretty=format:", "--first-parent", "--no-renames", "-z", "-1").AddDynamicArguments(commitID).Run(ctx, &gitcmd.RunOpts{
  378. Dir: repoPath,
  379. Stdout: w,
  380. Stderr: stderr,
  381. })
  382. w.Close() // Close writer to exit parsing goroutine
  383. if err != nil {
  384. return nil, gitcmd.ConcatenateError(err, stderr.String())
  385. }
  386. <-done
  387. return fileStatus, nil
  388. }
  389. // GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository.
  390. func GetFullCommitID(ctx context.Context, repoPath, shortID string) (string, error) {
  391. commitID, _, err := gitcmd.NewCommand("rev-parse").AddDynamicArguments(shortID).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
  392. if err != nil {
  393. if strings.Contains(err.Error(), "exit status 128") {
  394. return "", ErrNotExist{shortID, ""}
  395. }
  396. return "", err
  397. }
  398. return strings.TrimSpace(commitID), nil
  399. }
  400. // GetRepositoryDefaultPublicGPGKey returns the default public key for this commit
  401. func (c *Commit) GetRepositoryDefaultPublicGPGKey(forceUpdate bool) (*GPGSettings, error) {
  402. if c.repo == nil {
  403. return nil, nil
  404. }
  405. return c.repo.GetDefaultPublicGPGKey(forceUpdate)
  406. }
  407. func IsStringLikelyCommitID(objFmt ObjectFormat, s string, minLength ...int) bool {
  408. maxLen := 64 // sha256
  409. if objFmt != nil {
  410. maxLen = objFmt.FullLength()
  411. }
  412. minLen := util.OptionalArg(minLength, maxLen)
  413. if len(s) < minLen || len(s) > maxLen {
  414. return false
  415. }
  416. for _, c := range s {
  417. isHex := (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')
  418. if !isHex {
  419. return false
  420. }
  421. }
  422. return true
  423. }