Skip to content

Commit

Permalink
Merge pull request #25 from apiiro/tomer/escape-file-names
Browse files Browse the repository at this point in the history
Handle file names with special characters
  • Loading branch information
tomer-amir committed Feb 12, 2024
2 parents 7ca6906 + 8eeaa94 commit ae22951
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Tool to create a git revision snapshot for an existing repository clone.

```
NAME:
git-snap - 1.20 - Create a git revision snapshot for an existing repository clone. Symbolic link files will be omitted.
git-snap - 1.21 - 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]
Expand Down
36 changes: 27 additions & 9 deletions git/git.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package git

import (
"bufio"
"encoding/csv"
"errors"
"fmt"
"gitsnap/options"
Expand All @@ -11,6 +11,7 @@ import (
"log"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/avast/retry-go"
Expand Down Expand Up @@ -121,12 +122,15 @@ func loadFilePathsList(opts *options.Options, provider *repositoryProvider) erro
return fmt.Errorf("failed to read paths file from location: '%v', error: '%v'", opts.PathsFileLocation, err)
}

reader := csv.NewReader(file)
defer file.Close()
scanner := bufio.NewScanner(file)

for scanner.Scan() {
line := scanner.Text()
provider.fileListToSnap[line] = true
lines, err := reader.ReadAll()
if err != nil {
return fmt.Errorf("failed to read paths file from location: '%v', error: '%v'", opts.PathsFileLocation, err)
}
for i := range lines {
provider.fileListToSnap[lines[i][0]] = true
}
}
return nil
Expand Down Expand Up @@ -297,12 +301,14 @@ func isFileInList(provider *repositoryProvider, filePathToCheck string) bool {
return inFileList || len(provider.fileListToSnap) == 0
}

func addEntryToIndexFile(indexFile *os.File, name string, entry *object.TreeEntry) error {
func addEntryToIndexFile(indexFile *csv.Writer, 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()))
record := []string{name, entry.Hash.String(), strconv.FormatBool(entry.Mode.IsFile())}
err := indexFile.Write(record)
if err != nil {
return err
}
indexFile.Flush()
}
return nil
}
Expand All @@ -321,16 +327,23 @@ func (provider *repositoryProvider) snapshot(repository *git.Repository, commit
treeWalker := object.NewTreeWalker(tree, true, nil)
defer treeWalker.Close()

var indexOutputFile *os.File = nil
var indexOutputFile *csv.Writer = nil
if optionalIndexFilePath != "" && !dryRun {
locIndexOutputFile, err := os.Create(optionalIndexFilePath)
if err != nil {
return 0, fmt.Errorf("failed to create index file '%v': %v", optionalIndexFilePath, err)
}

csvWriter := csv.NewWriter(locIndexOutputFile)
csvWriter.Comma = '\t'
err = csvWriter.Write([]string{"Path", "BlobId", "IsFile"})
if err != nil {
return 0, fmt.Errorf("failed to write file headers '%v': %v", optionalIndexFilePath, err)
}

defer locIndexOutputFile.Close()

indexOutputFile = locIndexOutputFile
indexOutputFile = csvWriter
}

for {
Expand Down Expand Up @@ -383,6 +396,11 @@ func (provider *repositoryProvider) snapshot(repository *git.Repository, commit
return 0, fmt.Errorf("failed to iterate files of %v: %v", commit.Hash, err)
}
provider.verboseLog("iterated %v files for %v", count, commit.Hash)

if indexOutputFile != nil {
indexOutputFile.Flush()
}

return count, nil
}

Expand Down
15 changes: 14 additions & 1 deletion git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,21 @@ func (gitSuite *gitTestSuite) verifyIndexFile(

scanner := bufio.NewScanner(file)
fileCount := 0
i := 0
for scanner.Scan() {
fields := strings.Split(scanner.Text(), "\t")
i++
lineText := scanner.Text()
fields := strings.Split(lineText, "\t")

if i == 1 {
if len(fields) != 3 || fields[0] != "Path" || fields[1] != "BlobId" || fields[2] != "IsFile" {
gitSuite.Require().Fail("Index file header line is incorrect", lineText)
return
}

continue
}

fileStat, err := os.Stat(gitSuite.outputPath + "/" + fields[0])

if err == nil {
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.20"
const VERSION = "1.21"

func main() {
cli.AppHelpTemplate =
Expand Down

0 comments on commit ae22951

Please sign in to comment.