utils.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "bytes"
  7. "container/list"
  8. "fmt"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. const prettyLogFormat = `--pretty=format:%H`
  14. func parsePrettyFormatLog(repo *Repository, logByts []byte) (*list.List, error) {
  15. l := list.New()
  16. if len(logByts) == 0 {
  17. return l, nil
  18. }
  19. parts := bytes.Split(logByts, []byte{'\n'})
  20. for _, commitId := range parts {
  21. commit, err := repo.GetCommit(string(commitId))
  22. if err != nil {
  23. return nil, err
  24. }
  25. l.PushBack(commit)
  26. }
  27. return l, nil
  28. }
  29. func RefEndName(refStr string) string {
  30. if strings.HasPrefix(refStr, "refs/heads/") {
  31. return strings.TrimPrefix(refStr, "refs/heads/")
  32. }
  33. index := strings.LastIndex(refStr, "/")
  34. if index != -1 {
  35. return refStr[index+1:]
  36. }
  37. return refStr
  38. }
  39. // If the object is stored in its own file (i.e not in a pack file),
  40. // this function returns the full path to the object file.
  41. // It does not test if the file exists.
  42. func filepathFromSHA1(rootdir, sha1 string) string {
  43. return filepath.Join(rootdir, "objects", sha1[:2], sha1[2:])
  44. }
  45. // isDir returns true if given path is a directory,
  46. // or returns false when it's a file or does not exist.
  47. func isDir(dir string) bool {
  48. f, e := os.Stat(dir)
  49. if e != nil {
  50. return false
  51. }
  52. return f.IsDir()
  53. }
  54. // isFile returns true if given path is a file,
  55. // or returns false when it's a directory or does not exist.
  56. func isFile(filePath string) bool {
  57. f, e := os.Stat(filePath)
  58. if e != nil {
  59. return false
  60. }
  61. return !f.IsDir()
  62. }
  63. func concatenateError(err error, stderr string) error {
  64. if len(stderr) == 0 {
  65. return err
  66. }
  67. return fmt.Errorf("%v: %s", err, stderr)
  68. }