C# class for doing the basics.

Examples of programs integrating with Vault and Fortress API. Also, information on integrating with NAnt, CruiseControl.Net and other third-party tools

Moderator: SourceGear

Post Reply
kmann
Posts: 1
Joined: Sat May 02, 2009 7:55 am

C# class for doing the basics.

Post by kmann » Sat May 02, 2009 8:16 am

Here is a core class I just wrote that should handle many basics.
Parts were taken from two posts.
Post1
Post2

These bits of code are actually part of something I wrote this morning and may be something others wish to use or it may help them.

edit: many edits for cleaning code up.

Code: Select all

    using System;
    using System.Collections.Generic;
    using VaultClientIntegrationLib;
    using VaultClientOperationsLib;
    using VaultLib;

    /// <summary>
    /// Grants code level access for source control.
    /// </summary>
    public class Vault
    {
        #region Constant Fields
        #endregion Constant Fields

        #region Fields
        /// <summary>
        /// Shows if a user is logged in.
        /// </summary>
        private static bool isLoggedIn = false;

        /// <summary>
        /// Shows which user is currently logged in. Null if none.
        /// </summary>
        private static string usernameLoggedIn = null;

        /// <summary>
        /// Shows the working folder. Null if unassigned.
        /// </summary>
        private static string workingFolder = null;
        #endregion Fields

        #region Constructors
        #endregion Constructors

        #region Finalizers (Destructors)
        #endregion Finalizers (Destructors)

        #region Delegates
        #endregion Delegates

        #region Events
        #endregion Events

        #region Enums
        #endregion Enums

        #region Interfaces
        #endregion Interfaces

        #region Properties
        /// <summary>
        /// Gets a value indicating whether the user is logged in.
        /// </summary>
        public static bool IsUserLoggedIn
        {
            get
            {
                return isLoggedIn;
            }
        }

        /// <summary>
        /// Gets the curreny logged in user name.
        /// </summary>
        public static string UserNameLoggedIn
        {
            get
            {
                return usernameLoggedIn;
            }
        }

        /// <summary>
        /// Gets the current working folder.
        /// </summary>
        public static string WorkingFolder
        {
            get
            {
                return workingFolder;
            }
        }
        #endregion Properties

        #region Indexers
        #endregion Indexers

        #region Methods
        /// <summary>
        /// Gets the full source tree.
        /// </summary>
        public static void GetFullSourceTree()
        {
            GetOperations.ProcessCommandGet(new string[] { workingFolder }, new GetOptions());
        }

        /// <summary>
        /// Given a path, return all the labels from that path.
        /// </summary>
        /// <param name="path">Path to search in. We're expecting the format of: $/Dingo</param>
        /// <param name="includeInherited">Inherited items are items parents have.</param>
        /// <param name="includeFolderItems">Includes folder items or not. You most likely want this.</param>
        /// <param name="enableRecursiveSearch">This will return child labels.</param>
        /// <returns>List of all labels found in that path.</returns>
        public static List<string> GetLabelFromPath(string path, bool includeInherited, bool includeFolderItems, bool enableRecursiveSearch)
        {
            List<string> labelList = new List<string>();

            int rowsInheritedCount;
            int rowsRecursiveCount;
            string token = String.Empty;

            VaultLabelItemX[] items = null;
            VaultClientTreeObject tree = RepositoryUtil.FindVaultTreeObjectAtReposOrLocalPath(path);

            ServerOperations.client.ClientInstance.BeginLabelQuery(tree.FullPath, tree.ID, enableRecursiveSearch, includeInherited, true, includeFolderItems, 1000, out rowsInheritedCount, out rowsRecursiveCount, out token);

            if (token == null || token == String.Empty)
            {
                throw new Exception("Token null. I have no idea what this means but you should have some pie.");
            }

            ServerOperations.client.ClientInstance.GetLabelQueryItems_Main(token, 0, rowsInheritedCount, out items);

            if (items == null)
            {
                /* Most likely nothing was in the list. Instead of returning an empty array, null is returned. */
                return labelList;
            }

            foreach (VaultLabelItemX item in items)
            {
                labelList.Add(item.Name);
            }

            ServerOperations.client.ClientInstance.EndLabelQuery(token);

            return labelList;
        }

        /// <summary>
        /// Gets a tree from a label given a path.
        /// </summary>
        /// <param name="sourcePath">The path to the label. For example $/Development/</param>
        /// <param name="labelName">Name of the label.</param>
        public static void GetSourceTreeFromLabel(string sourcePath, string labelName)
        {
            string destPath = workingFolder + "\\" + labelName;
            GetOperations.ProcessCommandGetLabelToTempWorkingFolder(sourcePath, labelName, null, new GetOptions(), destPath);
        }

        /// <summary>
        /// Logs a user in. You only need to call this once in a session.
        /// </summary>
        /// <param name="serverUrl">URL to login.</param>
        /// <param name="userName">Username to login under.</param>
        /// <param name="password">Password to that user.</param>
        /// <param name="repository">The repository to open. We currently use: Fellowship Repos</param>
        public static void Login(string serverUrl, string userName, string password, string repository, string workingFolder)
        {
            ServerOperations.client.LoginOptions.URL = serverUrl;
            ServerOperations.client.LoginOptions.User = userName;
            ServerOperations.client.LoginOptions.Password = password;
            ServerOperations.client.LoginOptions.Repository = repository;
            ServerOperations.Login();

            isLoggedIn = true;
            usernameLoggedIn = userName;

            SetRootWorkingFolder(workingFolder);
        }

        /// <summary>
        /// Logs a user out of the session.
        /// </summary>
        public static void Logout()
        {
            ServerOperations.Logout();
            isLoggedIn = false;
            usernameLoggedIn = null;
        }

        /// <summary>
        /// Sets the root working folder.
        /// </summary>
        /// <param name="localFolder">Location on the local drive. In Vista and newer operating systems, it's recommended to use something inside of c:\Users\%username% to keep it out of the root and to avoid unnecessary permissions. You can use something like Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments). Remember, paths too deep causes Visual Studio 2008 to not be happy. 256 chars max for path+projects. Some areas care less, some care more. Tread lightly.</param>
        public static void SetRootWorkingFolder(string localFolder)
        {
            ServerOperations.SetWorkingFolder("$/", localFolder, true);
            workingFolder = localFolder;
        }
        #endregion Methods

        #region Structs
        #endregion Structs

        #region Classes
        #endregion Classes
    }

