Skip to content

Commit

Permalink
Handle rename on a branch of non-edited file. Added debug info.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcowham committed Sep 13, 2022
1 parent 157e62d commit e2f035c
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 7 deletions.
15 changes: 15 additions & 0 deletions journal/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ const (
Binary FileType = 0x00000103 // binary
)

func (f FileType) String() string {
switch f {
case UText:
return "UText"
case CText:
return "CText"
case UBinary:
return "UBinary"
case Binary:
return "Binary"
default:
return "Unknown"
}
}

type FileAction int

const (
Expand Down
50 changes: 43 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const (
rename
)

func (a GitAction) String() string {
return [...]string{"Unknown", "Modify", "Delete", "Copy", "Rename"}[a]
}

// Node - tree structure to record directory contents for directory renames
type Node struct {
name string
Expand Down Expand Up @@ -258,7 +262,7 @@ func (m *BlobFileMatcher) updateDuplicateGitFile(gf *GitFile) {
}
gf.lbrFile = origGF.lbrFile
gf.lbrRev = origGF.lbrRev
gf.logger.Debugf("Duplicate of: %s %d", gf.lbrFile, gf.lbrRev)
gf.logger.Debugf("Duplicate file %s %d of: %s %d", gf.depotFile, gf.rev, gf.lbrFile, gf.lbrRev)
}

// GitFile - A git file record - modify/delete/copy/move
Expand All @@ -284,6 +288,7 @@ type GitFile struct {
compressed bool
blob *GitBlob
logger *logrus.Logger
commit *GitCommit
}

func newGitFile(gf *GitFile) *GitFile {
Expand Down Expand Up @@ -359,6 +364,7 @@ func (gf *GitFile) getDepotPath(opts GitParserOptions, branch string, name strin
}

func (gf *GitFile) setDepotPaths(opts GitParserOptions, gc *GitCommit) {
gf.commit = gc
gf.depotFile = gf.getDepotPath(opts, gc.branch, gf.name)
if gf.srcName != "" {
gf.srcDepotFile = gf.getDepotPath(opts, gc.branch, gf.srcName)
Expand Down Expand Up @@ -397,7 +403,7 @@ func (b *GitBlob) setCompressionDetails() {
}
}

// Sets compression option and binary/text
// Sets p4 action
func (gf *GitFile) updateFileDetails() {
switch gf.action {
case delete:
Expand Down Expand Up @@ -661,7 +667,7 @@ func (g *GitP4Transfer) isSrcDeletedFile(gf *GitFile) bool {
}

// Maintain a list of latest revision counters indexed by depotFile and set lbrArchive/Rev
func (g *GitP4Transfer) updateDepotRevs(gf *GitFile, chgNo int) {
func (g *GitP4Transfer) updateDepotRevs(opts GitParserOptions, gf *GitFile, chgNo int) {
prevAction := unknown
if _, ok := g.depotFileRevs[gf.depotFile]; !ok {
g.depotFileRevs[gf.depotFile] = &RevChange{rev: 0, chgNo: chgNo, lbrRev: chgNo,
Expand All @@ -679,7 +685,6 @@ func (g *GitP4Transfer) updateDepotRevs(gf *GitFile, chgNo int) {
g.depotFileRevs[gf.depotFile].lbrFile = gf.depotFile
gf.lbrRev = chgNo
gf.lbrFile = gf.depotFile
isRename := (gf.action == rename)
gf.rev = g.depotFileRevs[gf.depotFile].rev
if gf.action == modify {
// modify defaults to edit, except when first rev or previously deleted
Expand All @@ -694,6 +699,12 @@ func (g *GitP4Transfer) updateDepotRevs(gf *GitFile, chgNo int) {
}
if gf.srcName == "" { // Simple modify or delete
g.recordDepotFileType(gf)
g.logger.Debugf("depotFile: %s, rev %d, action %v, lbrFile %s, lbrRev %d, filetype %v",
gf.depotFile, g.depotFileRevs[gf.depotFile].rev,
g.depotFileRevs[gf.depotFile].action,
g.depotFileRevs[gf.depotFile].lbrFile,
g.depotFileRevs[gf.depotFile].lbrRev,
gf.fileType)
return
}
if gf.action != delete {
Expand All @@ -706,15 +717,40 @@ func (g *GitP4Transfer) updateDepotRevs(gf *GitFile, chgNo int) {
gf.srcDepotFile = ""
gf.srcName = ""
gf.isMerge = false
} else if gf.action == rename {
g.logger.Debugf("Rename of branched file: '%s' <- '%s'", gf.depotFile, gf.srcDepotFile)
// Create a record for the source of the rename referring to its branched source
depotPathOrig := gf.getDepotPath(opts, gf.commit.prevBranch, gf.srcName)
if _, ok := g.depotFileRevs[depotPathOrig]; !ok {
g.logger.Errorf("Failed to find original file: '%s'", depotPathOrig)
} else {
lbrFileOrig := g.depotFileRevs[depotPathOrig].lbrFile
lbrRevOrig := g.depotFileRevs[depotPathOrig].lbrRev
g.depotFileRevs[gf.srcDepotFile] = &RevChange{rev: 1, chgNo: chgNo, lbrRev: lbrRevOrig,
lbrFile: lbrFileOrig, action: delete}
gf.srcRev = g.depotFileRevs[gf.srcDepotFile].rev
gf.lbrFile = g.depotFileRevs[gf.srcDepotFile].lbrFile
gf.lbrRev = g.depotFileRevs[gf.srcDepotFile].lbrRev
gf.fileType = g.getDepotFileTypes(depotPathOrig, g.depotFileRevs[depotPathOrig].rev)
g.depotFileRevs[gf.depotFile].lbrRev = gf.lbrRev
g.depotFileRevs[gf.depotFile].lbrFile = gf.lbrFile
g.recordDepotFileType(gf)
}
} else {
// A copy or branch without a source file just becomes new file added on branch
g.logger.Warnf("Copy/branch becomes add: '%s' '%s'", gf.depotFile, gf.srcDepotFile)
g.logger.Warnf("Copy/branch becomes add: '%s' <- '%s'", gf.depotFile, gf.srcDepotFile)
gf.srcDepotFile = ""
gf.srcName = ""
gf.isBranch = false
gf.isMerge = false
}
g.recordDepotFileType(gf)
g.logger.Debugf("depotFile: %s, rev %d, action %v, lbrFile %s, lbrRev %d, filetype %v",
gf.depotFile, g.depotFileRevs[gf.depotFile].rev,
g.depotFileRevs[gf.depotFile].action,
g.depotFileRevs[gf.depotFile].lbrFile,
g.depotFileRevs[gf.depotFile].lbrRev,
gf.fileType)
return
}
if gf.action == delete { // merge of delete
Expand All @@ -723,7 +759,7 @@ func (g *GitP4Transfer) updateDepotRevs(gf *GitFile, chgNo int) {
gf.lbrFile = g.depotFileRevs[gf.srcDepotFile].lbrFile
g.depotFileRevs[gf.depotFile].lbrRev = gf.lbrRev
g.depotFileRevs[gf.depotFile].lbrFile = gf.lbrFile
} else if isRename { // Rename means old file is being deleted
} else if gf.action == rename { // Rename means old file is being deleted
g.depotFileRevs[gf.srcDepotFile].rev += 1
g.depotFileRevs[gf.srcDepotFile].action = delete
gf.srcRev = g.depotFileRevs[gf.srcDepotFile].rev
Expand Down Expand Up @@ -813,7 +849,7 @@ func (g *GitP4Transfer) processCommit(currCommit *GitCommit) {
for i := range currCommit.files {
currCommit.files[i].setDepotPaths(g.opts, currCommit)
currCommit.files[i].updateFileDetails()
g.updateDepotRevs(currCommit.files[i], currCommit.commit.Mark)
g.updateDepotRevs(g.opts, currCommit.files[i], currCommit.commit.Mark)
}
g.gitChan <- *currCommit
}
Expand Down
68 changes: 68 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,74 @@ func TestBranch2(t *testing.T) {

}

func TestBranchRename(t *testing.T) {
// Branch and add new file which is then renamed
logger := createLogger()
logger.Debugf("======== Test: %s", t.Name())

d := createGitRepo(t)
os.Chdir(d)
logger.Debugf("Git repo: %s", d)

file1 := "file1.txt"
file2 := "file2.txt"
file3 := "file3.txt"
contents1 := ""
contents2 := "new"
writeToFile(file1, contents1)
writeToFile(file2, contents2)
runCmd("git add .")
runCmd("git commit -m initial")
runCmd("git switch -c dev")
runCmd(fmt.Sprintf("mv %s %s", file2, file3))
runCmd("git add .")
runCmd("git commit -m 'renamed on dev'")

r := runTransfer(t, logger)
logger.Debugf("Server root: %s", r)

result, err := runCmd("p4 files //...")
assert.Equal(t, nil, err)
assert.Equal(t, `//import/dev/file2.txt#1 - delete change 4 (text+C)
//import/dev/file3.txt#1 - add change 4 (text+C)
//import/main/file1.txt#1 - add change 3 (text+C)
//import/main/file2.txt#1 - add change 3 (text+C)
`,
result)

result, err = runCmd("p4 verify -qu //...")
assert.Equal(t, "", result)
assert.Equal(t, "<nil>", fmt.Sprint(err))

result, err = runCmd("p4 print -q //import/main/file1.txt#1")
assert.Equal(t, nil, err)
assert.Equal(t, contents1, result)

result, err = runCmd("p4 print -q //import/dev/file3.txt#1")
assert.Equal(t, nil, err)
assert.Equal(t, contents2, result)

result, err = runCmd("p4 fstat -Ob //import/dev/file3.txt#1")
assert.Equal(t, nil, err)
assert.Regexp(t, `headType text\+C`, result)
assert.Regexp(t, `lbrType text\+C`, result)
assert.Regexp(t, `lbrFile //import/main/file2.txt`, result)
assert.Regexp(t, `(?m)lbrPath .*/1.3.gz$`, result)

result, err = runCmd("p4 filelog //import/dev/file2.txt#1")
assert.Equal(t, nil, err)
assert.Regexp(t, `//import/dev/file2.txt`, result)
assert.Regexp(t, `\.\.\. #1 change 4 delete on .* by git-user@git-client.*
\.\.\. \.\.\. branch into //import/dev/file3.txt#1`, result)

result, err = runCmd("p4 filelog //import/dev/file3.txt#1")
assert.Equal(t, nil, err)
assert.Regexp(t, `//import/dev/file3.txt`, result)
assert.Regexp(t, `\.\.\. #1 change 4 add on .* by git-user@git-client.*
\.\.\. \.\.\. branch from //import/dev/file2.txt#1`, result)

}

func TestBranchMerge(t *testing.T) {
// Merge branches
logger := createLogger()
Expand Down

0 comments on commit e2f035c

Please sign in to comment.