Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a hunk mean in relation to pushes on sourcetree

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?

like image 216
Breako Breako Avatar asked Aug 01 '13 13:08

Breako Breako


People also ask

What does hunk mean in Git?

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.

What is a hunk programming?

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.


2 Answers

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) 
like image 180
Shahbaz Avatar answered Sep 24 '22 08:09

Shahbaz


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.

like image 24
Dirk Schumacher Avatar answered Sep 22 '22 08:09

Dirk Schumacher