|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace SourceGit.Commands |
| 5 | +{ |
| 6 | + public class QuerySingleCommit : Command |
| 7 | + { |
| 8 | + private const string GPGSIG_START = "gpgsig -----BEGIN PGP SIGNATURE-----"; |
| 9 | + private const string GPGSIG_END = " -----END PGP SIGNATURE-----"; |
| 10 | + |
| 11 | + public QuerySingleCommit(string repo, string sha) { |
| 12 | + WorkingDirectory = repo; |
| 13 | + Context = repo; |
| 14 | + Args = $"show --pretty=raw --decorate=full -s {sha}"; |
| 15 | + } |
| 16 | + |
| 17 | + public Models.Commit Result() |
| 18 | + { |
| 19 | + var succ = Exec(); |
| 20 | + if (!succ) |
| 21 | + return null; |
| 22 | + |
| 23 | + _commit.Message.Trim(); |
| 24 | + return _commit; |
| 25 | + } |
| 26 | + |
| 27 | + protected override void OnReadline(string line) |
| 28 | + { |
| 29 | + if (isSkipingGpgsig) |
| 30 | + { |
| 31 | + if (line.StartsWith(GPGSIG_END, StringComparison.Ordinal)) |
| 32 | + isSkipingGpgsig = false; |
| 33 | + return; |
| 34 | + } |
| 35 | + else if (line.StartsWith(GPGSIG_START, StringComparison.Ordinal)) |
| 36 | + { |
| 37 | + isSkipingGpgsig = true; |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if (line.StartsWith("commit ", StringComparison.Ordinal)) |
| 42 | + { |
| 43 | + line = line.Substring(7); |
| 44 | + |
| 45 | + var decoratorStart = line.IndexOf('(', StringComparison.Ordinal); |
| 46 | + if (decoratorStart < 0) |
| 47 | + { |
| 48 | + _commit.SHA = line.Trim(); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + _commit.SHA = line.Substring(0, decoratorStart).Trim(); |
| 53 | + ParseDecorators(_commit.Decorators, line.Substring(decoratorStart + 1)); |
| 54 | + } |
| 55 | + |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (line.StartsWith("tree ", StringComparison.Ordinal)) |
| 60 | + { |
| 61 | + return; |
| 62 | + } |
| 63 | + else if (line.StartsWith("parent ", StringComparison.Ordinal)) |
| 64 | + { |
| 65 | + _commit.Parents.Add(line.Substring("parent ".Length)); |
| 66 | + } |
| 67 | + else if (line.StartsWith("author ", StringComparison.Ordinal)) |
| 68 | + { |
| 69 | + Models.User user = Models.User.Invalid; |
| 70 | + ulong time = 0; |
| 71 | + Models.Commit.ParseUserAndTime(line.Substring(7), ref user, ref time); |
| 72 | + _commit.Author = user; |
| 73 | + _commit.AuthorTime = time; |
| 74 | + } |
| 75 | + else if (line.StartsWith("committer ", StringComparison.Ordinal)) |
| 76 | + { |
| 77 | + Models.User user = Models.User.Invalid; |
| 78 | + ulong time = 0; |
| 79 | + Models.Commit.ParseUserAndTime(line.Substring(10), ref user, ref time); |
| 80 | + _commit.Committer = user; |
| 81 | + _commit.CommitterTime = time; |
| 82 | + } |
| 83 | + else if (string.IsNullOrEmpty(_commit.Subject)) |
| 84 | + { |
| 85 | + _commit.Subject = line.Trim(); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + _commit.Message += (line.Trim() + "\n"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private bool ParseDecorators(List<Models.Decorator> decorators, string data) |
| 94 | + { |
| 95 | + bool isHeadOfCurrent = false; |
| 96 | + |
| 97 | + var subs = data.Split(new char[] { ',', ')', '(' }, StringSplitOptions.RemoveEmptyEntries); |
| 98 | + foreach (var sub in subs) |
| 99 | + { |
| 100 | + var d = sub.Trim(); |
| 101 | + if (d.StartsWith("tag: refs/tags/", StringComparison.Ordinal)) |
| 102 | + { |
| 103 | + decorators.Add(new Models.Decorator() |
| 104 | + { |
| 105 | + Type = Models.DecoratorType.Tag, |
| 106 | + Name = d.Substring(15).Trim(), |
| 107 | + }); |
| 108 | + } |
| 109 | + else if (d.EndsWith("/HEAD", StringComparison.Ordinal)) |
| 110 | + { |
| 111 | + continue; |
| 112 | + } |
| 113 | + else if (d.StartsWith("HEAD -> refs/heads/", StringComparison.Ordinal)) |
| 114 | + { |
| 115 | + isHeadOfCurrent = true; |
| 116 | + decorators.Add(new Models.Decorator() |
| 117 | + { |
| 118 | + Type = Models.DecoratorType.CurrentBranchHead, |
| 119 | + Name = d.Substring(19).Trim(), |
| 120 | + }); |
| 121 | + } |
| 122 | + else if (d.Equals("HEAD")) |
| 123 | + { |
| 124 | + isHeadOfCurrent = true; |
| 125 | + decorators.Add(new Models.Decorator() |
| 126 | + { |
| 127 | + Type = Models.DecoratorType.CurrentCommitHead, |
| 128 | + Name = d.Trim(), |
| 129 | + }); |
| 130 | + } |
| 131 | + else if (d.StartsWith("refs/heads/", StringComparison.Ordinal)) |
| 132 | + { |
| 133 | + decorators.Add(new Models.Decorator() |
| 134 | + { |
| 135 | + Type = Models.DecoratorType.LocalBranchHead, |
| 136 | + Name = d.Substring(11).Trim(), |
| 137 | + }); |
| 138 | + } |
| 139 | + else if (d.StartsWith("refs/remotes/", StringComparison.Ordinal)) |
| 140 | + { |
| 141 | + decorators.Add(new Models.Decorator() |
| 142 | + { |
| 143 | + Type = Models.DecoratorType.RemoteBranchHead, |
| 144 | + Name = d.Substring(13).Trim(), |
| 145 | + }); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + decorators.Sort((l, r) => |
| 150 | + { |
| 151 | + if (l.Type != r.Type) |
| 152 | + { |
| 153 | + return (int)l.Type - (int)r.Type; |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + return l.Name.CompareTo(r.Name); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + return isHeadOfCurrent; |
| 162 | + } |
| 163 | + |
| 164 | + private Models.Commit _commit = new Models.Commit(); |
| 165 | + private bool isSkipingGpgsig = false; |
| 166 | + } |
| 167 | +} |
0 commit comments