What does a hunk mean in relation to pushes on sourcetree

Breako Breako picture Breako Breako · Aug 1, 2013 · Viewed 27.5k times · Source

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?

Answer

Shahbaz picture Shahbaz · Aug 1, 2013

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)