github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/utils.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  // Copyright 2015 The Gogs Authors. All rights reserved.
     7  // Use of this source code is governed by a MIT-style
     8  // license that can be found in the LICENSE file.
     9  
    10  package git
    11  
    12  import (
    13  	"fmt"
    14  	"io"
    15  	"os"
    16  	"strconv"
    17  	"strings"
    18  	"sync"
    19  
    20  	"github.com/gitbundle/modules/util"
    21  )
    22  
    23  // ObjectCache provides thread-safe cache operations.
    24  type ObjectCache struct {
    25  	lock  sync.RWMutex
    26  	cache map[string]interface{}
    27  }
    28  
    29  func newObjectCache() *ObjectCache {
    30  	return &ObjectCache{
    31  		cache: make(map[string]interface{}, 10),
    32  	}
    33  }
    34  
    35  // Set add obj to cache
    36  func (oc *ObjectCache) Set(id string, obj interface{}) {
    37  	oc.lock.Lock()
    38  	defer oc.lock.Unlock()
    39  
    40  	oc.cache[id] = obj
    41  }
    42  
    43  // Get get cached obj by id
    44  func (oc *ObjectCache) Get(id string) (interface{}, bool) {
    45  	oc.lock.RLock()
    46  	defer oc.lock.RUnlock()
    47  
    48  	obj, has := oc.cache[id]
    49  	return obj, has
    50  }
    51  
    52  // isDir returns true if given path is a directory,
    53  // or returns false when it's a file or does not exist.
    54  func isDir(dir string) bool {
    55  	f, e := os.Stat(dir)
    56  	if e != nil {
    57  		return false
    58  	}
    59  	return f.IsDir()
    60  }
    61  
    62  // isFile returns true if given path is a file,
    63  // or returns false when it's a directory or does not exist.
    64  func isFile(filePath string) bool {
    65  	f, e := os.Stat(filePath)
    66  	if e != nil {
    67  		return false
    68  	}
    69  	return !f.IsDir()
    70  }
    71  
    72  // isExist checks whether a file or directory exists.
    73  // It returns false when the file or directory does not exist.
    74  func isExist(path string) bool {
    75  	_, err := os.Stat(path)
    76  	return err == nil || os.IsExist(err)
    77  }
    78  
    79  // ConcatenateError concatenats an error with stderr string
    80  func ConcatenateError(err error, stderr string) error {
    81  	if len(stderr) == 0 {
    82  		return err
    83  	}
    84  	return fmt.Errorf("%w - %s", err, stderr)
    85  }
    86  
    87  // RefEndName return the end name of a ref name
    88  func RefEndName(refStr string) string {
    89  	if strings.HasPrefix(refStr, BranchPrefix) {
    90  		return refStr[len(BranchPrefix):]
    91  	}
    92  
    93  	if strings.HasPrefix(refStr, TagPrefix) {
    94  		return refStr[len(TagPrefix):]
    95  	}
    96  
    97  	return refStr
    98  }
    99  
   100  // RefURL returns the absolute URL for a ref in a repository
   101  func RefURL(repoURL, ref string) string {
   102  	refName := util.PathEscapeSegments(RefEndName(ref))
   103  	switch {
   104  	case strings.HasPrefix(ref, BranchPrefix):
   105  		return repoURL + "/src/branch/" + refName
   106  	case strings.HasPrefix(ref, TagPrefix):
   107  		return repoURL + "/src/tag/" + refName
   108  	default:
   109  		return repoURL + "/src/commit/" + refName
   110  	}
   111  }
   112  
   113  // SplitRefName splits a full refname to reftype and simple refname
   114  func SplitRefName(refStr string) (string, string) {
   115  	if strings.HasPrefix(refStr, BranchPrefix) {
   116  		return BranchPrefix, refStr[len(BranchPrefix):]
   117  	}
   118  
   119  	if strings.HasPrefix(refStr, TagPrefix) {
   120  		return TagPrefix, refStr[len(TagPrefix):]
   121  	}
   122  
   123  	return "", refStr
   124  }
   125  
   126  // ParseBool returns the boolean value represented by the string as per git's git_config_bool
   127  // true will be returned for the result if the string is empty, but valid will be false.
   128  // "true", "yes", "on" are all true, true
   129  // "false", "no", "off" are all false, true
   130  // 0 is false, true
   131  // Any other integer is true, true
   132  // Anything else will return false, false
   133  func ParseBool(value string) (result, valid bool) {
   134  	// Empty strings are true but invalid
   135  	if len(value) == 0 {
   136  		return true, false
   137  	}
   138  	// These are the git expected true and false values
   139  	if strings.EqualFold(value, "true") || strings.EqualFold(value, "yes") || strings.EqualFold(value, "on") {
   140  		return true, true
   141  	}
   142  	if strings.EqualFold(value, "false") || strings.EqualFold(value, "no") || strings.EqualFold(value, "off") {
   143  		return false, true
   144  	}
   145  	// Try a number
   146  	intValue, err := strconv.ParseInt(value, 10, 32)
   147  	if err != nil {
   148  		return false, false
   149  	}
   150  	return intValue != 0, true
   151  }
   152  
   153  // LimitedReaderCloser is a limited reader closer
   154  type LimitedReaderCloser struct {
   155  	R io.Reader
   156  	C io.Closer
   157  	N int64
   158  }
   159  
   160  // Read implements io.Reader
   161  func (l *LimitedReaderCloser) Read(p []byte) (n int, err error) {
   162  	if l.N <= 0 {
   163  		_ = l.C.Close()
   164  		return 0, io.EOF
   165  	}
   166  	if int64(len(p)) > l.N {
   167  		p = p[0:l.N]
   168  	}
   169  	n, err = l.R.Read(p)
   170  	l.N -= int64(n)
   171  	return
   172  }
   173  
   174  // Close implements io.Closer
   175  func (l *LimitedReaderCloser) Close() error {
   176  	return l.C.Close()
   177  }