Skip to content

Commit

Permalink
Add --max.commits option
Browse files Browse the repository at this point in the history
  • Loading branch information
rcowham committed Sep 13, 2022
1 parent e8ee282 commit 157e62d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type GitParserOptions struct {
importDepot string
importPath string // After depot and branch
defaultBranch string
maxCommits int
}

type GitAction int
Expand Down Expand Up @@ -840,6 +841,7 @@ func (g *GitP4Transfer) GitParse(options GitParserOptions) chan GitCommit {
g.gitChan = make(chan GitCommit, 50)
var currCommit *GitCommit
var commitSize = 0
commitCount := 0

missingRenameLogged := false
missingDeleteLogged := false
Expand All @@ -854,6 +856,7 @@ func (g *GitP4Transfer) GitParse(options GitParserOptions) chan GitCommit {
go func() {
defer file.Close()
defer close(g.gitChan)
defer pool.StopAndWait()
for {
cmd, err := f.ReadCmd()
if err != nil {
Expand Down Expand Up @@ -883,6 +886,12 @@ func (g *GitP4Transfer) GitParse(options GitParserOptions) chan GitCommit {
case libfastimport.CmdCommitEnd:
commit := cmd.(libfastimport.CmdCommitEnd)
g.logger.Debugf("CommitEnd: %+v", commit)
commitCount += 1
if g.opts.maxCommits > 0 && commitCount >= g.opts.maxCommits {
g.logger.Infof("Processed %d commits", commitCount)
g.processCommit(currCommit)
return
}
case libfastimport.FileModify:
f := cmd.(libfastimport.FileModify)
g.logger.Debugf("FileModify: %+v", f)
Expand Down Expand Up @@ -1006,7 +1015,6 @@ func (g *GitP4Transfer) GitParse(options GitParserOptions) chan GitCommit {
}
}
g.processCommit(currCommit)
pool.StopAndWait()
}()

return g.gitChan
Expand Down Expand Up @@ -1054,6 +1062,10 @@ func main() {
"dump.archives",
"Saving the contained archive contents if --dump is specified.",
).Short('a').Bool()
maxCommits = kingpin.Flag(
"max.commits",
"Max no of commits to process.",
).Short('m').Int()
dryrun = kingpin.Flag(
"dryrun",
"Don't actually create archive files.",
Expand Down Expand Up @@ -1092,6 +1104,7 @@ func main() {
archiveRoot: *archive,
defaultBranch: *defaultBranch,
dryRun: *dryrun,
maxCommits: *maxCommits,
}

if *dump {
Expand Down
73 changes: 66 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,18 @@ func createLogger() *logrus.Logger {
return logger
}

func runTransferWithDump(t *testing.T, logger *logrus.Logger, output string) string {
func runTransferWithDump(t *testing.T, logger *logrus.Logger, output string, opts *GitParserOptions) string {
g := NewGitP4Transfer(logger)
g.testInput = output
p4t := MakeP4Test(t.TempDir())
opts := GitParserOptions{archiveRoot: p4t.serverRoot, importDepot: "import", defaultBranch: "main"}
commitChan := g.GitParse(opts)
if opts != nil {
opts.archiveRoot = p4t.serverRoot
opts.importDepot = "import"
opts.defaultBranch = "main"
} else {
opts = &GitParserOptions{archiveRoot: p4t.serverRoot, importDepot: "import", defaultBranch: "main"}
}
commitChan := g.GitParse(*opts)
commits := make([]GitCommit, 0)
// just read all commits and test them
for c := range commitChan {
Expand Down Expand Up @@ -198,7 +204,16 @@ func runTransfer(t *testing.T, logger *logrus.Logger) string {
if err != nil {
t.Errorf("ERROR: Failed to git export '%s': %v\n", output, err)
}
return runTransferWithDump(t, logger, output)
return runTransferWithDump(t, logger, output, nil)
}

func runTransferOpts(t *testing.T, logger *logrus.Logger, opts *GitParserOptions) string {
// fast-export with rename detection implemented
output, err := runCmd("git fast-export --all -M")
if err != nil {
t.Errorf("ERROR: Failed to git export '%s': %v\n", output, err)
}
return runTransferWithDump(t, logger, output, opts)
}

func TestAdd(t *testing.T) {
Expand Down Expand Up @@ -354,6 +369,50 @@ func TestAddEdit(t *testing.T) {
assert.Regexp(t, `lbrRev 1.4`, result)
}

func TestMaxCommits(t *testing.T) {
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("git add .")
runCmd("git commit -m initial")
srcContents2 := "contents\nappended\n"
writeToFile(src, srcContents2)
runCmd("git add .")
runCmd("git commit -m initial")

r := runTransferOpts(t, logger, &GitParserOptions{maxCommits: 1})
logger.Debugf("Server root: %s", r)

result, err := runCmd("p4 files //...")
assert.Equal(t, nil, err)
assert.Equal(t, "//import/main/src.txt#1 - add change 2 (text+C)\n", result)

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

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

result, err = runCmd("p4 fstat -Ob //import/main/src.txt")
assert.Equal(t, nil, err)
assert.Regexp(t, `headType text\+C`, result)
assert.Regexp(t, `lbrType text\+C`, result)
assert.Regexp(t, `lbrPath .*/1.2.gz`, result)

result, err = runCmd("p4 changes")
assert.Equal(t, nil, err)
assert.NotRegexp(t, `Change 4 on .* by git\-user@git\-client`, result)
assert.Regexp(t, `Change 2 on .* by git\-user@git\-client`, result)
}

func TestAddSameFile(t *testing.T) {
// Ensure single archive in git
logger := createLogger()
Expand Down Expand Up @@ -846,7 +905,7 @@ func TestRenameDir(t *testing.T) {
newOutput := strings.Join(newLines, "\n")
logger.Debugf("Changed output: %s", newOutput)

r := runTransferWithDump(t, logger, newOutput)
r := runTransferWithDump(t, logger, newOutput, nil)
logger.Debugf("Server root: %s", r)

result, err := runCmd("p4 files //...@2")
Expand Down Expand Up @@ -923,7 +982,7 @@ func TestRenameDirWithDelete(t *testing.T) {
newOutput := strings.Join(newLines, "\n")
logger.Debugf("Changed output: %s", newOutput)

r := runTransferWithDump(t, logger, newOutput)
r := runTransferWithDump(t, logger, newOutput, nil)
logger.Debugf("Server root: %s", r)

result, err := runCmd("p4 files //...@2")
Expand Down Expand Up @@ -988,7 +1047,7 @@ func TestDeleteDir(t *testing.T) {
newOutput := strings.Join(newLines, "\n")
logger.Debugf("Changed output: %s", newOutput)

r := runTransferWithDump(t, logger, newOutput)
r := runTransferWithDump(t, logger, newOutput, nil)
logger.Debugf("Server root: %s", r)

result, err := runCmd("p4 files //...@2")
Expand Down

0 comments on commit 157e62d

Please sign in to comment.