views:

6802

answers:

7

Example:

$ svn copy foo.txt bar.txt  
A    bar.txt
  • When would you use this technique, and why?
  • Will this command (taken from svn's "red book") creates a copy of <foo.txt> while preserving the history of it to be shared with <bar.txt>?
  • If I'm changing <bar.txt>, what will happen to <foo.txt>?

What are the equivalents to this in other modern systems (Clearcase, Accurev, Perforce)?

Clarification:
Let me emphasize the point I'm searching for:
Is this kind of branching out on a file level?
What happens if you use it in the same branch, i.e. create a copy of a file and than start changing that new file. all in the same branch?
I understand that it is also used for tagging but what is interesting me is what to expect when performing <svn copy> On the file level

+12  A: 

1) To create tags, and also to create branches, although usually you'd use it on a directory rather than a single file. A tag is a copy of one or more files, which you keep for convenience but never change again. A branch is a copy of one or more files, which then evolves separately from the original

2) Not quite, foo.txt's history will effectively be copied to form the history of bar.txt, then an extra entry appears in the history of bar.txt indicating that it was copied from foo.txt, and thereafter they are independent. So the history up to the point of the copy is identical/shared.

3) Nothing, they are completely separate. But you can later merge changes from one to the other.

Steve Jessop
I believe Copy will also be smart enough to efficiently store the history... if the file(s) have a long history, this saves significant space. Updates to the log will also be shared between files I believe.
James Schek
log = log messages.
James Schek
Yes, I'm sure that's right. SVN loudly advertises that the file is a "logical" copy, but does not duplicate the contents of the file in the database. This claim would be nonsense if it didn't apply to the whole history, and tagging would be incredibly expensive in DB space. Which it isn't.
Steve Jessop
+1  A: 

I use this technique when I'm creating a new file, and I want to copy scaffolding (I currently use Perforce, but used to use Subversion). Changing a copy will not effect the other file.

Michael Bobick
Not sure why this is voted down - I've done this myself. It means that if you change the boilerplate (copyright notice and blah blah), then merging the change into whichever derived files need it is exactly the same operation as a normal trunk->branch merge.
Steve Jessop
+3  A: 

What are the equivalents to this in other modern systems (Clearcase, Accurev, Perforce)?

git will notice the file is the same on a plain copy and show it as a copy.

Dustin
Please describe why you feel this is inaccurate so I can correct it and don't just vote down.
Dustin
that's awesome! +1
Orion Edwards
+10  A: 

Aside from branching/tagging, you can also use it when you split a file in two. In this case both new files will have history and you'll be able to look at the code before split.

BTW: that's one of few features that SVN has, but Git doesn't :)

porneL
Nice, I've not seen that one before.
Steve Jessop
A: 

Branch is not a first-class citizen in Subversion, since it is "implemented" as a directory.

Hence, the svn copy allow to kind of branch of file within the same branch (directory). You can later merge back the copied file into the first. But this is ill-suited for just one file, as mentionned in this thread

The equivalent in ClearCase would be a select rule like

element * .../myBranchForCopy/LATEST
element /myPath/myFile /main/myBranch/LATEST -mkbranch myBranchForCopy

However, in this view made for branching a file, you will only see one foo.txt at a time (either in the myBranch, or if it is checked-out, in the myBranchForCopy). There is no real "copy", it is the same element. Any merge would be between:

  • foo.txt@@/main/myBranch/myBranchForCopy/LATEST
    and
  • foo.txt@@/main/myBranch/LATEST
VonC
"Branch is not a first-class citizen". The Subversion people might phrase that, "branch is not a special case in Subversion". I've heard rumours that the latest version of svn does have some branch merging helpers, but I haven't checked that out yet.
Steve Jessop
+2  A: 

This is a slightly unusual use, but I find that sometimes I have to divide a source file into two separate files - for example, if it contains two sets of unrelated functionality, and I use svn copy to do this. I then modify both files and delete the inappropriate bits from each. This way, both of the new files retain revision history for their relevant bits.

Simon Howard
A: 

svn copy came in handy after I inadvertantly deleted a file. See the anwer to an accoring question I have asked.

René Nyffenegger