When I make changes on my "develop" branch, I see an up-arrow beside the branch telling me how many changes will be pushed. What confuses me thou is how sourcetree decides what the number is?
It seems to relate to something called hunks? What are hunks?
Is there an equiavalent git commit which returns the same number?
The term "hunk" is indeed not specific to Git, and comes from the Gnu diffutil format. Even more succinctly: Each hunk shows one area where the files differ. But the challenge for Git is to determine the right boundaries for a hunk.
When comparing two files, diff finds sequences of lines common to both files, interspersed with groups of differing lines called hunks. Comparing two identical files yields one sequence of common lines and no hunks, because no lines differ.
Note that the number of changes to be pushed probably refers to number of commits you are ahead of origin/master and is unrelated to hunks. To see the commits you are ahead of master, you can do the following:
# get most recent commit found in both master and origin/master mb=$(git merge-base master origin/master) # show commits from that merge base to current head git log $mb..HEAD
If you want to count it, simply do:
mb=... git log --pretty=oneline $mb..HEAD | wc -l
hunk
is a term related to diff
:
The format starts with the same two-line header as the context format, except that the original file is preceded by "---" and the new file is preceded by "+++". Following this are one or more change hunks that contain the line differences in the file. The unchanged, contextual lines are preceded by a space character, addition lines are preceded by a plus sign, and deletion lines are preceded by a minus sign.
If you have ever taken a diff of two files, you see the file like this (again from wikipedia):
--- /path/to/original ''timestamp'' +++ /path/to/new ''timestamp'' @@ -1,3 +1,9 @@ +This is an important +notice! It should +therefore be located at +the beginning of this +document! + This part of the document has stayed the same from version to @@ -5,16 +11,10 @@ be shown if it doesn't change. Otherwise, that would not be helping to -compress the size of the -changes. - -This paragraph contains -text that is outdated. -It will be deleted in the -near future. +compress anything. It is important to spell -check this dokument. On +check this document. On the other hand, a misspelled word isn't the end of the world. @@ -22,3 +22,7 @@ this paragraph needs to be changed. Things can be added after it. + +This paragraph contains +important new additions +to this document.
The file above has three hunks. If you want to see the diff associated with a commit, you can use git show [<commit>]
. To see the diff between your current unstaged changes and the repository, you can use git diff
. There are various other options.
To count the number of hunks (which is really, really useless, but if you insist), you can use a very simple script.
git show | grep '^@@.*@@.*$' | wc -l
The reason for the .*
after the second @@
is that git's diff also shows the function the change belongs to so it can better apply the diff later, so the hunk heading could for example look like this:
@@ -85,6 +85,6 @@ void urt_shmem_detach(void *mem)
On answering the hunk question:
Hunk means a piece of change in the Git world.
src: https://mvtechjourney.wordpress.com/2014/08/01/git-stage-hunk-and-discard-hunk-sourcetree/
There is the suggestion of
Replace the word ‘hunk’ with ‘change’ and it becomes pleasurable to follow Git.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With