Beispiel #1
0
        /// <summary>
        /// Get the information about a Git blob by its ID.
        /// </summary>
        /// <param name="repositoryId">The ID of the repository</param>
        /// <param name="blobId">The object ID of the blob</param>
        /// <returns>The blob</returns>
        public async Task<Blob> GetBlob(Guid repositoryId, ObjectId blobId)
        {
            Assert.NotNull(repositoryId, "repositoryId");
            Assert.NotNull(blobId, "blobId");

            var request = new TfsRestRequest("/_apis/git/repositories/{RepositoryId}/blobs/{BlobId}");
            request.AddUrlSegment("RepositoryId", repositoryId.ToString());
            request.AddUrlSegment("BlobId", blobId.ToString());

            return await Executor.Execute<Blob>(request);
        }
Beispiel #2
0
 /// <summary>
 /// A revision in a Git repository, by the fully-qualified reference
 /// name or the commit ID.
 /// </summary>
 /// <param name="refNameOrCommitId">The reference name or commit ID</param>
 public Revision(string refNameOrCommitId)
 {
     if (refNameOrCommitId.StartsWith("refs/heads/"))
     {
         type = RevisionType.Branch;
         version = refNameOrCommitId.Substring(11);
     }
     else if (refNameOrCommitId.StartsWith("refs/tags/"))
     {
         type = RevisionType.Tag;
         version = refNameOrCommitId.Substring(10);
     }
     else
     {
         type = RevisionType.Commit;
         version = new ObjectId(refNameOrCommitId).ToString();
     }
 }
Beispiel #3
0
        /// <summary>
        /// Download a blob's contents.
        /// </summary>
        /// <param name="repositoryId">The ID of the repository</param>
        /// <param name="blobId">The object ID of the blob</param>
        /// <param name="format">The format to download as</param>
        /// <param name="outputStream">The stream to write the response content to asynchronously</param>
        public async Task DownloadBlob(Guid repositoryId, ObjectId blobId, BlobFormat format, Stream outputStream)
        {
            Assert.NotNull(repositoryId, "repositoryId");
            Assert.NotNull(blobId, "blobId");
            Assert.NotNull(outputStream, "outputStream");

            var request = new TfsRestRequest("/_apis/git/repositories/{RepositoryId}/blobs/{BlobId}");
            request.AddUrlSegment("RepositoryId", repositoryId.ToString());
            request.AddUrlSegment("BlobId", blobId.ToString());

            if (format == BlobFormat.Zip)
            {
                request.AddUrlSegment("$format", "zip");
                request.AddHeader("Accept", "application/zip");
            }
            else
            {
                request.AddUrlSegment("$format", "octetstream");
                request.AddHeader("Accept", "application/octet-stream");
            }

            await Executor.Execute(request, outputStream);
        }
Beispiel #4
0
        /// <summary>
        /// Download the contents of a tree as a zip file.
        /// </summary>
        /// <param name="repositoryId">The ID of the repository</param>
        /// <param name="treeId">The object ID of the tree</param>
        /// <param name="outputStream">The stream to write to asynchronously</param>
        public async Task DownloadTree(Guid repositoryId, ObjectId treeId, Stream outputStream)
        {
            Assert.NotNull(repositoryId, "repositoryId");
            Assert.NotNull(treeId, "treeId");
            Assert.NotNull(outputStream, "outputStream");

            var request = new TfsRestRequest("/_apis/git/repositories/{RepositoryId}/trees/{TreeId}");
            request.AddUrlSegment("RepositoryId", repositoryId.ToString());
            request.AddUrlSegment("TreeId", treeId.ToString());
            request.AddUrlSegment("$format", "zip");
            request.AddHeader("Accept", "application/zip");

            await Executor.Execute(request, outputStream);
        }
Beispiel #5
0
        /// <summary>
        /// Get the information about a Git tree by its ID.
        /// </summary>
        /// <param name="repositoryId">The ID of the repository</param>
        /// <param name="treeId">The object ID of the tree</param>
        /// <returns>The tree</returns>
        public async Task<Tree> GetTree(Guid repositoryId, ObjectId treeId)
        {
            Assert.NotNull(repositoryId, "repositoryId");
            Assert.NotNull(treeId, "treeId");

            var request = new TfsRestRequest("/_apis/git/repositories/{RepositoryId}/trees/{TreeId}");
            request.AddUrlSegment("RepositoryId", repositoryId.ToString());
            request.AddUrlSegment("TreeId", treeId.ToString());

            return await Executor.Execute<Tree>(request);
        }
Beispiel #6
0
 /// <summary>
 /// Download a blob's contents.
 /// </summary>
 /// <param name="repositoryId">The ID of the repository</param>
 /// <param name="blobId">The object ID of the blob</param>
 /// <param name="outputStream">The stream to write the response content to asynchronously</param>
 public async Task DownloadBlob(Guid repositoryId, ObjectId blobId, Stream outputStream)
 {
     await DownloadBlob(repositoryId, blobId, BlobFormat.Raw, outputStream);
 }
