如何适应多种编码风格? (git vs. IDE) [英] How to accommodate multiple coding styles? (git vs. IDE)

查看:146
本文介绍了如何适应多种编码风格? (git vs. IDE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与使用多个IDE的用户(注1))合作开发具有不同代码样式偏好的git-source,maven管理的Java项目。



是否有工具或IDE配置,允许使用 style-1 查看和编辑代码,但是使用 style-2



我的研究指出我是不,而是将 git hooks Checkstyle / jrefactory 可能是可能的。



所以如果'否'以上,是否有一个工具/流程将执行下面的 TBD流程操作?



结帐User1的流程将是:


  1. git pull

  2. TBD流程将代码格式化为User1 style-1

  3. User1在其首选的IDE中使用样式-1 设置

User1的提交工作流将是:


  1. User1准备提交/推送代码

  2. TBD流程将代码格式化为标准格式 style-standard

  3. git push



注意1:多个IDE = Eclipse,IntelliJ,Netbeans。



注2:我的问题不同来自这个问题,我想集中在一个IDE相关的解决方案,因为强制少数标准歧视用户可能是一个更有效的解决方案。



注3:承认不应该以最佳做法为由。但是,如果您认为我们的IDE和SCMs有更多的灵活性,那么这个问题是为了探索这些解决方案。

解决方案

首先,你真的不应该这样做。 Codestyle战争对任何项目都是不利的,最好决定每个人都必须使用的一个码流。配置IDE可以在每个文件保存中自动应用指定的代码,因此开发人员不必在目标编码本身中编写代码,他们可以让IDE为它们做到这一点。诚然,这并不能解决这个事实,他们不得不在不熟悉的代码中阅读代码,但是比不可见的自动代码更改更安全;这是错误的主要来源。



也许可以使用Eclipse的代码格式化程序从命令行应用不同的代码。您必须设置git钩子,确保每个人都可以使用Eclipse,并为其首选的编程提供正确的配置文件。您需要钩子来进行后检查和预先提交,一个用于设置用户的编码,另一个用于中央编码。要进一步,您可以使用索引添加格式化的代码,使其不包含 git diff 中的样式差异(尽管它们将显示在 git diff --staged )。



再次,你不应该这样做


I am collaborating on a git-sourced, maven-managed Java project with differing code styling preferences with users using multiple IDE's (note 1).

Is there a tool or IDE configuration that will allow code to be viewed and edited using style-1, but committed to SCM using style-2?

My research points me to 'no', but a solution combining git hooks and Checkstyle/jrefactory might be possible.

So if 'no' to above, is there a tool/process that will perform the TBD process actions below?

The checkout process flow for User1 would be:

  1. git pull
  2. TBD process formats code to User1 style-1
  3. User1 works in their preferred IDE with style-1 settings

The commit workflow for User1 would be:

  1. User1 is ready to commit/push code
  2. TBD process formats code to standard format style-standard
  3. git push

Note 1: multiple IDE's = Eclipse, IntelliJ, Netbeans.

Note 2: My question differs from this question in that I'd like to focus on an IDE-related solution, since forcing the minority of standards-divergent users is probably a more efficient solution.

Note 3: Acknowledging that this shouldn't be done for best-practices-reasons. However, if you grant that it's time expect more flexibility from our IDEs and SCMs, this question is intended to explore those solutions.

解决方案

First of all, you really shouldn't do that. Codestyle wars are bad for any project, and it is best to decide upon one codestyle that everybody must use. It is simple to configure IDEs to automatically apply the specified codestyle at every filesave, so the developers don't have to write code in the target codestyle themselves, they can let the IDE do that for them. True, this doesn't solve the fact that they'll have to read code in a codestyle they don't yet like, but it's a lot safer than having invisible automatic code changes; that's a major source of bugs.

Maybe you can use Eclipse's code formatter from the command line to apply a different codestyle. You'd have to set up git hooks, make sure everybody has Eclipse available, and provide the proper configuration files for their preferred codestyle. You'd need hooks both for post-checkout and pre-commit, one to set up the user's codestyle, the other to commit in the central codestyle. To go one step further, you can play with the index to add the formatted code so that it doesn't include style differences in git diff (although they will show up in git diff --staged).

Again, you shouldn't do that.

这篇关于如何适应多种编码风格? (git vs. IDE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