move.filetypedb.to.OpenTF.Common
[tfs.git] / tools / opentf / GetCommand.cs
blob77fdc58ec5ff78d5ce15d18eba3011944c6a565d
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 List<string> fileList = new List<string>();
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))&&
74 (!String.IsNullOrEmpty(e.SourceLocalItem))&&
75 (e.SourceLocalItem != e.TargetLocalItem))
76 Console.WriteLine("renaming " + CanonicalPath(e.TargetLocalItem));
77 else
78 Console.WriteLine("updating " + CanonicalPath(e.TargetLocalItem));
80 if (e.ItemType == ItemType.Folder) return;
81 fileList.Add(e.TargetLocalItem);
84 public GetOptions GetOptionFlags()
86 GetOptions getOptions = GetOptions.None;
87 if (OptionAll || OptionForce) getOptions |= GetOptions.GetAll;
88 if (OptionOverwrite || OptionForce) getOptions |= GetOptions.Overwrite;
89 return getOptions;
92 public GetStatus UpdatePathFromServer(string[] paths)
94 // process command options
95 RecursionType folderRecursion = OptionRecursive ? RecursionType.Full : RecursionType.OneLevel;
96 bool getSetting = Settings.Current.GetAsBool("Get.Recursive");
97 if (getSetting) folderRecursion = RecursionType.Full;
99 VersionSpec version = VersionFromString(OptionVersion);
100 List<GetRequest> requests = new List<GetRequest>();
102 foreach (string path in paths)
104 RecursionType recursion = RecursionType.None;
105 if (Directory.Exists(path)) recursion = folderRecursion;
107 if (path[0] != '$')
108 requests.Add(new GetRequest(Path.GetFullPath(path), recursion, version));
109 else
110 requests.Add(new GetRequest(path, recursion, version));
113 return workspace.Get(requests.ToArray(), GetOptionFlags());
116 public static void SetPermissions(List<string> fileList)
118 Console.Write("Setting permissions...");
120 int i = 0;
121 foreach (string file in fileList)
123 if (0 == (i % 100)) Console.Write(".");
124 i++;
126 if (! FileTypeDatabase.ShouldBeExecutable(file)) continue;
127 FileType.MakeExecutable(file);
130 Console.WriteLine(" done!");
133 public override void Run()
135 List<string> paths = new List<string>();
136 foreach (string p in Arguments) paths.Add(p);
138 if (paths.Count == 0 && Settings.Current.GetAsBool("Get.DefaultToCwd"))
139 paths.Add(Environment.CurrentDirectory);
141 GetStatus status;
142 if (paths.Count > 0)
143 status = UpdatePathFromServer(paths.ToArray());
144 else
145 status = workspace.Get(VersionFromString(OptionVersion), GetOptionFlags());
147 if (status.NumOperations == 0)
149 Console.WriteLine("Nothing to do.");
150 Environment.Exit((int)ExitCode.PartialSuccess);
153 SetPermissions(fileList);