utils.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // trim the "refs/heads/"
  32. return refStr[len("refs/heads/"):]
  33. }
  34. index := strings.LastIndex(refStr, "/")
  35. if index != -1 {
  36. return refStr[index+1:]
  37. }
  38. return refStr
  39. }
  40. // If the object is stored in its own file (i.e not in a pack file),
  41. // this function returns the full path to the object file.
  42. // It does not test if the file exists.
  43. func filepathFromSHA1(rootdir, sha1 string) string {
  44. return filepath.Join(rootdir, "objects", sha1[:2], sha1[2:])
  45. }
  46. // isDir returns true if given path is a directory,
  47. // or returns false when it's a file or does not exist.
  48. func isDir(dir string) bool {
  49. f, e := os.Stat(dir)
  50. if e != nil {
  51. return false
  52. }
  53. return f.IsDir()
  54. }
  55. // isFile returns true if given path is a file,
  56. // or returns false when it's a directory or does not exist.
  57. func isFile(filePath string) bool {
  58. f, e := os.Stat(filePath)
  59. if e != nil {
  60. return false
  61. }
  62. return !f.IsDir()
  63. }
  64. func concatenateError(err error, stderr string) error {
  65. if len(stderr) == 0 {
  66. return err
  67. }
  68. return fmt.Errorf("%v: %s", err, stderr)
  69. }