5 // Joel Reed (joelwreed@gmail.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System
.Collections
;
31 using System
.Collections
.Generic
;
34 using Microsoft
.TeamFoundation
.Client
;
35 using Microsoft
.TeamFoundation
.VersionControl
.Client
;
36 using Microsoft
.TeamFoundation
.VersionControl
.Common
;
37 using Mono
.GetOptions
;
39 [Command("history", "Display changelog history for specified file.", "hist")]
40 class HistoryCommand
: Command
42 [Option("Format", "F", "format")]
43 public string OptionFormat
= "";
45 [Option("Stop After", "", "stopafter")]
46 public int OptionStopAfter
= 256;
48 public HistoryCommand(Driver driver
, string[] args
): base(driver
, args
)
52 public override void Run()
54 bool defaultCwdSetting
= Settings
.Current
.GetAsBool("History.DefaultToCwd");
55 string path
= Environment
.CurrentDirectory
;
57 if (Arguments
.Length
> 1) path
= Arguments
[1];
58 else if (!defaultCwdSetting
)
60 Console
.WriteLine("The history command takes exactly one item.");
64 if (!VersionControlPath
.IsServerItem(path
)) path
= Path
.GetFullPath(path
);
66 RecursionType rtype
= OptionRecursive
? RecursionType
.Full
: RecursionType
.None
;
67 bool histSetting
= Settings
.Current
.GetAsBool("History.Recursive");
68 if (histSetting
) rtype
= RecursionType
.Full
;
69 bool detailed
= OptionFormat
.Equals("detailed", StringComparison
.InvariantCultureIgnoreCase
);
71 IEnumerable changeSets
= VersionControlServer
.QueryHistory(path
, VersionSpec
.Latest
, 0, rtype
, null,
72 null, null, OptionStopAfter
, detailed
, false, false);
74 int maxId
= 2, maxOwner
= 5, maxDate
= 6;
75 foreach (Changeset changeSet
in changeSets
)
77 string id
= Convert
.ToString(changeSet
.ChangesetId
);
78 if (id
.Length
> maxId
) maxId
= id
.Length
;
80 string date
= changeSet
.CreationDate
.ToString("d");
81 if (date
.Length
> maxDate
) maxDate
= date
.Length
;
83 // domain is stripped on output
84 int ownerNameLen
= changeSet
.Owner
.Length
;
85 int slash
= changeSet
.Owner
.IndexOf('\\');
86 if (-1 != slash
) ownerNameLen
= changeSet
.Owner
.Length
- slash
;
88 if (ownerNameLen
> maxOwner
)
90 maxOwner
= ownerNameLen
;
94 int maxComment
= WindowWidth
- maxId
- maxOwner
- maxDate
- 5;
96 foreach (Changeset changeSet
in changeSets
)
98 string comment
= "none";
99 if (changeSet
.Comment
!= null)
101 if (changeSet
.Comment
.Length
> maxComment
)
102 comment
= changeSet
.Comment
.Remove(maxComment
);
104 comment
= changeSet
.Comment
;
107 // domain is stripped on output
108 string owner
= changeSet
.Owner
;
109 int slash
= changeSet
.Owner
.IndexOf('\\');
112 owner
= changeSet
.Owner
.Substring(slash
+1);
115 string line
= String
.Format("{0} {1} {2} {3}",
116 Convert
.ToString(changeSet
.ChangesetId
).PadRight(maxId
),
117 owner
.PadRight(maxOwner
),
118 changeSet
.CreationDate
.ToString("d").PadRight(maxDate
),
121 Console
.WriteLine(line
);
122 if (!detailed
) continue;
124 foreach (Change change
in changeSet
.Changes
)
126 Console
.WriteLine(" " + change
.ChangeType
.ToString().ToLower() + " " + change
.Item
.ServerItem
);