Example #1
0
        public GitRef(GitRepository repository, string name, string commitId, DateTimeOffset timeStamp, GitRefType type)
        {
            this.Repository = repository;
            this.Name       = name;
            this.CommitId   = commitId;
            this.TimeStamp  = timeStamp;
            this.Type       = type;

            this.CommitSingleton = new Singleton <GitCommit>(() => this.Repository.FindCommit(this.CommitId));
        }
Example #2
0
        public GitRepository(string workDir, CancellationToken cancel = default) : base(cancel)
        {
            try
            {
                this.WorkDir = Env.LocalPathParser.NormalizeDirectorySeparatorAndCheckIfAbsolutePath(workDir);

                this.Repository = new Repository(this.WorkDir);

                this.RefCache = new Singleton <GitRef[]>(() =>
                {
                    List <GitRef> ret = new List <GitRef>();

                    foreach (var e in this.Repository.Refs)
                    {
                        Commit commit = this.Repository.Lookup <Commit>(e.TargetIdentifier);
                        if (commit != null)
                        {
                            GitRefType type = GitRefType.LocalBranch;
                            if (e.IsRemoteTrackingBranch)
                            {
                                type = GitRefType.RemoteBranch;
                            }
                            if (e.IsTag)
                            {
                                type = GitRefType.Tag;
                            }
                            ret.Add(new GitRef(this, e.CanonicalName, e.TargetIdentifier, commit.Author.When, type));
                        }
                    }

                    return(ret.ToArray());
                });
            }
            catch (Exception ex)
            {
                this._DisposeSafe(ex);
                throw;
            }
        }
Example #3
0
        public GitRef(IGitModule module, [CanBeNull] ObjectId objectId, string completeName, string remote = "")
        {
            Module       = module;
            ObjectId     = objectId;
            Guid         = objectId?.ToString();
            CompleteName = completeName;
            Remote       = remote;

            IsDereference = CompleteName.EndsWith(GitRefName.TagDereferenceSuffix);

            _type = GetType();

            var name = ParseName();

            Name = name.IsNullOrWhiteSpace() ? CompleteName : name;

            _remoteSettingName = $"branch.{Name}.remote";
            _mergeSettingName  = $"branch.{Name}.merge";

            return;

            GitRefType GetType()
            {
                if (CompleteName.StartsWith(GitRefName.RefsTagsPrefix))
                {
                    return(GitRefType.Tag);
                }

                if (CompleteName.StartsWith(GitRefName.RefsHeadsPrefix))
                {
                    return(GitRefType.Head);
                }

                if (CompleteName.StartsWith(GitRefName.RefsRemotesPrefix))
                {
                    return(GitRefType.Remote);
                }

                if (CompleteName.StartsWith(GitRefName.RefsBisectPrefix))
                {
                    if (CompleteName.StartsWith(GitRefName.RefsBisectGoodPrefix))
                    {
                        return(GitRefType.BisectGood);
                    }

                    if (CompleteName.StartsWith(GitRefName.RefsBisectBadPrefix))
                    {
                        return(GitRefType.BisectBad);
                    }

                    return(GitRefType.Bisect);
                }

                if (CompleteName.StartsWith(GitRefName.RefsStashPrefix))
                {
                    return(GitRefType.Stash);
                }

                return(GitRefType.Other);
            }

            string ParseName()
            {
                if (IsRemote)
                {
                    return(CompleteName.SubstringAfterLast("remotes/"));
                }

                if (IsTag)
                {
                    // we need the one containing ^{}, because it contains the reference
                    return(CompleteName.RemoveSuffix(GitRefName.TagDereferenceSuffix).SubstringAfterLast("tags/"));
                }

                if (IsHead)
                {
                    return(CompleteName.SubstringAfterLast("heads/"));
                }

                // if we don't know ref type then we don't know if '/' is a valid ref character
                return(CompleteName.SubstringAfter("refs/"));
            }
        }