utils.go 1.6 KB

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