5 // Joel Reed (joelwreed@gmail.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System
.Security
.Cryptography
;
31 using System
.Collections
.Generic
;
34 using System
.Text
.RegularExpressions
;
35 using Microsoft
.TeamFoundation
.Client
;
36 using Microsoft
.TeamFoundation
.VersionControl
.Common
;
37 using Microsoft
.TeamFoundation
.VersionControl
.Client
;
38 using Mono
.GetOptions
;
40 [Command("online", "Finds all writable files and marks them as pending changes on the server.")]
41 class OnlineCommand
: Command
43 [Option("Look for added files.", "", "added")]
44 public bool OptionAdded
= false;
46 [Option("Look for deleted files.", "D", "deleted")]
47 public bool OptionDeleted
= false;
49 [Option("Look for modified files.", "", "modified")]
50 public bool OptionModified
= false;
52 [Option("Preview", "", "preview")]
53 public bool OptionPreview
= false;
55 [Option("Recursive", "R", "recursive")]
56 public bool OptionRecursive
= false;
58 private List
<string> addedFiles
= new List
<string>();
59 private List
<string> editedFiles
= new List
<string>();
60 private List
<string> deletedFiles
= new List
<string>();
61 private Workspace workspace
;
62 private MD5CryptoServiceProvider md5
= new MD5CryptoServiceProvider();
64 public OnlineCommand(Driver driver
, string[] args
): base(driver
, args
)
68 private void ProcessFile(SortedList
<string, byte[]> itemList
, string path
)
70 bool isReadOnly
= (FileAttributes
.ReadOnly
== (File
.GetAttributes(path
) & FileAttributes
.ReadOnly
));
72 if (!itemList
.ContainsKey(path
))
76 Console
.WriteLine("Added: " + path
);
80 else if (!isReadOnly
&& OptionModified
)
82 string itemHash
= Convert
.ToBase64String(itemList
[path
]);
84 using (FileStream fileStream
= new FileStream(path
, FileMode
.Open
, FileAccess
.Read
))
86 md5
.ComputeHash(fileStream
);
87 string localHash
= Convert
.ToBase64String(md5
.Hash
);
88 if (itemHash
!= localHash
)
90 editedFiles
.Add(path
);
91 Console
.WriteLine("Modified: " + path
);
97 private void ProcessFileList(SortedList
<string, bool> files
)
99 List
<ItemSpec
> itemSpecs
= new List
<ItemSpec
>();
100 foreach (string file
in files
.Keys
)
101 itemSpecs
.Add(new ItemSpec(file
, RecursionType
.None
));
103 // pull item list based on WorkspaceVersion. otherwise might get
104 // new items on server that haven't been pulled yet in the list returned
105 WorkspaceVersionSpec version
= new WorkspaceVersionSpec(workspace
);
106 ItemSet itemSet
= VersionControlServer
.GetItems(itemSpecs
.ToArray(), version
, DeletedState
.NonDeleted
, ItemType
.File
, true);
108 // get item list from TFS server
109 Item
[] items
= itemSet
.Items
;
110 SortedList
<string, byte[] > itemList
= new SortedList
<string, byte[] >(PathComparer
);
112 foreach (Item item
in items
)
114 string localItem
= workspace
.GetLocalItemForServerItem(item
.ServerItem
);
115 itemList
.Add(localItem
, item
.HashValue
);
118 // process adds and edits
119 foreach (string file
in files
.Keys
)
121 // skip files we're not interested in here
122 if (IsExcludedFile(file
)) continue;
123 if (!File
.Exists(file
)) continue;
125 ProcessFile(itemList
, file
);
128 if (!OptionDeleted
) return;
130 // now process deletes
131 foreach (string key
in itemList
.Keys
)
133 // skip files that exist or we're not interested in
134 if (files
.ContainsKey(key
)) continue;
135 if (IsExcludedFile(key
)) continue;
137 Console
.WriteLine("Deleted: " + key
);
138 deletedFiles
.Add(key
);
142 private void ProcessDirectory(string path
)
144 char[] charsToTrim
= { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}
;
145 string itemPath
= path
.TrimEnd(charsToTrim
);
146 string serverPath
= workspace
.GetServerItemForLocalItem(itemPath
);
148 // pull item list based on WorkspaceVersion. otherwise might get
149 // new items on server that haven't been pulled yet in the list returned
150 WorkspaceVersionSpec version
= new WorkspaceVersionSpec(workspace
);
152 // process recursion settings
153 RecursionType rtype
= OptionRecursive
? RecursionType
.Full
: RecursionType
.OneLevel
;
154 bool recursionSetting
= Settings
.Current
.GetAsBool("Online.Recursive");
155 if (recursionSetting
) rtype
= RecursionType
.Full
;
156 SearchOption searchType
= (rtype
== RecursionType
.Full
) ? SearchOption
.AllDirectories
: SearchOption
.TopDirectoryOnly
;
158 // process command options
159 ItemSpec itemSpec
= new ItemSpec(itemPath
, rtype
);
160 ItemSet itemSet
= VersionControlServer
.GetItems(itemSpec
, version
, DeletedState
.NonDeleted
, ItemType
.Any
, true);
162 // get item list from TFS server
163 Item
[] items
= itemSet
.Items
;
164 SortedList
<string, byte[] > itemList
= new SortedList
<string, byte[] >(PathComparer
);
166 foreach (Item item
in items
)
168 if (item
.ServerItem
.Length
== serverPath
.Length
) continue;
169 string serverItem
= item
.ServerItem
.Remove(0, serverPath
.Length
+1);
171 // server item paths are separated with '/', but on windows the file list below has '\' separated paths
172 if (Path
.DirectorySeparatorChar
!= '/')
173 serverItem
= serverItem
.Replace('/', Path
.DirectorySeparatorChar
);
175 string fname
= Path
.Combine(itemPath
, serverItem
);
176 //Console.WriteLine(serverItem + " : " + fname);
178 itemList
.Add(fname
, item
.HashValue
);
181 DirectoryInfo dir
= new DirectoryInfo(path
);
182 FileInfo
[] localFiles
= dir
.GetFiles("*", searchType
);
184 SortedList
<string, bool> dirList
= new SortedList
<string, bool>();
185 foreach (FileInfo file
in localFiles
)
187 // skip files we're not interested in
188 if (IsExcludedFile(file
.Name
)) continue;
190 dirList
.Add(file
.FullName
, true);
191 ProcessFile(itemList
, file
.FullName
);
194 foreach (DirectoryInfo di
in dir
.GetDirectories("*", SearchOption
.AllDirectories
))
196 dirList
.Add(di
.FullName
, true);
199 if (!OptionDeleted
) return;
201 foreach (string key
in itemList
.Keys
)
203 // skip files that exist or we're not interested in
204 if (dirList
.ContainsKey(key
)) continue;
205 if (IsExcludedFile(key
)) continue;
207 Console
.WriteLine("Deleted: " + key
);
208 deletedFiles
.Add(key
);
212 private void Online(string[] args
)
214 if (args
.Length
== 0)
216 ProcessDirectory(Environment
.CurrentDirectory
);
220 SortedList
<string, bool> files
= new SortedList
<string, bool>();
221 foreach (string arg
in args
)
223 string path
= Path
.GetFullPath(arg
);
224 if (Directory
.Exists(path
)) ProcessDirectory(path
);
225 else files
.Add(path
, true);
228 if (files
.Count
> 0) ProcessFileList(files
);
231 public override void Run()
233 // must get server<->local mappings for GetServerItemForLocalItem
234 workspace
= GetWorkspaceFromCache();
235 workspace
.RefreshMappings();
237 // by default, if nothing specified we process all changes
238 if ((!OptionModified
) && (!OptionDeleted
) && (!OptionAdded
))
240 OptionModified
= OptionAdded
= OptionDeleted
= true;
244 if (OptionPreview
) return;
247 changes
+= workspace
.PendAdd(addedFiles
.ToArray(), false);
248 changes
+= workspace
.PendEdit(editedFiles
.ToArray(), RecursionType
.None
);
249 changes
+= workspace
.PendDelete(deletedFiles
.ToArray(), RecursionType
.None
);
250 Console
.WriteLine("{0} pending changes.", changes
);