Example #1
0
        /// <summary>
        /// Checkout the specified tree.
        /// </summary>
        /// <param name="tree">The <see cref="Tree"/> to checkout.</param>
        /// <param name="paths">The paths to checkout.</param>
        /// <param name="opts">Collection of parameters controlling checkout behavior.</param>
        private void CheckoutTree(
            Tree tree,
            IList <string> paths,
            CheckoutOptions opts)
        {
            CheckoutNotifyHandler onCheckoutNotify    = opts.CheckoutNotificationOptions != null ? opts.CheckoutNotificationOptions.CheckoutNotifyHandler : null;
            CheckoutNotifyFlags   checkoutNotifyFlags = opts.CheckoutNotificationOptions != null ? opts.CheckoutNotificationOptions.NotifyFlags : default(CheckoutNotifyFlags);
            CheckoutCallbacks     checkoutCallbacks   = CheckoutCallbacks.GenerateCheckoutCallbacks(opts.OnCheckoutProgress, onCheckoutNotify);

            GitStrArrayIn strArray = (paths != null && paths.Count > 0) ? GitStrArrayIn.BuildFrom(ToFilePaths(paths)) : null;

            var options = new GitCheckoutOpts
            {
                version           = 1,
                checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_SAFE,
                progress_cb       = checkoutCallbacks.CheckoutProgressCallback,
                notify_cb         = checkoutCallbacks.CheckoutNotifyCallback,
                notify_flags      = checkoutNotifyFlags,
                paths             = strArray
            };

            try
            {
                if (opts.CheckoutModifiers.HasFlag(CheckoutModifiers.Force))
                {
                    options.checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_FORCE;
                }

                Proxy.git_checkout_tree(Handle, tree.Id, ref options);
            }
            finally
            {
                options.Dispose();
            }
        }
Example #2
0
        /// <summary>
        ///   Internal implementation of Checkout that expects the ID of the checkout target
        ///   to already be in the form of a canonical branch name or a commit ID.
        /// </summary>
        /// <param name="tree">The <see cref="Tree"/> to checkout.</param>
        /// <param name="checkoutOptions"><see cref = "CheckoutOptions" /> controlling checkout behavior.</param>
        /// <param name="onCheckoutProgress"><see cref = "CheckoutProgressHandler" /> that checkout progress is reported through.</param>
        private void CheckoutTree(Tree tree, CheckoutOptions checkoutOptions, CheckoutProgressHandler onCheckoutProgress)
        {
            GitCheckoutOpts options = new GitCheckoutOpts
            {
                version           = 1,
                checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_SAFE,
                progress_cb       = CheckoutCallbacks.GenerateCheckoutCallbacks(onCheckoutProgress)
            };

            if (checkoutOptions.HasFlag(CheckoutOptions.Force))
            {
                options.checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_FORCE;
            }

            Proxy.git_checkout_tree(this.Handle, tree.Id, ref options);
        }
Example #3
0
        /// <summary>
        /// Clone with specified options.
        /// </summary>
        /// <param name="sourceUrl">URI for the remote repository</param>
        /// <param name="workdirPath">Local path to clone into</param>
        /// <param name="bare">True will result in a bare clone, false a full clone.</param>
        /// <param name="checkout">If true, the origin's HEAD will be checked out. This only applies
        /// to non-bare repositories.</param>
        /// <param name="onTransferProgress">Handler for network transfer and indexing progress information</param>
        /// <param name="onCheckoutProgress">Handler for checkout progress information</param>
        /// <param name="credentials">Credentials to use for user/pass authentication</param>
        /// <returns>The path to the created repository.</returns>
        public static string Clone(string sourceUrl, string workdirPath,
                                   bool bare     = false,
                                   bool checkout = true,
                                   TransferProgressHandler onTransferProgress = null,
                                   CheckoutProgressHandler onCheckoutProgress = null,
                                   Credentials credentials = null)
        {
            CheckoutCallbacks checkoutCallbacks = CheckoutCallbacks.GenerateCheckoutCallbacks(onCheckoutProgress, null);

            var cloneOpts = new GitCloneOptions
            {
                Bare = bare ? 1 : 0,
                TransferProgressCallback = TransferCallbacks.GenerateCallback(onTransferProgress),
                CheckoutOpts             =
                {
                    version     = 1,
                    progress_cb =
                        checkoutCallbacks.CheckoutProgressCallback,
                    checkout_strategy = checkout
                                            ? CheckoutStrategy.GIT_CHECKOUT_SAFE_CREATE
                                            : CheckoutStrategy.GIT_CHECKOUT_NONE
                },
            };

            if (credentials != null)
            {
                cloneOpts.CredAcquireCallback =
                    (out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload) =>
                    NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);
            }

            FilePath repoPath;

            using (RepositorySafeHandle repo = Proxy.git_clone(sourceUrl, workdirPath, cloneOpts))
            {
                repoPath = Proxy.git_repository_path(repo);
            }

            // To be safe, make sure the credential callback is kept until
            // alive until at least this point.
            GC.KeepAlive(cloneOpts.CredAcquireCallback);

            return(repoPath.Native);
        }
Example #4
0
        /// <summary>
        /// Clone with specified options.
        /// </summary>
        /// <param name="sourceUrl">URI for the remote repository</param>
        /// <param name="workdirPath">Local path to clone into</param>
        /// <param name="bare">True will result in a bare clone, false a full clone.</param>
        /// <param name="checkout">If true, the origin's HEAD will be checked out. This only applies
        /// to non-bare repositories.</param>
        /// <param name="onTransferProgress">Handler for network transfer and indexing progress information</param>
        /// <param name="onCheckoutProgress">Handler for checkout progress information</param>
        /// <param name="credentials">Credentials to use for user/pass authentication</param>
        /// <returns>The path to the created repository.</returns>
        public static string Clone(string sourceUrl, string workdirPath,
                                   bool bare     = false,
                                   bool checkout = true,
                                   TransferProgressHandler onTransferProgress = null,
                                   CheckoutProgressHandler onCheckoutProgress = null,
                                   Credentials credentials = null)
        {
            CheckoutCallbacks checkoutCallbacks = CheckoutCallbacks.GenerateCheckoutCallbacks(onCheckoutProgress, null);

            var callbacks = new RemoteCallbacks(null, onTransferProgress, null, credentials);
            GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();

            var cloneOpts = new GitCloneOptions
            {
                Bare         = bare ? 1 : 0,
                CheckoutOpts =
                {
                    version     = 1,
                    progress_cb =
                        checkoutCallbacks.CheckoutProgressCallback,
                    checkout_strategy = checkout
                                            ? CheckoutStrategy.GIT_CHECKOUT_SAFE_CREATE
                                            : CheckoutStrategy.GIT_CHECKOUT_NONE
                },
                RemoteCallbacks = gitCallbacks,
            };

            FilePath repoPath;

            using (RepositorySafeHandle repo = Proxy.git_clone(sourceUrl, workdirPath, cloneOpts))
            {
                repoPath = Proxy.git_repository_path(repo);
            }

            return(repoPath.Native);
        }