Commits

Patrick Mézard  committed 1ec6dab

gofmt: run gofmt on working copy when --rev is omitted

On my Win7 cygwin setup it reduces "hg gofmt -a" on go repository from 1mn14s
to 6s.

  • Participants
  • Parent commits 4a7793c

Comments (0)

Files changed (2)

File lib/codereview/codereview.py

 	print >>sys.stderr, "You are running Python " + sys.version
 	sys.exit(2)
 
+import errno
 import json
 import os
 import random
 		if not self.files:
 			ui.warn("no files in change list\n")
 		if ui.configbool("codereview", "force_gofmt", True):
-			if gofmtfiles(ui, ctx, list(ctx.files())):
+			if gofmtctx(ui, ctx, list(ctx.files())):
 				raise hg_util.Abort('gofmt failed, stopping changelist upload')
 		set_status("uploading CL metadata + diffs")
 		baserev = str(ctx.p1())
 		out = 'gofmt failed for unknown reason'
 	return out
 
-def gofmtfiles(ui, ctx, files, header=None):
+def gofmtctx(ui, ctx, files, header=None):
 	if header is None:
 		header = 'error: gofmt failed:\n'
 	res = 0
 			ui.warn(errs)
 	return res
 
+def gofmtwc(ui, repo, ctx, files):
+	files = [f for f in files if f.endswith('.go') and f in ctx]
+	try:
+		p = subprocess.Popen(['gofmt', '-l'] + files, close_fds=os.name=='posix',
+				stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
+				cwd=repo.root)
+	except OSError, e:
+		if e.errno != errno.E2BIG or len(files) < 2:
+			raise
+		# command is too long, split the load and retry
+		res = gofmtwc(ui, repo, ctx, files[:len(files)//2])
+		res = gofmtwc(ui, repo, ctx, files[len(files)//2:]) or res
+		return res
+	res = 0
+	for line in p.stdout:
+		ui.warn(line)
+		if not res and line.strip():
+			res = 1
+	if p.wait():
+		res = 1
+	return res
+
 #######################################################################
 # Mercurial helper functions.
 # Read http://mercurial.selenic.com/wiki/MercurialApi before writing any of these.
 
 	Return 1 upon incorrectly formatted file.
 	"""
-	rev = opts.get('rev')
+	rev = opts.get('rev') or None
 	ctx = scmutil.revsingle(repo, rev, default=None)
 	m = scmutil.match(ctx, pats, opts)
 	if opts.get('all'):
 	else:
 		files = list(ctx.files())
 	files = [f for f in files if m(f)]
-	return gofmtfiles(ui, ctx, files, header='')
+	if rev is None:
+		return gofmtwc(ui, repo, ctx, files)
+	return gofmtctx(ui, ctx, files, header='')
 
 def extsetup():
 	try:

File tests/test-gofmt.t

   invalid4.go
   [1]
 
+From working directory sub-directory
+
+  $ mkdir sub
+  $ cd sub
+  $ hg gofmt
+  invalid4.go
+  [1]
+  $ cd ..
+
 Again as a changeset
 
   $ hg ci -m change