diff adds new function inside existing one rather than after

Good day everyone,

I’ve started paying a little more attention to the diff files I am creating (with git) and I am unhappy with the clarity of the diff this situation creates.

  • What does “@@ -1 +1 @@” mean in Git's diff output?
  • Want to exclude file from “git diff”
  • Use Xcode 4 as Git difftool
  • Does Github have a view that shows diffs between file versions?
  • How do I view previous diff commits using git?
  • How to merge unversioned code into Git repository?
  • I start with:

    sub main {
        my $self = shift;
    
        return $self;
    }
    

    I then add a second function and a call to it in the first to get:

    sub main {
        my $self = shift;
    
        $self -> process ( 'hello world!' );
    
        return $self;
    }
    
    sub process {
        my $self = shift;
    
        print @_, "\n";
    
        return $self;
    }
    

    The diff output I get for that is:

    @@ -1,5 +1,15 @@
     sub main {
        my $self = shift;
    
    +   $self -> process ( 'hello world!' );
    +   
    +   return $self;
    +}
    +
    +sub process {
    +   my $self = shift;
    +   
    +   print @_, "\n";
    +   
        return $self;
     }
    

    This seems very untidy and unclear to the reader to me. The chunk of additions makes it look like I am adding a lot of lines to the ‘main’ function when I have actually mostly added lines after it. For example, I would expect/like the diff to be like so:

    @@ -1,5 +1,15 @@
     sub main {
        my $self = shift;
    
    +   $self -> process ( 'hello world!' );
    +   
        return $self;
     }
    +
    +sub process {
    +   my $self = shift;
    +   
    +   print @_, "\n";
    +   
    +   return $self;
    +}
    

    I understand why diff is creating this output (simplest way of going from “a -> b”) but would like if it could be made clearer. Is this possible? Or am I looking at manually editing the diffs or even separating the addition of the new function and the call to it into 2 separate patches/commits? [1]

    I’ve tried using –no-minimal, –patience and –histogram algorithms but the diff result is identical. Editing the diff manually with ‘git add -p’ makes no difference, it still stages as the first, “unclear” diff.

    [1] As a side question, is this separation what I am supposed to be doing anyway commit-wise?

    Thank you for your time.

  • Using git over ssh won't pick up private key
  • git clone, upload-pack out of memory
  • Merge conflict resolution: how to accept all remote changes except the ones in files changed by $AUTHOR?
  • Ivy setup with Git?
  • git — how to recover from unfinished merge
  • import all branches from github
  • 2 Solutions collect form web for “diff adds new function inside existing one rather than after”

    Normally people don’t separate changes like that into multiple commits.
    Generally the rule is to try to have each commit stand on its own. Even if you
    do split that into multiple commits, often people would be looking at a diff of
    the state before either of those commits and the state after both of them, and
    so see the same type of output that you’re seeing.

    Using git add -p definitely wouldn’t work to affect the diff that is output.
    That’s just a way of having more control over which changes get into the next
    commit. In any case the commit consists of only the new contents of the tree
    that you’re working with, it does not record a diff against the previous
    version. The diffs that get displayed later are calculated each time that
    they’re requested.

    One thing that would help to avoid that would be to put a comment like # End
    of <function name>
    after the closing brace of each function. But that has its
    own ugliness to it. In real code there’s likely to be less commonality between
    the different functions, and so less likelihood of the diff being that
    misleading. But in any case, it’s something that most people just get used to.

    According to git diff man page, default context for diff is 3 lines. When two hunks of (possible) independent changes are closer and therefore overlap, they are united in one common hunk

    You can live with it or use always -U<n> option

    Generate diffs with <n> lines of context instead of the usual three

    with your diffs

    Git Baby is a git and github fan, let's start git clone.