show the difference between two versions for a specified path
[tfs.git] / tools / opentf / GetCommand.cs
blobc8cf0d32b96a8c7addcbaba6542b592713bbc9d8
1 //
2 // GetCommand.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.Collections.Generic;
31 using System.IO;
32 using System.Text;
33 using Microsoft.TeamFoundation.Client;
34 using Microsoft.TeamFoundation.VersionControl.Client;
35 using Mono.GetOptions;
36 using OpenTF.Common;
38 [Command("get", "Update local repository copy with latest versions from the server.", "<path>...")]
39 class GetCommand : Command
41 [Option("Get all files, not just those out of date", "", "all")]
42 public bool OptionAll = false;
44 [Option("Force operation", "P", "force")]
45 public bool OptionForce = false;
47 [Option("Recursive", "R", "recursive")]
48 public bool OptionRecursive = false;
50 [Option("Version", "V", "version")]
51 public string OptionVersion;
53 [Option("Overwrite files that are writable", "", "overwrite")]
54 public bool OptionOverwrite = false;
56 SortedList<string, int> fileList = new SortedList<string, int>();
57 Workspace workspace;
59 public GetCommand(Driver driver, string[] args): base(driver, args)
61 workspace = GetWorkspaceFromCache();
62 VersionControlServer.Getting += MyGettingEventHandler;
65 public void MyGettingEventHandler(Object sender, GettingEventArgs e)
67 if (e.DeletionId != 0)
69 Console.WriteLine("deleting " + CanonicalPath(e.SourceLocalItem));
70 return;
73 if (String.IsNullOrEmpty(e.TargetLocalItem)) return;
75 if ((!String.IsNullOrEmpty(e.SourceLocalItem)) &&
76 (e.SourceLocalItem != e.TargetLocalItem))
77 Console.WriteLine("renaming " + CanonicalPath(e.TargetLocalItem));
78 else
79 Console.WriteLine("updating " + CanonicalPath(e.TargetLocalItem));
81 if (e.ItemType == ItemType.Folder) return;
82 fileList.Add(e.TargetLocalItem, e.Version);
85 public GetOptions GetOptionFlags()
87 GetOptions getOptions = GetOptions.None;
88 if (OptionAll || OptionForce) getOptions |= GetOptions.GetAll;
89 if (OptionOverwrite || OptionForce) getOptions |= GetOptions.Overwrite;
90 return getOptions;
93 public GetStatus UpdatePathFromServer(string[] paths)
95 // process command options
96 RecursionType folderRecursion = OptionRecursive ? RecursionType.Full : RecursionType.OneLevel;
97 bool getSetting = Settings.Current.GetAsBool("Get.Recursive");
98 if (getSetting) folderRecursion = RecursionType.Full;
100 VersionSpec version = VersionFromString(OptionVersion);
101 List<GetRequest> requests = new List<GetRequest>();
103 foreach (string path in paths)
105 RecursionType recursion = RecursionType.None;
106 if (Directory.Exists(path)) recursion = folderRecursion;
108 if (path[0] != '$')
109 requests.Add(new GetRequest(Path.GetFullPath(path), recursion, version));
110 else
111 requests.Add(new GetRequest(path, recursion, version));
114 return workspace.Get(requests.ToArray(), GetOptionFlags());
117 public void SetPermissions(SortedList<string, int> fileList)
119 Console.Write("Setting permissions ...");
121 int i = 0;
122 foreach (string file in fileList.Keys)
124 if (0 == (i % 100)) Console.Write(".");
125 i++;
127 if (! FileTypeDatabase.ShouldBeExecutable(file)) continue;
128 FileType.MakeExecutable(file);
131 Console.WriteLine();
132 Console.WriteLine("Done!");
135 public override void Run()
137 List<string> paths = new List<string>();
138 foreach (string p in Arguments) paths.Add(p);
140 if (paths.Count == 0 && Settings.Current.GetAsBool("Get.DefaultToCwd"))
141 paths.Add(Environment.CurrentDirectory);
143 GetStatus status;
144 if (paths.Count > 0)
145 status = UpdatePathFromServer(paths.ToArray());
146 else
147 status = workspace.Get(VersionFromString(OptionVersion), GetOptionFlags());
149 if (status.NumOperations == 0)
151 Console.WriteLine("Nothing to do.");
152 Environment.Exit((int)ExitCode.PartialSuccess);
155 SetPermissions(fileList);