utils.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "os"
  9. "path/filepath"
  10. "strings"
  11. )
  12. const prettyLogFormat = `--pretty=format:%H`
  13. func parsePrettyFormatLog(repo *Repository, logByts []byte) (*list.List, error) {
  14. l := list.New()
  15. if len(logByts) == 0 {
  16. return l, nil
  17. }
  18. parts := bytes.Split(logByts, []byte{'\n'})
  19. for _, commitId := range parts {
  20. commit, err := repo.GetCommit(string(commitId))
  21. if err != nil {
  22. return nil, err
  23. }
  24. l.PushBack(commit)
  25. }
  26. return l, nil
  27. }
  28. func RefEndName(refStr string) string {
  29. index := strings.LastIndex(refStr, "/")
  30. if index != -1 {
  31. return refStr[index+1:]
  32. }
  33. return refStr
  34. }
  35. // If the object is stored in its own file (i.e not in a pack file),
  36. // this function returns the full path to the object file.
  37. // It does not test if the file exists.
  38. func filepathFromSHA1(rootdir, sha1 string) string {
  39. return filepath.Join(rootdir, "objects", sha1[:2], sha1[2:])
  40. }
  41. // isDir returns true if given path is a directory,
  42. // or returns false when it's a file or does not exist.
  43. func isDir(dir string) bool {
  44. f, e := os.Stat(dir)
  45. if e != nil {
  46. return false
  47. }
  48. return f.IsDir()
  49. }
  50. // isFile returns true if given path is a file,
  51. // or returns false when it's a directory or does not exist.
  52. func isFile(filePath string) bool {
  53. f, e := os.Stat(filePath)
  54. if e != nil {
  55. return false
  56. }
  57. return !f.IsDir()
  58. }