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 \"brief\" or \"detailed\".", "F", "format")]
43 public string OptionFormat
= "";
45 [Option("Recursive", "R", "recursive")]
46 public bool OptionRecursive
= false;
48 [Option("Stop After", "", "stopafter")]
49 public int OptionStopAfter
= -1;
51 public HistoryCommand(Driver driver
, string[] args
): base(driver
, args
)
55 public override void Run()
57 bool defaultCwdSetting
= Settings
.Current
.GetAsBool("History.DefaultToCwd");
58 string path
= Environment
.CurrentDirectory
;
60 if (Arguments
.Length
> 1) path
= Arguments
[1];
61 else if (!defaultCwdSetting
)
63 Console
.WriteLine("The history command takes exactly one item.");
67 if (!VersionControlPath
.IsServerItem(path
)) path
= Path
.GetFullPath(path
);
69 RecursionType rtype
= OptionRecursive
? RecursionType
.Full
: RecursionType
.None
;
70 bool histSetting
= Settings
.Current
.GetAsBool("History.Recursive");
71 if (histSetting
) rtype
= RecursionType
.Full
;
72 bool detailed
= OptionFormat
.Equals("detailed", StringComparison
.InvariantCultureIgnoreCase
);
74 int stopAfter
= Settings
.Current
.GetAsInt("History.StopAfter");
75 if (OptionStopAfter
!= -1) stopAfter
= OptionStopAfter
;
77 IEnumerable changeSets
= VersionControlServer
.QueryHistory(path
, VersionSpec
.Latest
, 0, rtype
, null,
78 null, null, stopAfter
, detailed
, false, false);
80 int maxId
= 2, maxOwner
= 5, maxDate
= 6;
81 foreach (Changeset changeSet
in changeSets
)
83 string id
= Convert
.ToString(changeSet
.ChangesetId
);
84 if (id
.Length
> maxId
) maxId
= id
.Length
;
86 string date
= changeSet
.CreationDate
.ToString("d");
87 if (date
.Length
> maxDate
) maxDate
= date
.Length
;
89 // domain is stripped on output
90 int ownerNameLen
= changeSet
.Owner
.Length
;
91 int slash
= changeSet
.Owner
.IndexOf('\\');
92 if (-1 != slash
) ownerNameLen
= changeSet
.Owner
.Length
- slash
;
94 if (ownerNameLen
> maxOwner
)
96 maxOwner
= ownerNameLen
;
100 int maxComment
= WindowWidth
- maxId
- maxOwner
- maxDate
- 5;
102 foreach (Changeset changeSet
in changeSets
)
104 string comment
= "none";
105 if (changeSet
.Comment
!= null)
107 if (changeSet
.Comment
.Length
> maxComment
)
108 comment
= changeSet
.Comment
.Remove(maxComment
);
110 comment
= changeSet
.Comment
;
113 // domain is stripped on output
114 string owner
= changeSet
.Owner
;
115 int slash
= changeSet
.Owner
.IndexOf('\\');
118 owner
= changeSet
.Owner
.Substring(slash
+1);
121 string line
= String
.Format("{0} {1} {2} {3}",
122 Convert
.ToString(changeSet
.ChangesetId
).PadRight(maxId
),
123 owner
.PadRight(maxOwner
),
124 changeSet
.CreationDate
.ToString("d").PadRight(maxDate
),
127 Console
.WriteLine(line
);
128 if (!detailed
) continue;
130 foreach (Change change
in changeSet
.Changes
)
132 Console
.WriteLine(" " + ChangeTypeToString(change
.ChangeType
) + " " + change
.Item
.ServerItem
);