Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

auto format code on commit

Jan Bom Trap September 30, 2019

Hi i'm fairly new at git and bitbucket, and i've tired to find a way to format code before it's beeing merged with origin.  

the issue i'm trying to solve is that we constantly get merge conflicts, that aren't real conflict, due to developers using their ovn code formatting templates.

i've tried to find examples through google and forums that either solve my issue or point me in the right direction but with no luck.

could i use hooks or something similar to format the code before merge? or should the problem be solved elsewhere

 

regards jan

 

2 answers

1 accepted

0 votes
Answer accepted
Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 30, 2019

Hi Jan Bom Trap and Welcome,

IMHO this should be fixed on the developer IDE side, not sure what are you using so the proposed solution might or not be implemented depending of the IDE features.

My team are used to code using Eclipse IDE, all of us use a prefefined Code Formatting Template and also we have configure the IDE to auto-formatting the code when saved the file.

On Eclipse IDE see:

  • Preferences >> Java >> Editor >> Save Actions -- enable 'Format all lines'
  • Preferences >> Java >> Code Style >>Formatter -- ensure all members use the same Profile, you can also export / import your own.


In this way, only changes in the code are reflected in Pull Request.

Hope this helps.
Regards

Jan Bom Trap September 30, 2019

thanks for your respons. we primarily use toad for oracle as an IDE, manily oracle database programming and oracle forms.

we've discussed if its possible to auto format all code(or files with specific extensions) prior to merge via a hook in bitbucket. if it is possible, then we wouldn't have to ensure that all developers use the exact same formatting profile. bitbucket would enforce the company profile on all code prior to merge. the developers could then use whatever profile they prefer locally.

but as far as i can understand from your answer, it's not the "usual way" that this is done?

Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 30, 2019

At least from my experience, the team should adhere to the same Code Style to avoid this kind of problem.

Apart from that, while developing a Java code, we also use some maven plugins to force that code is correctly formatted and report the problems found.

See Apache Maven Checkstyle Plugin for further information, not idea if you can use something similar with Oracle "code"

Jan Bom Trap September 30, 2019

thank for your time, it's been most helpful

Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 30, 2019

You are welcome :)

Did my answer solves your problem?

If so, consider to mark the proposed solution as an 'Accepted answer'. This will help other people with the same or a quite similar problem to find a solution in a easy and faster way.

In addition, by doing this the post will automatically gain the "Solved:" prefix in search results to indicate other people that no further support is needed.

Kind Regards.

0 votes
timhoustontx June 2, 2020

Similar question... our build fails when code is wrongly formatted.  

It seems like putting the cart in front of the horse:  formatting can be automated.  why not format automatically?  Instead of asking every developer to do it, do it in one place.

checkin should be able to reformat code.  it should be possible to include/exclude because some code should be excluded--examples are generated code.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events