Commits

Patrick Mézard committed 8ccd510

gofmt: add --write option to rewrite instead of reporting

  • Participants
  • Parent commits 1ec6dab

Comments (0)

Files changed (3)

 
 and put it in /path/to/evolve.
 
+`gofmt` must be in $PATH (the extension gofmt bit has been kept from the Go
+fork and might be moved in another extension at some point).
+
 Finally, you need a valid codereview.appspot.com cookie file. To get one, use
 codereview extension "code-login" command once (you need to have a codereview
 account). The file is available in

File lib/codereview/codereview.py

 			ui.warn(errs)
 	return res
 
-def gofmtwc(ui, repo, ctx, files):
+def gofmtwc(ui, repo, ctx, files, write=False):
 	files = [f for f in files if f.endswith('.go') and f in ctx]
+	if not files:
+		return 0
+	opts = ['-l']
+	if write:
+		opts = ['-w']
 	try:
-		p = subprocess.Popen(['gofmt', '-l'] + files, close_fds=os.name=='posix',
+		p = subprocess.Popen(['gofmt'] + opts + 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
+		res = gofmtwc(ui, repo, ctx, files[:len(files)//2], write)
+		res = gofmtwc(ui, repo, ctx, files[len(files)//2:], write) or res
 		return res
 	res = 0
 	for line in p.stdout:
 @hgcommand('gofmt',
 	[('a', 'all', False, 'check all changeset Go files'),
 	 ('r', 'rev', '', 'changeset to check', 'REV'),
+	 ('w', 'write', False, 'rewrite files instead of checking'),
 	 ] + commands.walkopts,
 	'[-r REV] [FILE]...')
 def gofmt(ui, repo, *pats, **opts):
 
 	Use --all to run gofmt on all working copy or changeset files.
 
+	With --write, invalid files are rewritten instead of being reported.
+	It only works on the working copy, --rev is not supported.
+
 	Return 1 upon incorrectly formatted file.
 	"""
 	rev = opts.get('rev') or None
+	write = opts.get('write')
+	if write and rev is not None:
+		raise hg_util.Abort('--write can only be used on working copy')
 	ctx = scmutil.revsingle(repo, rev, default=None)
 	m = scmutil.match(ctx, pats, opts)
 	if opts.get('all'):
 		files = list(ctx.files())
 	files = [f for f in files if m(f)]
 	if rev is None:
-		return gofmtwc(ui, repo, ctx, files)
+		return gofmtwc(ui, repo, ctx, files, write)
 	return gofmtctx(ui, ctx, files, header='')
 
 def extsetup():

File tests/test-gofmt.t

   invalid4.go
   [1]
 
+With --write
+
+  $ hg gofmt -r . --write --all
+  abort: --write can only be used on working copy
+  [255]
+  $ hg gofmt --write --all
+  invalid2.go:4:13: expected '(', found 'IDENT' error
+  invalid2.go:5:3: expected ')', found 'return'
+  invalid2.go:6:1: expected declaration, found '}'
+  [1]
+  $ hg status
+  M invalid4.go
+  $ hg gofmt --all
+  invalid2.go:4:13: expected '(', found 'IDENT' error
+  invalid2.go:5:3: expected ')', found 'return'
+  invalid2.go:6:1: expected declaration, found '}'
+  [1]
+