jeremy_sg
Posts: 1821
Joined: Thu Dec 18, 2003 11:39 am
Location: Sourcegear
Contact:

Re: C# class for doing the basics.

Post by jeremy_sg » Mon May 04, 2009 7:50 am

Thanks for sharing your code. I'm sure that it will help others get a running start.
Subscribe to the Fortress/Vault blog

CoffeeNerd
Posts: 25
Joined: Tue Mar 16, 2010 8:54 am
Location: Seattle, WA

Re: C# class for doing the basics.

Post by CoffeeNerd » Tue Mar 16, 2010 1:59 pm

Does anyone have such an example using the Java API?

-CoffeeNerd
Murray Williams | Starbucks Coffee Company

dwgrogan
Posts: 2
Joined: Thu Apr 07, 2011 8:39 am

Re: C# class for doing the basics.

Post by dwgrogan » Thu Apr 07, 2011 9:24 am

I have began using the class given here and expanded it to include getting history items. The class appears to work fine for logging in, and getting the whole tree. But, I appear to be having issues in getting the proper history items. If my vault repository is something like:

$/
[+] Something
[+] Test
[-] SomethingElse
[+] SomethingElseChild

and my code is like:
public VaultTxHistoryItem[] GetHistoryItems(string vaultPath, DateTime minDate) {
VaultDateTime minVaultDate = (minDate == DateTime.MinValue) ?
VaultDate.EmptyDate() :
new VaultDateTime(minDate.Ticks);

return ServerOperations.ProcessCommandVersionHistory(vaultPath,
-1,
minVaultDate,
VaultDate.EmptyDate(),
0);
}

If I send parameters:
vaultPath = "$/SomethingElse/SomethingElseChild";
minDate = new DateTime(2011, 4, 1);

Then resolve each history item in the result with:
TxInfo info = ServerOperations.ProcessCommandTxDetail(historyItems.TxID);

It turns out that some of the history items have an ItemPath1 : "$/Something/Test", instead of the desired vaultPath I sent with the original call. Note that in this example, the info.items.Length == 1...

Am I doing something wrong to get items from other paths here?

Beth
Posts: 8550
Joined: Wed Jun 21, 2006 8:24 pm
Location: SourceGear
Contact:

Re: C# class for doing the basics.

Post by Beth » Thu Apr 07, 2011 12:50 pm

Were the items moved at any point in their history?

Also check to see if the items were branched or shared.
Beth Kieler
SourceGear Technical Support

dwgrogan
Posts: 2
Joined: Thu Apr 07, 2011 8:39 am

Re: C# class for doing the basics.

Post by dwgrogan » Fri Apr 08, 2011 8:33 am

Yes, there are shares on the two parent folders involved. Is there something in the result that shows it's a shared folder item, or do I have to do a path compare to weed out the ones I'm really not interested in?

jclausius
Posts: 3702
Joined: Tue Dec 16, 2003 1:17 pm
Location: SourceGear
Contact:

Re: C# class for doing the basics.

Post by jclausius » Fri Apr 08, 2011 12:27 pm

TxInfo's items will give you the exact path where the operation took place. So, in the case of an operation in a share, if $/A/B/C/D/file.txt was modified, it will show up in a history of $/X/Y/Z/D/ where D/ is shared.

I'm not 100% certain, but some of the paths of the history items (MiscInfo1, MiscInfo2, and HistoricName) may provide you with a relative path that may be of more help.
Jeff Clausius
SourceGear

Post Reply