Got viewing files and diffing files (arbitrary commits) to work.
[Widgit.git] / Git / File.cs
blob6c2c0ec0cac4b8c2622a9e4571800d53f8c0835a
2 using System;
4 namespace Git
6 public enum FileState
8 Normal = 1,
9 Added = 1<<1,
10 Copied = 1<<2,
11 Deleted = 1<<3,
12 Modified = 1<<4,
13 Renamed = 1<<5,
14 ModeChanged = 1<<6,
15 Unmerged = 1<<7,
16 Unknown = 1<<8,
17 Broken = 1<<9
20 public class File : IComparable<File>
22 protected string m_mode;
23 public string Mode { get { return m_mode; } set { m_mode = value; } }
25 protected string m_hash;
26 public string ID { get { return m_hash; } }
28 protected string m_repoPath;
30 protected string m_relPath;
31 public string RelativePath { get { return m_relPath; } }
33 protected string m_pathWithName;
34 public string PathWithName { get { return m_pathWithName; } }
36 protected string m_name;
37 public string Name { get { return m_name; } }
39 protected int m_stage;
40 public int Stage
42 get
44 return m_stage;
47 set
49 if (value < 0 || value > 3)
51 m_stage = value;
53 m_stage = value;
57 protected FileState m_state = FileState.Unknown;
58 public FileState State
60 get
62 return m_state;
65 set
67 m_state = value;
72 public File(string repoPath, string relativePath)
74 m_repoPath = repoPath;
75 int idx = relativePath.LastIndexOf(Repo.DirectorySeparator);
76 if (idx < 0)
78 m_relPath = "";
79 m_name = relativePath;
81 else
83 m_relPath = relativePath.Substring(0, idx);
84 m_name = relativePath.Substring(idx + 1);
86 m_pathWithName = relativePath;
89 public bool SetStage(string val)
91 int i;
92 if (!int.TryParse(val, out i))
94 return false;
96 if (m_stage < 0 || m_stage > 3)
98 return false;
100 m_stage = i;
101 return true;
104 public int CompareTo(File other)
106 return m_name.CompareTo(other.m_name);
109 /*************
110 * Added (A)
111 * Copied (C) Has 2 Files, source, dest
112 * Deleted (D)
113 * Modified (M)
114 * Renamed (R) Has 2 Files, source, dest
115 * mode changed (T)
116 * Unmerged (U)
117 * Unknown (X)
118 * Broken (B)
119 * ************/
120 public void SetState(string val)
122 val = val.Trim();
123 switch(val)
125 case "A":
126 m_state = FileState.Added;
127 break;
128 case "C":
129 m_state = FileState.Copied;
130 break;
131 case "D":
132 m_state = FileState.Deleted;
133 break;
134 case "M":
135 m_state = FileState.Modified;
136 break;
137 case "R":
138 m_state = FileState.Renamed;
139 break;
140 case "T":
141 m_state = FileState.ModeChanged;
142 break;
143 case "U":
144 m_state = FileState.Unmerged;
145 break;
146 case "X":
147 m_state = FileState.Unknown;
148 break;
149 case "B":
150 m_state = FileState.Broken;
151 break;
152 default:
153 m_state = FileState.Unknown;
154 break;
158 public static string GetStateName(FileState state)
160 switch (state)
162 case FileState.Normal:
163 return "normal";
164 case FileState.Deleted:
165 return "delete";
166 case FileState.Modified:
167 return "modified";
168 case FileState.Unknown:
169 return "unknown";
170 case FileState.Unmerged:
171 return "unmerged";
172 case FileState.Added:
173 return "added";
174 case FileState.Copied:
175 return "copied";
176 case FileState.ModeChanged:
177 return "modechanged";
178 case FileState.Broken:
179 return "broken";
180 case FileState.Renamed:
181 return "renamed";
183 return "unknown";
186 public bool GetRevision(out string contents)
188 return GetRevision("HEAD", out contents);
191 public bool GetRevision(string commitish, out string contents)
193 if (String.IsNullOrEmpty(commitish))
195 contents = null;
196 return false;
198 Executioner e = Executioner.GetExecutioner(m_repoPath, true);
200 string error;
201 return 0 == e.Execute("git-show", commitish + ":" + m_pathWithName, "", out contents, out error);