-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitinfo.pas
176 lines (153 loc) · 4.88 KB
/
gitinfo.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{
gitinfo: Git info utility
Author: Jerry Jian (emisjerry@gmail.com)
v0.01 2014/09/01: Initial revision
v0.02 2014/09/02: Move git prompt to another command
v0.03 2014/09/04: read .git/HEAD by git rev-parse command to get it properly
v0.04 2014/09/09: change help version
}
program gitinfo;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, CustApp, Windows, IniFiles, Dos, Process
{ you can add units after this };
type
{ TMyApplication }
TMyApplication = class(TCustomApplication)
protected
procedure DoRun; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
end;
const
_DEBUG: Boolean = true;
_VERSION:String = '0.4 2014/09/09';
{ TMyApplication }
function help(): Boolean;
begin
writeln('gitinfo - Git info utility v' + _VERSION + ' [written by Jerry Jian (emisjerry@gmail.com)]');
writeln('');
writeln('Display the information of working directory, as "svn info".');
writeln('Put git-info.exe to <Git innstall folder>\libexec\git-core will make git-info.exe as a Git command.');
writeln('git-info.exe will generate a git-prompt.bat which changes the prompt of your DOS-box.');
writeln('');
writeln('Settings file (git-info.ini) can be found in the git-info.exe''s folder.');
Result := true;
end;
function getHEADFile(): String;
var
_sResult, s: String;
begin
_sResult := '.git/HEAD';
// git rev-parse --show-toplevel will get the root folder
if (RunCommand('git', ['rev-parse', '--show-toplevel'], s)) then begin
s := Copy(s, 1, Length(s)-1); // remove the last #10
_sResult := s + '/' + _sResult;
end;
Result := _sResult;
end;
function exec(sTitle: String; sCmd: String; s: String): String;
var _sCmd, _sSubCmd, _sParam, _sRunResult: String;
_iPos: Integer;
begin
s := sTitle + #13#10;
_iPos := Pos(' ', sCmd);
if (_iPos > 0) then begin
_sCmd := Copy(sCmd, 1, _iPos-1);
_sParam := Copy(sCmd, _iPos+1, 1024);
_iPos := Pos(' ', _sParam);
if (_iPos > 0) then begin
_sSubCmd := Copy(_sParam, 1, _iPos-1);
_sParam := Copy(_sParam, _iPos+1, 1024);
end else begin
_sSubCmd := _sParam;
_sParam := '';
end;
end else begin
_sCmd := sCmd;
_sSubCmd := '';
_sParam := '';
end;
_sRunResult := '';
if RunCommand(_sCmd, [_sSubCmd, _sParam], _sRunResult) then s := Concat(s, _sRunResult);
Result := Concat(s, #13#10);
end;
procedure TMyApplication.DoRun;
var i: Integer;
_sParam, _sExeFileDir, _sCurrentDir, _sTitle, _sCmd: String;
s, _sResult, _sHEADFile: AnsiString;
_oIni: TIniFile;
begin
{ add your program here }
_sParam := '';
if (ParamCount = 1) then _sParam := ParamStr(1);
if (_sParam = '-?') or (_sParam = '-help') or (_sParam = '-h')then begin
help();
Terminate;
Exit;
end;
_sExeFileDir := ExtractFilePath(ExeName);
_sCurrentDir := GetCurrentDir();
_sHEADFile := GetHEADFile();
if not FileExists(_sHEADFile) then begin
WriteLn('This folder is not a Git working directory.');
Terminate;
Exit;
end;
_oIni := TIniFile.Create(_sExeFileDir + 'git-info.ini');
if not FileExists(_sExeFileDir + 'git-info.ini') then begin
_oIni.WriteString('Command', 'cmd1.title', '===Remote URL:');
_oIni.WriteString('Command', 'cmd1.exec', 'git remote -v');
_oIni.WriteString('Command', 'cmd2.title', '===All branches:');
_oIni.WriteString('Command', 'cmd2.exec', 'git branch -a');
_oIni.WriteString('Command', 'cmd3.title', '===Recent commits:');
_oIni.WriteString('Command', 'cmd3.exec', 'git log --pretty=format:"%C(yellow)%h %C(cyan)%ai %C(bold green)[%cn]%C(bold red)%d %C(bold green)%s%C(reset)" -10 --abbrev-commit --abbrev=4');
_oIni.WriteString('Command', 'cmd4.title', '===Current Status:');
_oIni.WriteString('Command', 'cmd4.exec', 'git status');
end;
_oIni.WriteString('Command', 'version', _VERSION);
_sResult := '';
for i:=1 to 99 do begin
_sTitle := _oIni.ReadString('Command', 'cmd' + IntToStr(i) + '.title', '');
if (_sTitle = '') then break;
_sCmd := _oIni.ReadString('Command', 'cmd' + IntToStr(i) + '.exec', '');
s := '';
s := exec(_sTitle, _sCmd, s);
_sResult := Concat(_sResult, s);
end;
_sResult := Concat(_sResult, '===Notes:'#13#10);
_sResult := Concat(_sResult, 'Help: git info -?');
_oIni.Free;
Writeln(_sResult);
Terminate;
Exit;
end;
constructor TMyApplication.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
StopOnException:=True;
end;
destructor TMyApplication.Destroy;
begin
inherited Destroy;
end;
procedure TMyApplication.WriteHelp;
begin
{ add your help code here }
{$IFDEF WINDOWS}
SetConsoleOutputCP(CP_UTF8);
{$ENDIF}
end;
var
Application: TMyApplication;
begin
Application:=TMyApplication.Create(nil);
Application.Title:='My Application';
Application.Run;
Application.Free;
end.