Got viewing files and diffing files (arbitrary commits) to work.
[Widgit.git] / Git / Commit.cs
blob033ce8a2af2ea1e0ebace0ddb81915a50c843606
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
5 namespace Git
7 public class Commit : Treeish
9 protected List<string> m_Parents;
10 public List<string> Parents
12 get
14 if (null == m_Parents && !Populate())
16 return null;
18 return new List<string>(m_Parents);
20 internal set { m_Parents = value; }
23 public override string Name { get { return "Commit"; } internal set { } }
25 protected string m_hash;
26 public override string ID { get { return m_hash; } internal set { m_hash = value; } }
28 protected string m_author;
29 public string AuthorName
31 get
33 if (null == m_author && !Populate())
35 return null;
37 return m_author;
39 internal set { m_author = value; }
42 protected string m_authorEmail;
43 public string AuthorEmail
45 get
47 if (null == m_authorEmail && !Populate())
49 return null;
51 return m_authorEmail;
53 internal set { m_authorEmail = value; }
56 protected string m_date;
57 public string Date
59 get
61 if (null == m_date && !Populate())
63 return null;
65 return m_date;
67 internal set { m_date = value; }
70 protected string m_subject;
71 public string Subject
73 get
75 if (null == m_subject && !Populate())
77 return null;
79 return m_subject;
81 internal set { m_subject = value; }
84 protected string m_body;
85 public string Body
87 get
89 if (null == m_body && !GetBody())
91 return null;
93 return m_body;
95 internal set { m_body = value; }
98 internal static string FormatString { get { return " --pretty=format:\"%H %P\t%an\t%ae\t%ad\t%s\" "; } }
100 internal string FullFormatString { get { return " --pretty=format:\"%H %P\t%an\t%ae\t%ad\t%s\" "; } } //use this to get body too
102 public Commit(string path, string id) : base(path)
104 m_hash = id;
107 protected Commit(string path) : base(path)
111 public static Commit GetCommit(string path, string record)
113 Commit c = new Commit(path);
114 c.Populate(record);
115 return c;
118 protected bool GetBody()
120 Executioner e = Executioner.GetExecutioner(m_path, true);
121 string output, error;
122 if (0 == e.Execute("git-show", "--name-only --pretty=format:%b " + ID, "", out output, out error))
124 m_body = output.Trim();
125 return true;
127 return false;
130 protected bool Populate()
132 Executioner e = Executioner.GetExecutioner(m_path, true);
133 string output, error;
134 if (0 == e.Execute("git-show", FormatString + ID, "", out output, out error))
136 return Populate(output.Trim());
138 return false;
141 internal bool Populate(string record)
145 int idx1 = record.IndexOf(" ");
146 string id = record.Substring(0, idx1);
147 int idx2 = record.IndexOf("\t", idx1 + 1);
148 string[] parents = record.Substring(idx1 + 1, (idx2 - idx1) - 1).Split(" ".ToCharArray());
149 idx1 = record.IndexOf("\t", idx2 + 1);
150 string authorName = record.Substring(idx2 + 1, (idx1 - idx2) - 1);
151 idx2 = record.IndexOf("\t", idx1 + 1);
152 string authorEmail = record.Substring(idx1 + 1, (idx2 - idx1) - 1);
153 idx1 = record.IndexOf("\t", idx2 + 1);
154 string date = record.Substring(idx2 + 1, (idx1 - idx2) - 1);
156 string subject = record.Substring(idx1 + 1);
157 ID = id;
158 Parents = new List<string>(parents);
159 AuthorName = authorName;
160 AuthorEmail = authorEmail;
161 Date = date;
162 Subject = subject;
164 catch (IndexOutOfRangeException ex)
166 System.Console.WriteLine(ex.Message + " " + ex.StackTrace);
167 return false;
169 return true;