Skip to content

Commit

Permalink
Merge pull request #22 from apiiro/tomer/file-paths-file
Browse files Browse the repository at this point in the history
Allow sending the location of a file with a list of files to snap
  • Loading branch information
tomer-amir authored Jan 21, 2024
2 parents a5430a1 + 0d8e851 commit b973616
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ build-osx:
ifneq ($(shell uname -s),Darwin)
$(error this makefile assumes you're building from mac env)
endif
GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -o bin/$(BINARY_NAME)-$(shell $(GOCMD) run . --version | cut -d" " -f 3)-osx .
GO111MODULE=on CGO_ENABLED=0 GOARCH=amd64 $(GOCMD) build -o bin/$(BINARY_NAME)-$(shell $(GOCMD) run . --version | cut -d" " -f 3)-osx .

build-linux:
docker run --rm -v $(shell pwd):/app -w /app golang:1.20-alpine /bin/sh -c "GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -o bin/$(BINARY_NAME)-$(shell $(GOCMD) run . --version | cut -d" " -f 3)-linux ."
docker run --rm -v $(shell pwd):/app -w /app --platform=linux/amd64 golang:1.20-alpine3.18 /bin/sh -c "GO111MODULE=on CGO_ENABLED=0 GOARCH=amd64 $(GOCMD) build -o bin/$(BINARY_NAME)-$(shell $(GOCMD) run . --version | cut -d" " -f 3)-linux ."

clean:
rm -rf ./bin
Expand Down
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@ Tool to create a git revision snapshot for an existing repository clone.

```
NAME:
git-snap - 1.17 - Create a git revision snapshot for an existing repository clone. Symbolic link files will be omitted.
git-snap - 1.18 - Create a git revision snapshot for an existing repository clone. Symbolic link files will be omitted.
USAGE:
git-snap --src value --rev value --out value [optional flags]
git-snap --src value --rev value --out value [optional flags]
OPTIONS:
--src value, -s value path to existing git clone as source directory, may contain no more than .git directory, current git state doesn't affect the command
--rev value, -r value commit-ish Revision
--index value, -x value Create index file listing file paths and their blob IDs
--index-only, --xo Create index only - Don't checkout any files (default: false)
--out value, -o value output directory. will be created if does not exist
--include value, -i value patterns of file paths to include, comma delimited, may contain any glob pattern
--exclude value, -e value patterns of file paths to exclude, comma delimited, may contain any glob pattern
--verbose, --vv verbose logging (default: false)
--text-only include only text files (default: false)
--hash-markers create also hint files mirroring the hash of original files at <path>.hash (default: false)
--ignore-case ignore case when checking path against inclusion patterns (default: false)
--max-size value maximal file size, in MB (default: 6)
--no-double-check disable files discrepancy double check (default: false)
--include-noise-dirs don't filter out noisy directory names in paths (bin, node_modules etc) (default: false)
--help, -h show help (default: false)
--version, -v print the version (default: false)
--src value, -s value path to existing git clone as source directory, may contain no more than .git directory, current git state doesn't affect the command
--rev value, -r value commit-ish Revision
--index value, -x value Create index file listing file paths and their blob IDs
--index-only, --xo Create index only - Don't checkout any files (default: false)
--out value, -o value output directory. will be created if does not exist
--include value, -i value patterns of file paths to include, comma delimited, may contain any glob pattern
--exclude value, -e value patterns of file paths to exclude, comma delimited, may contain any glob pattern
--verbose, --vv verbose logging (default: false)
--text-only include only text files (default: false)
--hash-markers create also hint files mirroring the hash of original files at <path>.hash (default: false)
--ignore-case ignore case when checking path against inclusion patterns (default: false)
--max-size value maximal file size, in MB (default: 6)
--no-double-check disable files discrepancy double check (default: false)
--include-noise-dirs don't filter out noisy directory names in paths (bin, node_modules etc) (default: false)
--paths-file-location value, --pl value a location of a text file with all the paths to snap (one path per line)
--help, -h show help (default: false)
--version, -v print the version (default: false)
EXIT CODES:
0 Success
Expand Down
38 changes: 37 additions & 1 deletion git/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"bufio"
"errors"
"fmt"
"gitsnap/options"
Expand Down Expand Up @@ -30,13 +31,20 @@ type repositoryProvider struct {
repository *git.Repository
includePatterns []glob.Glob
excludePatterns []glob.Glob
fileListToSnap map[string]bool
opts *options.Options
}

func Snapshot(opts *options.Options) (err error) {

provider := &repositoryProvider{
opts: opts,
opts: opts,
fileListToSnap: map[string]bool{},
}

err = loadFilePathsList(opts, provider)
if err != nil {
return err
}

provider.includePatterns, err = provider.compileGlobs(opts.IncludePatterns, "include")
Expand Down Expand Up @@ -106,6 +114,24 @@ func Snapshot(opts *options.Options) (err error) {
return nil
}

func loadFilePathsList(opts *options.Options, provider *repositoryProvider) error {
if opts.PathsFileLocation != "" {
file, err := os.Open(opts.PathsFileLocation)
if err != nil {
return fmt.Errorf("failed to read paths file from location: '%v', error: '%v'", opts.PathsFileLocation, err)
}

defer file.Close()
scanner := bufio.NewScanner(file)

for scanner.Scan() {
line := scanner.Text()
provider.fileListToSnap[line] = true
}
}
return nil
}

func (provider *repositoryProvider) getCommit(commitish string) (*object.Commit, error) {

hash, err := provider.repository.ResolveRevision(plumbing.Revision(commitish))
Expand Down Expand Up @@ -174,6 +200,11 @@ func (provider *repositoryProvider) dumpFile(repository *git.Repository, name st
filePathToCheck = strings.ToLower(filePathToCheck)
}

if !isFileInList(provider, filePathToCheck) {
provider.verboseLog("--- skipping '%v' - not matching file list", filePath)
return nil, false
}

skip := true
hasIncludePatterns := len(provider.includePatterns) > 0
if hasIncludePatterns && !matches(filePathToCheck, provider.includePatterns) {
Expand Down Expand Up @@ -261,6 +292,11 @@ func (provider *repositoryProvider) dumpFile(repository *git.Repository, name st
return nil, true
}

func isFileInList(provider *repositoryProvider, filePathToCheck string) bool {
_, inFileList := provider.fileListToSnap[filePathToCheck]
return inFileList || len(provider.fileListToSnap) == 0
}

func addEntryToIndexFile(indexFile *os.File, name string, entry *object.TreeEntry) error {
if indexFile != nil {
_, err := indexFile.WriteString(fmt.Sprintf("%v\t%v\t%v\n", name, entry.Hash.String(), entry.Mode.IsFile()))
Expand Down
40 changes: 40 additions & 0 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,43 @@ func (gitSuite *gitTestSuite) TestSnapshotWithIndexPath() {
gitSuite.Nil(err)
gitSuite.verifyIndexFile(40, indexFilePath)
}

func (gitSuite *gitTestSuite) TestSnapshotWithPathsFile() {
filesDir, err := ioutil.TempDir("", "")
if err != nil {
panic(err)
}

filePath := filepath.Join(filesDir, "file.txt")

file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)

if err != nil {
gitSuite.Nil(err)
return
}

data := []byte("src/main/java/com/dchealth/VO/DataElementFormat.java")
_, err = file.Write(data)

if err != nil {
err = fmt.Errorf("Failed to write paths file: '%v'", err)
fmt.Println(err)
gitSuite.Nil(err)
return
}

file.Close()

err = Snapshot(&options.Options{
ClonePath: gitSuite.clonePath,
Revision: "2ca742044ba451d00c6854a465fdd4280d9ad1f5",
OutputPath: gitSuite.outputPath,
IncludePatterns: []string{},
ExcludePatterns: []string{},
PathsFileLocation: filePath,
})

gitSuite.Nil(err)
gitSuite.verifyOutputPath(7, 1, 1696, 1696)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module gitsnap

go 1.16
go 1.14

require (
github.com/avast/retry-go v3.0.0+incompatible
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/urfave/cli/v2"
)

const VERSION = "1.17"
const VERSION = "1.18"

func main() {
cli.AppHelpTemplate =
Expand Down
8 changes: 8 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ var Flags = []cli.Flag{
Usage: "don't filter out noisy directory names in paths (bin, node_modules etc)",
Required: false,
},
&cli.StringFlag{
Name: "paths-file-location",
Aliases: []string{"pl"},
Usage: "a location of a text file with all the paths to snap (one path per line)",
Required: false,
},
}

type Options struct {
Expand All @@ -116,6 +122,7 @@ type Options struct {
MaxFileSizeBytes int64
SkipDoubleCheck bool
IncludeNoiseDirs bool
PathsFileLocation string
}

func splitListFlag(flag string) []string {
Expand Down Expand Up @@ -162,6 +169,7 @@ func ParseOptions(c *cli.Context) (*Options, error) {
IncludeNoiseDirs: c.Bool("include-noise-dirs"),
OptionalIndexFilePath: c.String("index"),
IndexOnly: c.Bool("index-only"),
PathsFileLocation: c.String("paths-file-location"),
}

err := validateDirectory(opts.ClonePath, false)
Expand Down

0 comments on commit b973616

Please sign in to comment.