root.node
[tfs.git] / tools / tf / LsFilesCommand.cs
blob169317c1439b2ba989c65092f19f9016d9afcb8a
1 //
2 // LsFilesCommand.cs
3 //
4 // Authors:
5 // Joel Reed (joelwreed@gmail.com)
6 //
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
29 using System;
30 using System.Security.Cryptography;
31 using System.Collections.Generic;
32 using System.IO;
33 using System.Text;
34 using Microsoft.TeamFoundation.Client;
35 using Microsoft.TeamFoundation.VersionControl.Common;
36 using Microsoft.TeamFoundation.VersionControl.Client;
37 using Mono.GetOptions;
39 [Command("ls-files", "Shows known, deleted, modified, old, or unknown files under the given path.")]
40 class LsFilesCommand : Command
42 [Option("Get all files, not just those out of date", "", "all")]
43 public bool OptionAll = false;
45 [Option("Look for deleted files.", "D", "deleted")]
46 public bool OptionDeleted = false;
48 [Option("Look for modified files.", "", "modified")]
49 public bool OptionModified = false;
51 [Option("Show out of date files (newer version on server)", "", "old")]
52 public bool OptionOld = false;
54 [Option("Show other/unknown files", "", "others")]
55 public bool OptionOthers = false;
57 [Option("Show writable files", "", "writable")]
58 public bool OptionWritable;
60 public LsFilesCommand(Driver driver, string[] args): base(driver, args)
64 public void ShowModifiedFiles(string itemPath, SortedList<string, string> itemList)
66 DirectoryInfo dir = new DirectoryInfo(itemPath);
67 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
68 foreach (FileInfo file in dir.GetFiles("*", SearchOption.AllDirectories))
70 if (!itemList.ContainsKey(file.FullName)) continue;
72 using (FileStream fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
74 md5.ComputeHash(fileStream);
75 string hash1 = Convert.ToBase64String(md5.Hash);
77 string hash2 = itemList[file.FullName];
78 if (hash1 != hash2)
79 Console.WriteLine(file.FullName);
84 public void ShowOtherFiles(string itemPath, SortedList<string, string> itemList)
86 DirectoryInfo dir = new DirectoryInfo(itemPath);
87 foreach (FileInfo file in dir.GetFiles("*", SearchOption.AllDirectories))
89 if (!itemList.ContainsKey(file.FullName))
91 Console.WriteLine(file.FullName);
96 public void ShowWritableFiles(string itemPath)
98 DirectoryInfo dir = new DirectoryInfo(itemPath);
99 foreach (FileInfo file in dir.GetFiles("*", SearchOption.AllDirectories))
101 if (IsExcludedFile(file.Name)) continue;
103 bool isReadOnly = (FileAttributes.ReadOnly == (File.GetAttributes(file.FullName) & FileAttributes.ReadOnly));
104 if (!isReadOnly) Console.WriteLine(file.FullName);
108 public void ShowDeletedFiles(string itemPath, SortedList<string, string> itemList)
110 SortedList<string, bool> dirList = new SortedList<string, bool>();
111 DirectoryInfo dir = new DirectoryInfo(itemPath);
113 foreach (FileInfo file in dir.GetFiles("*", SearchOption.AllDirectories))
115 dirList.Add(file.FullName, true);
118 foreach (DirectoryInfo di in dir.GetDirectories("*", SearchOption.AllDirectories))
120 dirList.Add(di.FullName, true);
123 foreach (string key in itemList.Keys)
125 if (!dirList.ContainsKey(key))
127 Console.WriteLine(key);
132 public void ShowOldFiles(Workspace workspace, ItemSpec itemSpec)
134 List<ItemSpec> itemSpecs = new List<ItemSpec>();
135 itemSpecs.Add(itemSpec);
137 ExtendedItem[][] items = workspace.GetExtendedItems(itemSpecs.ToArray(),
138 DeletedState.NonDeleted, ItemType.Any);
140 StringBuilder sb = new StringBuilder();
141 foreach (ExtendedItem[] itemArray in items)
143 foreach (ExtendedItem item in itemArray)
145 if (item.IsLatest || item.ItemType == ItemType.Folder) continue;
147 // new files will have a null local item
148 if (String.IsNullOrEmpty(item.LocalItem))
150 if (OptionAll)
151 sb.Append(String.Format("{0}\n", workspace.GetLocalItemForServerItem(item.SourceServerItem)));
153 else
154 Console.WriteLine(item.LocalItem);
158 if (OptionAll)
160 Console.WriteLine();
161 Console.WriteLine("New files on the server:");
162 Console.WriteLine();
163 Console.WriteLine(sb.ToString());
167 public override void Run()
169 string path = Environment.CurrentDirectory;
170 if (Arguments.Length > 0)
171 path = Path.GetFullPath(Arguments[0]);
173 char[] charsToTrim = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar};
174 string itemPath = path.TrimEnd(charsToTrim);
176 if (OptionWritable)
178 ShowWritableFiles(itemPath);
179 Environment.Exit(0);
182 Workspace workspace = GetWorkspaceFromCache();
183 workspace.RefreshMappings();
184 string serverPath = workspace.GetServerItemForLocalItem(itemPath);
186 // process command options
187 ItemSpec itemSpec = new ItemSpec(itemPath, RecursionType.Full);
189 if (OptionOld)
191 ShowOldFiles(workspace, itemSpec);
192 Environment.Exit(0);
195 // pull item list based on WorkspaceVersion. otherwise might get
196 // new items on server that haven't been pulled yet in the list returned
197 WorkspaceVersionSpec version = new WorkspaceVersionSpec(workspace);
198 ItemSet itemSet = VersionControlServer.GetItems(itemSpec, version, DeletedState.NonDeleted, ItemType.Any, true);
200 Item[] items = itemSet.Items;
201 SortedList<string, string > itemList = new SortedList<string, string >(PathComparer);
203 foreach (Item item in items)
205 if (item.ServerItem.Length == serverPath.Length) continue;
206 string serverItem = item.ServerItem.Remove(0, serverPath.Length+1);
207 string fname = Path.Combine(itemPath, serverItem);
209 string hash = "";
210 if (item.ItemType == ItemType.File)
211 hash = Convert.ToBase64String(item.HashValue);
213 itemList.Add(fname, hash);
216 if (OptionOthers) ShowOtherFiles(itemPath, itemList);
217 else if (OptionDeleted) ShowDeletedFiles(itemPath, itemList);
218 else if (OptionModified) ShowModifiedFiles(itemPath, itemList);
219 else
221 foreach (string key in itemList.Keys)
223 Console.WriteLine(key);