Beispiel #7
0
        /// <summary>
        /// Get a commit in a repository.
        /// </summary>
        /// <param name="repositoryId">The ID of the repository</param>
        /// <param name="commitId">The ID of the commit</param>
        /// <param name="changeCount">The number of changes to return, or 0 for none.</param>
        /// <returns>The commit in the Git repository</returns>
        public async Task<Commit> GetCommit(Guid repositoryId, ObjectId commitId, int changeCount = 0)
        {
            Assert.NotNull("commitId", "commitId");

            var request = new TfsRestRequest("/_apis/git/repositories/{RepositoryId}/commits/{CommitId}");
            request.AddUrlSegment("RepositoryId", repositoryId.ToString());
            request.AddUrlSegment("CommitId", commitId.ToString());
            request.AddOptionalParameter("changeCount", changeCount, () => { return changeCount > 0; });

            return await Executor.Execute<Commit>(request);
        }
Beispiel #8
0
 /// <summary>
 /// A revision in a Git repository, by the commit ID.
 /// </summary>
 /// <param name="commitId"></param>
 public Revision(ObjectId commitId)
 {
     type = RevisionType.Commit;
     version = commitId.ToString();
 }
Beispiel #9
0
        public int DownloadBlob(string[] args)
        {
            if (args.Length < 2)
            {
                Console.Error.WriteLine("usage: {0} <url> Git.DownloadBlob [repositoryId] [blobId] [--filename=filename]", Program.ProgramName);
                return 1;
            }

            Guid repositoryId = new Guid(args[0]);
            ObjectId blobId = new ObjectId(args[1]);
            string filename = null;
            BlobFormat format = BlobFormat.Raw;

            for (int i = 1; i < args.Length; i++)
            {
                if (args[i].StartsWith("--") || args[i].StartsWith("/"))
                {
                    string arg = args[i].Substring(args[i].StartsWith("--") ? 2 : 1);
                    string[] options = arg.Split(new char[] { '=' }, 2);

                    if (options[0] != null &&
                        options[0].Equals("filename", StringComparison.InvariantCultureIgnoreCase))
                    {
                        filename = options[1];
                    }
                    else if (options[0] != null &&
                        options[0].Equals("format", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (options[1].Equals("raw", StringComparison.InvariantCultureIgnoreCase))
                        {
                            format = BlobFormat.Raw;
                        }
                        else if (options[1].Equals("zip", StringComparison.InvariantCultureIgnoreCase))
                        {
                            format = BlobFormat.Zip;
                        }
                        else
                        {
                            Console.Error.WriteLine("{0}: unknown format '{1}'", Program.ProgramName, options[1]);
                            return 1;
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine("{0}: unknown option '{1}'", Program.ProgramName, args[i]);
                        return 1;
                    }
                }
            }

            if (filename == null)
            {
                filename = (format == BlobFormat.Zip) ? String.Format("{0}", args[1]) : args[1];
            }

            Task.Run(async () =>
            {
                using (Stream outputStream = File.Create(filename))
                {
                    await Client.Git.DownloadBlob(repositoryId, blobId, format, outputStream);
                    outputStream.Close();
                }
            }).Wait();

            return 0;
        }
Beispiel #10
0
        public int GetTree(string[] args)
        {
            if (args.Length != 2)
            {
                Console.Error.WriteLine("usage: {0} <url> Git.GetTree [repositoryId] [treeId]", Program.ProgramName);
                return 1;
            }

            Guid repositoryId = new Guid(args[0]);
            ObjectId treeId = new ObjectId(args[1]);

            Tree tree = null;

            Task.Run(async () =>
            {
                tree = await Client.Git.GetTree(repositoryId, treeId);
            }).Wait();

            Console.WriteLine("Tree {0}:", tree.Id);
            Model.Write(tree);

            return 0;
        }
Beispiel #11
0
        public int GetCommit(string[] args)
        {
            if (args.Length < 1)
            {
                Console.Error.WriteLine("usage: {0} <url> Git.GetCommit [repositoryId] [commitId]", Program.ProgramName);
                return 1;
            }

            Guid repositoryId = new Guid(args[0]);
            ObjectId commitId = new ObjectId(args[1]);
            int? changeCount = null;

            for (int i = 1; i < args.Length; i++)
            {
                if (args[i].StartsWith("--") || args[i].StartsWith("/"))
                {
                    string arg = args[i].Substring(args[i].StartsWith("--") ? 2 : 1);
                    string[] options = arg.Split(new char[] { '=' }, 2);

                    if (options[0] != null &&
                        options[0].Equals("changeCount", StringComparison.InvariantCultureIgnoreCase))
                    {
                        changeCount = int.Parse(options[1]);
                    }
                    else
                    {
                        Console.Error.WriteLine("{0}: unknown option '{1}'", Program.ProgramName, args[i]);
                        return 1;
                    }
                }
            }

            Commit commit = null;

            Task.Run(async () =>
            {
                if (changeCount.HasValue)
                {
                    commit = await Client.Git.GetCommit(repositoryId, commitId, changeCount.Value);
                }
                else
                {
                    commit = await Client.Git.GetCommit(repositoryId, commitId);
                }
            }).Wait();

            Console.WriteLine("Commit {0}:", commit.Id);
            Model.Write(commit);

            return 0;
        }