ChatGPT解决这个技术问题 Extra ChatGPT

Partial Commits with Subversion

Given the case I made two independent changes in one file: eg. added a new method and changed another method.

I often don't want to commit both changes as one commit, but as two independent commits.

On a git repository I would use the Interactive Mode of git-add(1) to split the hunk into smaller ones:

 git add --patch

What's the easiest way to do this with Subversion? (Maybe even using an Eclipse plug-in)

Update:
In The Thing About Git, Ryan calls it: “The Tangled Working Copy Problem.”

Were you searching specifically for a solution on the command line? Many of the responses below mention TortoiseSVN 'restore after commit', which is a Tortoise-only feature -- so GUI- and Windows-based.

M
Micha Wiedenmann

Tortoise SVN 1.8 now supports this with it's "Restore after commit" feature. This allow you to make edits to a file, with all of the edits being undone after the commit

Per the documentation:

To commit only the parts of the file that relate to one specific issue: in the commit dialog, right-click on file, choose "restore after commit" edit the file in e.g. TortoiseMerge: undo the changes that you don't want to commit yet save the file commit the file


I wonder how this works. I checked the SVN 1.8 release notes, but I can't figure out which feature "Restore after commit" is based on.
@DavidS This is not a feature of SVN. It's a feature of TortoiseSVN.
FYI, SVN 1.8 is EOL from April 2018 with the release of 1.10
We can also find it in the documentation here : tortoisesvn.net/docs/release/TortoiseSVN_en/…
K
Kariem

I have done this using TortoiseSVN.

The built in merge utility allows you to show a diff between the repository version and your working copy.

Use the create backup function of the diff utility

Go to commit your file as if you were going to commit all your changes. In the commit window, double click the file to show a diff. In the diff settings, click the option to backup original file. Right-click the changes you don't want, and use select use other text block. Save the diff exactly once. The backup will be overwritten each time you save. This is why you only want to save once. Commit the change. Overwrite the original with the created .bak file (which will have all your original changes). Commit your file.

You should now have all your changes committed, using two separate commits.


How exactly did you do this? I'd be interested in learning that technique.
It would be so neat if this would automatically overwrite with the .bak files after a commit.
I did not find the step-3 : Backup option , using Tortoise SVN 1.8.1
Never mind. Found the coresponding link for 1.8 tortoisesvn.net/tsvn_1.8_releasenotes.html To commit only the parts of the file that relate to one specific issue: in the commit dialog, right-click on file, choose "restore after commit" edit the file in e.g. TortoiseMerge: undo the changes that you don't want to commit yet save the file commit the file
Sadly, this does not appear to be available in Tortoise version 1.8.5, nor is "restore after commit".
b
blahdiblah

With git-svn you can make a local GIT repository of the remote SVN repository, work with it using the full GIT feature set (including partial commits) and then push it all back to the SVN repository.

git-svn (1)


There is a particular irony in this solution.
C
Chris

Try using svn diff > out.patch then copy the out.patch file to out.patch.add and out.patch.modify

Only when you have a working patch file revert the original file using svn revert out.c.

Edit the patch files by hand so that they only contain the hunks for adding or modifying. Apply them to the original file using the patch command, test if the addition worked, then svn commit the addition.

Wash rinse repeat for the out.patch.modify patch.

If the changes are separate in the file as your initial question stated - added a new method, changed an existing method - this will work

This is a very tedious solution - although I'm not convinced you should have any reason to separate your commits.

You also could have checked out multiple working copies of the same source to apply your work against:

svn co http://location/repository methodAdd svn co http://location/repository methodModify

Be sure to svn up and test to make sure all is well.


C
Community

This is possible using TortoiseSvn (Windows) since v1.8.

4.4.1. The Commit Dialog If your working copy is up to date and there are no conflicts, you are ready to commit your changes. Select any file and/or folders you want to commit, then TortoiseSVN → Commit.... 4.4.3. Commit only parts of files Sometimes you want to only commit parts of the changes you made to a file. Such a situation usually happens when you're working on something but then an urgent fix needs to be committed, and that fix happens to be in the same file you're working on. right click on the file and use Context Menu → Restore after commit. This will create a copy of the file as it is. Then you can edit the file, e.g. in TortoiseMerge and undo all the changes you don't want to commit. After saving those changes you can commit the file. After the commit is done, the copy of the file is restored automatically, and you have the file with all your modifications that were not committed back.

On Linux, I would give http://webstaff.itn.liu.se/~karlu20/div/blog/2013-05-31_SVNPartialCommit.php a try. Haven't tried it out myself, though.


G
Greg Hewgill

I used to do this:

In my editor (I use vim), edit the file so that only one of the changes appear

Save the file (but don't quit the editor)

Commit the changed file to svn

Hit "undo" in the editor enough times for the second set of changes reappear

Save the file again

Commit the second set of changes.

This is a simplistic approach that assumes one set of changes is reasonably easy to undo. For more complex situations, I would give up and commit both changes without worrying about it.

Now that I use git, this is something I hope I'll never have to do again!


A
Aeon

I use either a local darcs repo, or just merge the changes in gradually. With merging (opendiff opens FileMerge, a merge program that comes with Xcode; replace with your favorite merge tool):

cp file file.new
svn revert file
opendiff file.new file -merge file

merge the related changes, save the merge, quit the merge program

svn ci -m 'first hunk' file
mv file.new file
svn ci -m 'second hunk' file

if more than one unrelated hunk in the file, rinse and repeat (but why would you wait so long before committing?!)

Also, if you know git, you can use git-svn to maintain a local git repo and sync your commits to an svn master server; works great in my limited experience.


Re: "Why wait so long?" Your doing a big all day refactor and the boss drops a little fix-this-now bug just before lunch.
b
bahrep

Try VisualSVN for Visual Studio. The latest 6.1 release introduces the QuickCommit feature. You can partially commit selected changes in a file using the new Commit this Block and Commit Selection context menu commands in the Visual Studio editor.

https://i.stack.imgur.com/hV92h.gif


B
BCS

Open all the files you want to split in editor-of-choice Using a different tool set (on Win, use Spike's suggestion (the old version)) back out the second set Commit go back to your editor-of-choice and save all the files

It's a little riskier than Spike's full suggestion but can be easier to do. Also make sure you try it on something else first as some editors will refuse to save over a file that has changed out from under them unless you reload that file (losing all your changes)


I
Ian Dunn

I think an easier option than generating diff files, reverting, etc, would be to have two copies of the repository checked out, and use a visual diff tool like DeltaWalker to copy hunks from one to the other.

The first copy would be the one you actually work off of, and the second would just be for this purpose. Once you've made a ton of changes to the first, you can copy one section over to the second, commit it, copy another section, commit it, etc.


m
michaeljt

Copy all modified files concerned to back-up copies. Create a patch of the working state using svn diff. Revert the files using svn revert. Re-apply the parts of the patch which you wish to commit, either using the patch tool, or by manual editing, or whatever. Run diff afterwards to compare your working copy with your back-up to be sure you applied the patch-parts correctly. Build and test. Commit. Copy your back-up copies back to your repository check-out. Repeat at 2. (not at 1.!) until done.