github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/pipeline/revlist.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 package pipeline 7 8 import ( 9 "bufio" 10 "bytes" 11 "context" 12 "fmt" 13 "io" 14 "strings" 15 "sync" 16 17 "github.com/gitbundle/modules/git" 18 "github.com/gitbundle/modules/log" 19 ) 20 21 // RevListAllObjects runs rev-list --objects --all and writes to a pipewriter 22 func RevListAllObjects(ctx context.Context, revListWriter *io.PipeWriter, wg *sync.WaitGroup, basePath string, errChan chan<- error) { 23 defer wg.Done() 24 defer revListWriter.Close() 25 26 stderr := new(bytes.Buffer) 27 var errbuf strings.Builder 28 cmd := git.NewCommand(ctx, "rev-list", "--objects", "--all") 29 if err := cmd.Run(&git.RunOpts{ 30 Dir: basePath, 31 Stdout: revListWriter, 32 Stderr: stderr, 33 }); err != nil { 34 log.Error("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String()) 35 err = fmt.Errorf("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String()) 36 _ = revListWriter.CloseWithError(err) 37 errChan <- err 38 } 39 } 40 41 // RevListObjects run rev-list --objects from headSHA to baseSHA 42 func RevListObjects(ctx context.Context, revListWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath, headSHA, baseSHA string, errChan chan<- error) { 43 defer wg.Done() 44 defer revListWriter.Close() 45 stderr := new(bytes.Buffer) 46 var errbuf strings.Builder 47 cmd := git.NewCommand(ctx, "rev-list", "--objects", headSHA, "--not", baseSHA) 48 if err := cmd.Run(&git.RunOpts{ 49 Dir: tmpBasePath, 50 Stdout: revListWriter, 51 Stderr: stderr, 52 }); err != nil { 53 log.Error("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String()) 54 errChan <- fmt.Errorf("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String()) 55 } 56 } 57 58 // BlobsFromRevListObjects reads a RevListAllObjects and only selects blobs 59 func BlobsFromRevListObjects(revListReader *io.PipeReader, shasToCheckWriter *io.PipeWriter, wg *sync.WaitGroup) { 60 defer wg.Done() 61 defer revListReader.Close() 62 scanner := bufio.NewScanner(revListReader) 63 defer func() { 64 _ = shasToCheckWriter.CloseWithError(scanner.Err()) 65 }() 66 for scanner.Scan() { 67 line := scanner.Text() 68 if len(line) == 0 { 69 continue 70 } 71 fields := strings.Split(line, " ") 72 if len(fields) < 2 || len(fields[1]) == 0 { 73 continue 74 } 75 toWrite := []byte(fields[0] + "\n") 76 for len(toWrite) > 0 { 77 n, err := shasToCheckWriter.Write(toWrite) 78 if err != nil { 79 _ = revListReader.CloseWithError(err) 80 break 81 } 82 toWrite = toWrite[n:] 83 } 84 } 85 }