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("Limit the number of changesets shown", "", "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
> 0) path
= Arguments
[0];
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
;
73 bool detailed
= Settings
.Current
.GetAsBool("History.Detailed");
74 if (!String
.IsNullOrEmpty(OptionFormat
))
75 detailed
= OptionFormat
.Equals("detailed", StringComparison
.InvariantCultureIgnoreCase
);
77 int stopAfter
= Settings
.Current
.GetAsInt("History.StopAfter");
78 if (OptionStopAfter
!= -1) stopAfter
= OptionStopAfter
;
80 IEnumerable changeSets
= VersionControlServer
.QueryHistory(path
, VersionSpec
.Latest
, 0, rtype
, null,
81 null, null, stopAfter
, detailed
, false, false);
83 int maxId
= 2, maxOwner
= 5, maxDate
= 6;
84 foreach (Changeset changeSet
in changeSets
)
86 string id
= Convert
.ToString(changeSet
.ChangesetId
);
87 if (id
.Length
> maxId
) maxId
= id
.Length
;
89 string date
= changeSet
.CreationDate
.ToString("d");
90 if (date
.Length
> maxDate
) maxDate
= date
.Length
;
92 // domain is stripped on output
93 int ownerNameLen
= changeSet
.Owner
.Length
;
94 int slash
= changeSet
.Owner
.IndexOf('\\');
95 if (-1 != slash
) ownerNameLen
= changeSet
.Owner
.Length
- slash
;
97 if (ownerNameLen
> maxOwner
)
99 maxOwner
= ownerNameLen
;
103 int maxComment
= WindowWidth
- maxId
- maxOwner
- maxDate
- 5;
105 foreach (Changeset changeSet
in changeSets
)
107 string comment
= "none";
108 if (changeSet
.Comment
!= null)
110 if (changeSet
.Comment
.Length
> maxComment
)
111 comment
= changeSet
.Comment
.Remove(maxComment
);
113 comment
= changeSet
.Comment
;
116 // domain is stripped on output
117 string owner
= changeSet
.Owner
;
118 int slash
= changeSet
.Owner
.IndexOf('\\');
121 owner
= changeSet
.Owner
.Substring(slash
+1);
124 string line
= String
.Format("{0} {1} {2} {3}",
125 Convert
.ToString(changeSet
.ChangesetId
).PadRight(maxId
),
126 owner
.PadRight(maxOwner
),
127 changeSet
.CreationDate
.ToString("d").PadRight(maxDate
),
130 Console
.WriteLine(line
);
131 if (!detailed
) continue;
133 foreach (Change change
in changeSet
.Changes
)
135 Console
.WriteLine(" " + ChangeTypeToString(change
.ChangeType
) + " " + change
.Item
.ServerItem
);