1 //Widgit is free software; you can redistribute it and/or modify
2 //it under the terms of the GNU General Public License as published by
3 //the Free Software Foundation; either version 2 of the License, or
4 //(at your option) any later version.
6 //Widgit is distributed in the hope that it will be useful,
7 //but WITHOUT ANY WARRANTY; without even the implied warranty of
8 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 //GNU General Public License for more details.
11 //You should have received a copy of the GNU General Public License
12 //along with Widgit; if not, write to the Free Software
13 //Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USAusing System;
16 using System
.Collections
.Generic
;
22 using FilesList
= List
<Git
.File
>;
24 public class FilesUpdatedEvent
: EventArgs
26 public FilesUpdatedEvent(FilesList files
)
28 m_files
= new FilesList(files
);
30 protected FilesList m_files
;
31 public FilesList FilesList { get { return new FilesList(m_files); }
}
34 public delegate void FileUpdatedHandler(FileUpdater sender
, FilesUpdatedEvent evt
);
36 public class FileUpdater
38 public event FileUpdatedHandler FilesUpdated
;
42 protected Dictionary
<IFileOperations
, ISelectable
> m_EventSourcesToSelectables
= new Dictionary
<IFileOperations
,ISelectable
>();
43 public ISelectable
this[IFileOperations o
]
45 get { return m_EventSourcesToSelectables[o]; }
51 o
.Copy
-= OnCopyFiles
;
52 o
.Delete
-= OnDeleteFiles
;
53 o
.RenameOrMove
-= OnMoveFiles
;
54 o
.Revert
-= OnRevertFiles
;
55 o
.DiffHead
-= OnDiffFile
;
56 o
.DiffOther
-= OnDiffHistoryFile
;
58 o
.ViewPrevious
-= OnViewPreviousFile
;
59 m_EventSourcesToSelectables
.Remove(o
);
63 m_EventSourcesToSelectables
[o
] = value;
65 o
.Copy
+= OnCopyFiles
;
66 o
.Delete
+= OnDeleteFiles
;
67 o
.RenameOrMove
+= OnMoveFiles
;
68 o
.Revert
+= OnRevertFiles
;
69 o
.DiffHead
+= OnDiffFile
;
70 o
.DiffOther
+= OnDiffHistoryFile
;
72 o
.ViewPrevious
+= OnViewPreviousFile
;
80 internal FileUpdater(Prefs prefs
)
85 public void AddEventSource(IFileOperations f
, ISelectable s
)
87 m_EventSourcesToSelectables
[f
] = s
;
89 f
.Copy
+= OnCopyFiles
;
90 f
.Delete
+= OnDeleteFiles
;
91 f
.RenameOrMove
+= OnMoveFiles
;
92 f
.Revert
+= OnRevertFiles
;
95 public void SetRepo(Repo repo
)
100 public bool AddFiles(FilesList files
)
102 List
<string> paths
= MakePathsList(files
);
103 FilesList updatedFiles
;
104 if (m_currentRepo
.Add(paths
, out updatedFiles
) && files
.Count
> 0)
106 OnFileUpdated(new FilesUpdatedEvent(updatedFiles
));
111 public bool DeleteFiles(FilesList files
)
113 List
<string> paths
= MakePathsList(files
);
114 FilesList updatedFiles
;
115 if (m_currentRepo
.Remove(paths
, out updatedFiles
) && files
.Count
> 0)
117 OnFileUpdated(new FilesUpdatedEvent(updatedFiles
));
122 public bool RevertFiles(FilesList files
)
124 List
<string> paths
= MakePathsList(files
);
125 FilesList updatedFiles
;
126 if (m_currentRepo
.Revert(paths
, out updatedFiles
) && files
.Count
> 0)
128 OnFileUpdated(new FilesUpdatedEvent(updatedFiles
));
133 private static List
<string> MakePathsList(FilesList files
)
135 List
<string> paths
= new List
<string>();
136 foreach (File f
in files
)
138 if (f
.RelativePath
.Length
> 0)
140 paths
.Add(f
.PathWithName
);
150 protected void OnFileUpdated(FilesUpdatedEvent e
)
152 if (FilesUpdated
!= null)
154 FilesUpdated(this, e
);
158 protected void OnAddFiles(object sender
, EventArgs e
)
160 ISelectable s
= m_EventSourcesToSelectables
[(IFileOperations
)sender
];
161 AddFiles(s
.GetSelected());
164 protected void OnDeleteFiles(object sender
, EventArgs e
)
166 ISelectable s
= m_EventSourcesToSelectables
[(IFileOperations
)sender
];
167 DeleteFiles (s
.GetSelected());
170 protected void OnRevertFiles(object sender
, EventArgs e
)
172 ISelectable s
= m_EventSourcesToSelectables
[(IFileOperations
)sender
];
173 RevertFiles(s
.GetSelected());
176 protected void OnViewFile(object sender
, EventArgs e
)
178 ISelectable s
= m_EventSourcesToSelectables
[(IFileOperations
)sender
];
179 foreach (File f
in s
.GetSelected())
181 string path
= SerializeRevision(f
, null);
183 if (String
.IsNullOrEmpty(m_prefs
.ViewerApp
))
185 System
.Diagnostics
.ProcessStartInfo si
= new System
.Diagnostics
.ProcessStartInfo();
187 si
.UseShellExecute
= true;
188 System
.Console
.WriteLine(si
.FileName
+ " " + si
.Arguments
);
189 System
.Diagnostics
.Process p
= new System
.Diagnostics
.Process();
195 LaunchApp(m_prefs
.ViewerApp
, m_prefs
.ViewerArgs
, path
, "", "", "");
200 protected void OnViewPreviousFile(object sender
, EventArgs e
)
202 ISelectable s
= m_EventSourcesToSelectables
[(IFileOperations
)sender
];
203 RevertFiles(s
.GetSelected());
206 protected void OnDiffFile(object sender
, EventArgs e
)
208 ISelectable s
= m_EventSourcesToSelectables
[(IFileOperations
)sender
];
209 if (String
.IsNullOrEmpty(m_prefs
.DiffApp
))
213 foreach (File f
in s
.GetSelected())
215 Diff(f
, null, f
, "HEAD");
219 protected void OnDiffHistoryFile(object sender
, EventArgs e
)
221 ISelectable s
= m_EventSourcesToSelectables
[(IFileOperations
)sender
];
222 if (String
.IsNullOrEmpty(m_prefs
.DiffApp
))
227 List
<Git
.File
> files
= s
.GetSelected();
232 List
<string> paths
= new List
<string>();
233 foreach (File f
in files
)
235 paths
.Add(f
.PathWithName
);
237 RevisionChooser c
= new RevisionChooser();
238 string commitish
= c
.Choose(m_currentRepo
, paths
);
239 if (null == commitish
)
243 foreach (File f
in files
)
245 Diff(f
, null, f
, commitish
);
250 private string ReplaceOrAppend(string orig
, string toReplace
, string replaceWith
)
252 if (orig
.IndexOf(toReplace
) < 0)
254 orig
+= " \"" + replaceWith
+ "\"";
258 orig
= orig
.Replace("[File]", "\"" + replaceWith
+ "\"");
263 protected string SerializeRevision(File f
, string commitish
)
266 if (String
.IsNullOrEmpty(commitish
))
268 string nativePath
= f
.PathWithName
.Replace(Repo
.DirectorySeparator
,
269 System
.IO
.Path
.DirectorySeparatorChar
.ToString());
270 nativePath
= m_currentRepo
.Path
+ System
.IO
.Path
.DirectorySeparatorChar
+ nativePath
;
273 if (!f
.GetRevision(commitish
, out contents
))
277 string tmpFileName
= System
.IO
.Path
.GetTempFileName();
278 System
.IO
.StreamWriter writer
= new System
.IO
.StreamWriter(tmpFileName
);
279 writer
.Write(contents
);
284 protected bool Diff(File file1
, string commitish1
, File file2
, string commitish2
)
291 string nativePath1
= SerializeRevision(file1
, commitish1
);
292 if (null == nativePath1
)
297 string nativePath2
= SerializeRevision(file2
, commitish2
);
298 if (null == nativePath2
)
302 return LaunchApp(m_prefs
.DiffApp
, m_prefs
.DiffArgs
, "", nativePath2
, nativePath1
, "");
305 protected bool LaunchApp(string execPath
, string args
, string file
, string yours
, string theirs
, string target
)
307 System
.Diagnostics
.ProcessStartInfo si
= new System
.Diagnostics
.ProcessStartInfo();
308 si
.FileName
= execPath
;
309 args
= ReplaceOrAppend(args
, "[File]", file
);
310 args
= ReplaceOrAppend(args
, "[Theirs]", theirs
);
311 args
= ReplaceOrAppend(args
, "[Yours]", yours
);
312 args
= ReplaceOrAppend(args
, "[Target]", target
);
314 System
.Console
.WriteLine(si
.FileName
+ " " + si
.Arguments
);
315 System
.Diagnostics
.Process p
= new System
.Diagnostics
.Process();
321 protected void OnCopyFiles(object sender
, EventArgs e
)
323 //ISelectable s = m_EventSourcesToSelectables[sender];
324 //CopyFiles(s.GetSelected());
327 protected void OnMoveFiles(object sender
, EventArgs e
)
329 //ISelectable s = m_EventSourcesToSelectables[sender];
330 //RevertFiles(s.GetSelected()); ;