tags:

views:

34

answers:

2

Hi,

I have code such as

void myfunc()
{
  introduction();

  while(condition())
  {
    complex();
    loop();
    interior();
    code();
  }

  cleanup();
}

which I wish to duplicate into two versions, viz:

void myfuncA()
{
  introduction();
  minorchangeA();

  while(condition())
  {
    complex();
    loop();
    interior();
    code();
  }

  cleanup();
}

void myfuncB()
{
  introduction();
  minorchangeB();

  while(condition())
  {
    complex();
    modifiedB();
    loop();
    interior();
    code();
  }

  cleanup();
  extracleanupB();
}

git claims to track content rather than files, so do I need to tell it that there are chunks here that are common to both myfuncA and myfuncB so that when merging with upstream changes to myfunc that those changes should propagate to both myfuncA and myfuncB? If so, how?

The code could be written so that myfuncAB did the correct thing at each point by testing for condition A or B, but that could seriously hinder readability or performance.

A: 

In fact, git it a great source code management system, but it rarely does programming for you.

Michael Krelin - hacker
A: 

Do not duplicate code. That's the DRY principle.

Neither git nor anyone will help you for that.

Besides, you misunderstood the role of git: git tracks file contents, not chunks. There is no way to tell git that such and such chunks in one file are the same.

Olivier
git tracks *content*, actually. But what you should've emphasized is *tracks*, anyway ;-) It doesn't produce any.
Michael Krelin - hacker
I didn't ask it to *produce* content :-) I said I wished that it might recognize that I'd copied and pasted content.Not copying and pasting code is a good idea, but all rules can have exceptions.Such a git feature would not immediately violate the DRY principle, because there *is* one source of "truth". One problem comes later when some modification to `myfuncA` or `myfuncB` should be made to the other, and isn't made because they're now separated.Thanks for the feedback.
mabraham