BUGFIX: tf online <path> shouldn't croak if <path> is an add awaiting
[tfs.git] / tools / opentf / GetCommand.cs
blob6519b50ade61449cbce59db893a5df7b1a44cde9
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;
37 [Command("get", "Update local repository copy with latest versions from the server.", "<path>...")]
38 class GetCommand : Command
40 [Option("Get all files, not just those out of date", "", "all")]
41 public bool OptionAll = false;
43 [Option("Force operation", "P", "force")]
44 public bool OptionForce = false;
46 [Option("Recursive", "R", "recursive")]
47 public bool OptionRecursive = false;
49 [Option("Version", "V", "version")]
50 public string OptionVersion;
52 [Option("Overwrite files that are writable", "", "overwrite")]
53 public bool OptionOverwrite = false;
55 List<string> fileList = new List<string>();
56 Workspace workspace;
58 public GetCommand(Driver driver, string[] args): base(driver, args)
60 workspace = GetWorkspaceFromCache();
61 VersionControlServer.Getting += MyGettingEventHandler;
64 public void MyGettingEventHandler(Object sender, GettingEventArgs e)
66 if (e.DeletionId != 0)
68 Console.WriteLine("deleting " + CanonicalPath(e.SourceLocalItem));
69 return;
72 if ((!String.IsNullOrEmpty(e.TargetLocalItem))&&
73 (!String.IsNullOrEmpty(e.SourceLocalItem))&&
74 (e.SourceLocalItem != e.TargetLocalItem))
75 Console.WriteLine("renaming " + CanonicalPath(e.TargetLocalItem));
76 else
77 Console.WriteLine("updating " + CanonicalPath(e.TargetLocalItem));
79 if (e.ItemType == ItemType.Folder) return;
80 fileList.Add(e.TargetLocalItem);
83 public GetOptions GetOptionFlags()
85 GetOptions getOptions = GetOptions.None;
86 if (OptionAll || OptionForce) getOptions |= GetOptions.GetAll;
87 if (OptionOverwrite || OptionForce) getOptions |= GetOptions.Overwrite;
88 return getOptions;
91 public GetStatus UpdatePathFromServer(string[] paths)
93 // process command options
94 RecursionType folderRecursion = OptionRecursive ? RecursionType.Full : RecursionType.OneLevel;
95 bool getSetting = Settings.Current.GetAsBool("Get.Recursive");
96 if (getSetting) folderRecursion = RecursionType.Full;
98 VersionSpec version = VersionFromString(OptionVersion);
99 List<GetRequest> requests = new List<GetRequest>();
101 foreach (string path in paths)
103 RecursionType recursion = RecursionType.None;
104 if (Directory.Exists(path)) recursion = folderRecursion;
106 if (path[0] != '$')
107 requests.Add(new GetRequest(Path.GetFullPath(path), recursion, version));
108 else
109 requests.Add(new GetRequest(path, recursion, version));
112 return workspace.Get(requests.ToArray(), GetOptionFlags());
115 public override void Run()
117 List<string> paths = new List<string>();
118 foreach (string p in Arguments) paths.Add(p);
120 if (paths.Count == 0 && Settings.Current.GetAsBool("Get.DefaultToCwd"))
121 paths.Add(Environment.CurrentDirectory);
123 GetStatus status;
124 if (paths.Count > 0)
125 status = UpdatePathFromServer(paths.ToArray());
126 else
127 status = workspace.Get(VersionFromString(OptionVersion), GetOptionFlags());
129 if (status.NumOperations == 0)
131 Console.WriteLine("Nothing to do.");
132 Environment.Exit((int)ExitCode.PartialSuccess);
135 FileTypeDatabase.SetPermissions(fileList);