From c2bdc2f97033d51d8964e0ef3d8479e7eab047c8 Mon Sep 17 00:00:00 2001 From: Robert Cowham Date: Mon, 3 Jul 2023 16:16:49 +0100 Subject: [PATCH] Fix problem with binary files already auto-detected as binary+F --- main.go | 14 +++++++++++++- main_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 6248750..7b752fc 100644 --- a/main.go +++ b/main.go @@ -1095,7 +1095,19 @@ func (g *GitP4Transfer) updateDepotRevs(opts GitParserOptions, gf *GitFile, chgN // Determine filetype based on Typemap (if specified) for _, r := range opts.config.ReTypeMaps { if r.RePath.MatchString(gf.p4.depotFile) { - gf.fileType = r.Filetype + if r.Filetype == journal.Binary { + if gf.baseFileType == journal.Binary || gf.baseFileType == journal.UBinary { + gf.fileType = gf.baseFileType + } else { + gf.fileType = journal.Binary + } + } else { // Text + if gf.baseFileType == journal.UText || gf.baseFileType == journal.CText { + gf.fileType = gf.baseFileType + } else { + gf.fileType = journal.CText + } + } gf.baseFileType = gf.fileType } } diff --git a/main_test.go b/main_test.go index 10414b0..4018af0 100644 --- a/main_test.go +++ b/main_test.go @@ -1431,6 +1431,48 @@ func TestAddBinary(t *testing.T) { assert.Regexp(t, `(?m)lbrPath .*/1.2$`, result) } +func TestAddBinaryAndFileType(t *testing.T) { + // Add a binary file and set the filetype to be binary, although file will be auto-detected as binary+F + logger := createLogger() + logger.Debugf("======== Test: %s", t.Name()) + + d := createGitRepo(t) + os.Chdir(d) + logger.Debugf("Git repo: %s", d) + + src := "src.txt" + srcContents1 := "contents\n" + writeToFile(src, srcContents1) + runCmd("gzip " + src) + runCmd("git add .") + runCmd("git commit -m initial") + + c := &config.Config{ + ImportDepot: "import", DefaultBranch: "main", + ReTypeMaps: make([]config.RegexpTypeMap, 0), + } + rePath := regexp.MustCompile("//.*.gz$") // Go version of typemap + c.ReTypeMaps = append(c.ReTypeMaps, config.RegexpTypeMap{Filetype: journal.Binary, RePath: rePath}) + + opts := &GitParserOptions{config: c} + + runTransferOpts(t, logger, opts) + + result, err := runCmd("p4 files //...@2") + assert.Equal(t, nil, err) + assert.Equal(t, "//import/main/src.txt.gz#1 - add change 2 (binary+F)\n", result) + + result, err = runCmd("p4 verify -qu //...") + assert.Equal(t, "", fmt.Sprint(err)) + assert.Equal(t, "", result) + + result, err = runCmd("p4 fstat -Ob //import/main/src.txt.gz#1") + assert.Equal(t, nil, err) + assert.Regexp(t, `headType binary\+F`, result) + assert.Regexp(t, `lbrType binary\+F`, result) + assert.Regexp(t, `(?m)lbrPath .*/1.2$`, result) +} + func TestAddCRLF1(t *testing.T) { // Test where CRLF should be converted to LF - but only for text files! logger := createLogger()