give command only its args not all args
[tfs.git] / tools / tf / LsFilesCommand.cs
blobca08a56f328095588207deb970183ac39fd0d5bb
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;
38 [Command("ls-files", "Shows known, deleted, modified, old, or unknown files under the given path.")]
39 class LsFilesCommand : Command
41 public LsFilesCommand(Driver driver, string[] args): base(driver, args)
45 public void ShowModifiedFiles(string itemPath, SortedList<string, string> itemList)
47 DirectoryInfo dir = new DirectoryInfo(itemPath);
48 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
49 foreach (FileInfo file in dir.GetFiles("*", SearchOption.AllDirectories))
51 if (!itemList.ContainsKey(file.FullName)) continue;
53 using (FileStream fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
55 md5.ComputeHash(fileStream);
56 string hash1 = Convert.ToBase64String(md5.Hash);
58 string hash2 = itemList[file.FullName];
59 if (hash1 != hash2)
60 Console.WriteLine(file.FullName);
65 public void ShowOtherFiles(string itemPath, SortedList<string, string> itemList)
67 DirectoryInfo dir = new DirectoryInfo(itemPath);
68 foreach (FileInfo file in dir.GetFiles("*", SearchOption.AllDirectories))
70 if (!itemList.ContainsKey(file.FullName))
72 Console.WriteLine(file.FullName);
77 public void ShowWritableFiles(string itemPath)
79 DirectoryInfo dir = new DirectoryInfo(itemPath);
80 foreach (FileInfo file in dir.GetFiles("*", SearchOption.AllDirectories))
82 bool isReadOnly = (FileAttributes.ReadOnly == (File.GetAttributes(file.FullName) & FileAttributes.ReadOnly));
83 if (!isReadOnly) Console.WriteLine(file.FullName);
87 public void ShowDeletedFiles(string itemPath, SortedList<string, string> itemList)
89 SortedList<string, bool> dirList = new SortedList<string, bool>();
90 DirectoryInfo dir = new DirectoryInfo(itemPath);
92 foreach (FileInfo file in dir.GetFiles("*", SearchOption.AllDirectories))
94 dirList.Add(file.FullName, true);
97 foreach (DirectoryInfo di in dir.GetDirectories("*", SearchOption.AllDirectories))
99 dirList.Add(di.FullName, true);
102 foreach (string key in itemList.Keys)
104 if (!dirList.ContainsKey(key))
106 Console.WriteLine(key);
111 public void ShowOldFiles(Workspace workspace, ItemSpec itemSpec)
113 List<ItemSpec> itemSpecs = new List<ItemSpec>();
114 itemSpecs.Add(itemSpec);
116 ExtendedItem[][] items = workspace.GetExtendedItems(itemSpecs.ToArray(),
117 DeletedState.NonDeleted, ItemType.Any);
119 StringBuilder sb = new StringBuilder();
120 foreach (ExtendedItem[] itemArray in items)
122 foreach (ExtendedItem item in itemArray)
124 if (item.IsLatest || item.ItemType == ItemType.Folder) continue;
126 // new files will have a null local item
127 if (String.IsNullOrEmpty(item.LocalItem))
129 if (Options.All)
130 sb.Append(String.Format("{0}\n", workspace.GetLocalItemForServerItem(item.SourceServerItem)));
132 else
133 Console.WriteLine(item.LocalItem);
137 if (Options.All)
139 Console.WriteLine();
140 Console.WriteLine("New files on the server:");
141 Console.WriteLine();
142 Console.WriteLine(sb.ToString());
146 public override void Run()
148 string path = Environment.CurrentDirectory;
149 if (Arguments.Length > 1)
151 path = Path.GetFullPath(Arguments[1]);
154 char[] charsToTrim = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar};
155 string itemPath = path.TrimEnd(charsToTrim);
157 if (Options.Writable)
159 ShowWritableFiles(itemPath);
160 Environment.Exit(0);
163 Workspace workspace = GetWorkspaceFromCache();
164 workspace.RefreshMappings();
165 string serverPath = workspace.GetServerItemForLocalItem(itemPath);
167 // process command options
168 ItemSpec itemSpec = new ItemSpec(itemPath, RecursionType.Full);
170 if (Options.Old)
172 ShowOldFiles(workspace, itemSpec);
173 Environment.Exit(0);
176 // pull item list based on WorkspaceVersion. otherwise might get
177 // new items on server that haven't been pulled yet in the list returned
178 WorkspaceVersionSpec version = new WorkspaceVersionSpec(workspace);
179 ItemSet itemSet = VersionControlServer.GetItems(itemSpec, version, DeletedState.NonDeleted, ItemType.Any, true);
181 Item[] items = itemSet.Items;
182 SortedList<string, string > itemList = new SortedList<string, string >(PathComparer);
184 foreach (Item item in items)
186 if (item.ServerItem.Length == serverPath.Length) continue;
187 string serverItem = item.ServerItem.Remove(0, serverPath.Length+1);
188 string fname = Path.Combine(itemPath, serverItem);
190 string hash = "";
191 if (item.ItemType == ItemType.File)
192 hash = Convert.ToBase64String(item.HashValue);
194 itemList.Add(fname, hash);
197 if (Options.Others) ShowOtherFiles(itemPath, itemList);
198 else if (Options.Deleted) ShowDeletedFiles(itemPath, itemList);
199 else if (Options.Modified) ShowModifiedFiles(itemPath, itemList);
200 else
202 foreach (string key in itemList.Keys)
204 Console.WriteLine(key);