Peeking Inside a Software Engineer's Git Toolbox
March 28, 2024
Cheryl Craig
CGD AMP and CSEG Software Engineer
This material is based upon work supported by the National Center for Atmospheric Research, which is a major facility sponsored by the National Science Foundation under Cooperative Agreement No. 1852977.
What is Git?
Git is the tool that scientists and software engineers use to keep track of CESM
What is my favorite tool for git?
diffmerge
This tool is useful for :
Why do I need a tool?
How many times has this happened to you?
So what do you do?
git status
What do you see?
Depending on how prolific you were you may see something like this:
Changes to be committed:
modified: Externals_CAM.cfg
modified: bld/build-namelist
modified: bld/configure
modified: bld/namelist_files/namelist_defaults_cam.xml
modified: bld/namelist_files/namelist_definition.xml
modified: cime_config/testdefs/testlist_cam.xml
modified: doc/ChangeLog
modified: src/chemistry/modal_aero/modal_aero_convproc.F90
modified: src/physics/cam/cloud_fraction.F90
modified: src/physics/cam/clubb_intr.F90
modified: src/physics/cam/convect_shallow.F90
modified: src/physics/cam/macrop_driver.F90
modified: src/physics/cam/rk_stratiform.F90
Wow - I made more changes than I remember - what are they?
Now you need to see what you changed. So you type:
git diff
That is painful as you have pages of output like:
diff --cc src/physics/cam/zm_conv_intr.F90
index d5da099b,d559ce4b..00000000
--- a/src/physics/cam/zm_conv_intr.F90
+++ b/src/physics/cam/zm_conv_intr.F90
@@@ -8,11 -8,13 +8,19 @@@ module zm_conv_int
! January 2010 modified by J. Kay to add COSP simulator fields to physics buffer
!---------------------------------------------------------------------------------
use shr_kind_mod, only: r8=>shr_kind_r8
- use physconst, only: cpair
+ use physconst, only: cpair, epsilo, gravit, latvap, tmelt, rair
use ppgrid, only: pver, pcols, pverp, begchunk, endchunk
++<<<<<<< HEAD
+ use zm_conv, only: zm_conv_evap, zm_convr, convtran, momtran
+
+ use zm_microphysics, only: zm_aero_t, zm_conv_t
A much better way to view
Instead type:
git difftool
Important note (this requires diffmerge being set in your .gitconfig file)
These details will be on the last slide
Actual changes
(old on left, new on right)
Visualization of changes within entire file
Step to next changed set of lines
git difftool
Will step through all files changed one-at-a-time
git difftool <filename>
Will look at diffs just in that file
You have completed your project, but the head of the main development repo has made progress as well.
Time to merge:
git merge …
�You see:
izumi$ git merge origin/atmos_select_zm_support
Auto-merging zm/zm_conv_convtran.F90
CONFLICT (add/add): Merge conflict in zm/zm_conv_convtran.F90
Auto-merging zm/zm_conv_evap.F90
CONFLICT (add/add): Merge conflict in zm/zm_conv_evap.F90
Auto-merging zm/zm_conv_momtran.F90
CONFLICT (add/add): Merge conflict in zm/zm_conv_momtran.F90
Auto-merging zm/zm_convr.F90
CONFLICT (add/add): Merge conflict in zm/zm_convr.F90
Automatic merge failed; fix conflicts and then commit the result.
Now what?
If you examine your file, you'll see something like this:
<<<<<<< HEAD
logical, intent(in) :: new_logical
integer, intent(in) :: pcols, pver, pverp
! Original had a tipo
logical, intent(in) :: domomtran(:) ! revise comment
=======
integer, intent(in) :: my_new var
integer, intent(in) :: pcols, pver, pverp
! Original had a typo, but added more info
logical, intent(in) :: domomtran(:) ! final comment
>>>>>>> atmos_phys_main_for_talk
You can avoid that mind numbing process if
you have diffmerge and it's 3-way diff installed
After your git merge has done what it can automatically merge and has told you that there are conflicts, you can say:
git mergetool
Will step through all files which have conflicts one-at-a-time and allow you to resolve the conflicts
git mergetool <filename>
Will bring up that one file with conflicts and allow you to edit it
Reminder again (this requires diffmerge being set in your .gitconfig file)
These details will be on the last slide
What you see when you say "git mergetool"
Pop up window indicates 2 conflicts
Final Merged code
Code merging in
"Your" code
Step to next conflict
First conflict already highlighted
Right mouse click on first conflict
First conflict: two new lines which were added in same location - want to keep both
Right mouse click gives you a choice of options, including "Insert This"
Line is inserted in merged "final" code
Right mouse click on my code's line
Right mouse click gives you slightly different options, including "Append This"
Both lines are inserted in merged "final" code
Step to next conflict (no longer highlighted as I hit it and it is the last one)
Notice highlighting moved down to next conflict
Second conflict has modifications want from both files
Want first line from this file
Want second line from this file
The middle pane is a visual editor, so there are numerous ways to proceed
Save your changes and then close the window
If you said: git mergetool
The mergetool will now advance to the next file with conflicts. It will repeat until all conflicts are resolved
When you have resolved all your conflicts, if you say: git status
You will see:
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: zm/zm_conv_momtran.F90
Follow the directions and say: git commit
Will finish the merge process (no comment needed as the merge is supplying it)
�MERGE IS DONE!
What do I need to do to get this tool?
It is a one-time change
Edit your .gitconfig file (which resides in your home directory) to contain the following:
[diff]
tool = diffmerge
algorithm = histogram
[difftooldiffmerge]
cmd = diffmerge \"$LOCAL\" \"$REMOTE\"
[difftool "diffmerge"]
cmd = diffmerge $LOCAL $REMOTE
[difftool]
prompt = false
[merge]
tool = diffmerge
ff = false
[mergetool "diffmerge"]
cmd = diffmerge --merge --result=$MERGED $REMOTE $BASE $LOCAL
[mergetool]
keepBackup = false
Where do I find this information?
It resides on the ESCOMP/CAM github wiki page:
— OR—
Bonnie will have this link in this week's CGD Weekly Update
Important Note: While diffmerge exists in a public area on izumi and derecho, it is a tool that I have needed to ask system administrators to put onto the machines (it is free). On derecho, they needed to put it in a container.