How to branch a file using API

This forum is now locked, since Gold Support is no longer offered.

Moderator: SourceGear

Locked
Jerry R
Posts: 67
Joined: Tue Jun 15, 2004 3:01 pm

How to branch a file using API

Post by Jerry R » Tue Feb 28, 2006 7:37 am

I have a bunch of file which are individually shared and I want to write a C# program to branch them all, but I'm not sure how to do it.

I've tried using ChangeSetItem_CopyBranch but this doesn't seem to work.

Here's my non-working function:

public static void BranchFile(ClientInstance ciUser, VaultClientFile vcFile)
{
if (ciUser.TreeCache.IsShared(vcFile.ID))
{
string[] discoveredPaths;
long labelID = 0;
VaultClientTreeObject vcTreeObject;
long rootID;

if (ciUser.GetByLabel_GetStructure(vcFile.FullPath, "6.2r1", ref labelID, null, out discoveredPaths, out vcTreeObject, out rootID))
{
ChangeSetItemColl csic = new ChangeSetItemColl();
ChangeSetItem_CopyBranch csi = new ChangeSetItem_CopyBranch(DateTime.Now, "", String.Empty, vcFile.FullPath, vcFile.FullPath, vcTreeObject.ObjVerID);
csic.Add(csi);
ciUser.InternalChangeSet_Append(csic);
ciUser.Commit(csic));
}
}
}

Sorry for the poor formating.

Thanks,
Jerry

dan
Posts: 2448
Joined: Wed Dec 17, 2003 5:03 pm
Location: SourceGear
Contact:

Post by dan » Tue Feb 28, 2006 10:06 am

Do you want to unshare the files, or to branch them to a different location? If you want to unshare them, you should use the ChangeSetItem_ShareBranch type. I'm not honestly sure if ChangeSetItem_CopyBranch will work on a shared item.

The problem is that the term "branch" is overloaded - it can be both unshare a share, and branch a file/folder to a different location, hence the two different change item types.

Also, I'm not sure what the GetByLabel_GetStructure is trying to accomplish. If you already know the file, it shouldn't be necessary.

If this doesn't help, I'll need more info on the error you are getting.

Jerry R
Posts: 67
Joined: Tue Jun 15, 2004 3:01 pm

Post by Jerry R » Tue Feb 28, 2006 12:47 pm

Hi Dan,

I guess I'm not sure exactly which operation I need. Let me describe in more detail what I want to accomplish and hopefully that will give you enough information to point me in the right direction.

I have two directories, A and B, and a file in A called foo.cpp. At some point, foo.cpp was shared into directory B, so now there's a A/foo.cpp and a B/foo.cpp. I want to break the connection between them so there's now an A/foo.cpp and a B/foo.cpp, but changes to one will not affect the other.

To me, to "unshare" foo.cpp seems to imply that it will no longer exist in B (the reverse operation of a share), but perhaps, in fact, that is the term for what I am trying to accomplish? In the API help file, it says something like "TODO: explain the difference between ShareBranch and CopyBranch". :)

About the GetByLabel_GetStructure, oops - that's leftover code from the code I'm reusing, which used to be involved in a Pin operation.

-Jerry

dan
Posts: 2448
Joined: Wed Dec 17, 2003 5:03 pm
Location: SourceGear
Contact:

Post by dan » Tue Feb 28, 2006 12:53 pm

Yes, what you want is unshare, which breaks the share link, but keeps the files in the locations they already exist at.

So, try it out with the ShareBranch item and see if that works.

Jerry R
Posts: 67
Joined: Tue Jun 15, 2004 3:01 pm

Post by Jerry R » Tue Feb 28, 2006 2:01 pm

Thanks Dan, that did the trick. Now I'm _almost_ there - I just have one problem left. To iterate through all the files, I have this code:

foreach (VaultClientFile vcFile in vcFolder.Files)
{
BranchFile(ciUser, vcFile);
}

After the first ShareBranch however, I get an exception because vcFolder collection has changed so it can't enumerate any more. If this is more of a programming question, I understand, but I figured I'd ask here in case it was something simple (since I'm not that experienced with C#).

-Jerry

dan
Posts: 2448
Joined: Wed Dec 17, 2003 5:03 pm
Location: SourceGear
Contact:

Post by dan » Tue Feb 28, 2006 2:18 pm

Hmm. Since the file in the folder is now branched, the folder struct may no longer be valid.

One thing you can do is collect (append) all the share branch commands into a single change set, and then commit them all at the same time. You might want to do this anyway, as it would be quicker to send a bunch of commands at once rather than doing them individually.

Jerry R
Posts: 67
Joined: Tue Jun 15, 2004 3:01 pm

Post by Jerry R » Tue Feb 28, 2006 2:25 pm

Now that you said it, that seems totally obvious. Yes, I'll just append all the ShareBranches to the change set and just move the Commit to after the foreach loop.

Thanks again,
Jerry

Jerry R
Posts: 67
Joined: Tue Jun 15, 2004 3:01 pm

Post by Jerry R » Tue Feb 28, 2006 5:20 pm

Dan (or anyone),

The program I made seems to be working as expected. However, one of the other users here has started to have a major problem since I started doing the ShareBranching. I don't know if it is related, or just a coincidence. In the Vault client on his machine, he cannot see _any_ files. I stopped running my branch program. He rebooted. Still can't see any files. If I log in under my Vault account on his machine, I can see files, but when he logs in, nothing. I checked in the administrator, and he has the same permissions (r/w) as everyone else.

When he logs into vault, it sees and displays the directory structure, but spends almost no time retrieving the repository information, unlike other systems where it takes a few seconds.

Could this be related to the ShareBranch? Any suggestions on what to try?

-Jerry

lbauer
Posts: 9736
Joined: Tue Dec 16, 2003 1:25 pm
Location: SourceGear

Post by lbauer » Tue Feb 28, 2006 5:42 pm

Have the user delete (or rename) the client side cache:

http://support.sourcegear.com/viewtopic.php?t=6
Linda Bauer
SourceGear
Technical Support Manager

Jerry R
Posts: 67
Joined: Tue Jun 15, 2004 3:01 pm

Post by Jerry R » Tue Feb 28, 2006 5:52 pm

Thank you, that did it. Doing the branching while people are logged in seems to have confused the cache.

-Jerry

Locked