-
Notifications
You must be signed in to change notification settings - Fork 13
/
gitcmd.cpp
1475 lines (1370 loc) · 47.1 KB
/
gitcmd.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// gitcmd.cpp : Gitcommander plugin for total commander
//
//pluginst.inf
//
//Ideas from:
//https://github.com/libgit2/GitForDelphi/blob/binary/tests/git2.dll
//https://github.com/libgit2/GitForDelphi
//
//Usage - hints in TC
//first format - last commit info:
//[=gitcmd.Branch]([=gitcmd.CommitAge]) [=gitcmd.FirstRemoteUrl]\n[=gitcmd.CommitMessage]\n[=gitcmd.CommitAuthor] [=gitcmd.CommitMail] [=gitcmd.CommitDate.D.M.Y h:m:s]
//last format different for dires and for files: (authors choice)
//[=gitcmd.Branch] [=gitcmd.FirstRemoteUrl]\n[=gitcmd.FallIsLast] [=gitcmd.FallAge]\n[=gitcmd.FallMessage]\n[=gitcmd.FallAuthor] [=gitcmd.FallMail] [=gitcmd.FallDate.D.M.Y h:m:s]\n[=gitcmd.GeneralStatus]
//
//Usage - Column with branch instead of DIR - SizeAndBranch
//if C++ builder5:
#define _BCB
//normal defines:
#include "StdAfx.h"
#include "contentplug.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#include <algorithm>
//spec defines:
#ifdef _BCB
#define _BUILD_DLL_
//GIT: normally we should do this
//#include "include/git2.h"
//but we include GitForBCB, things ripped from libgit2:
#include "uGitForBCB.h"
#include "time.h"
#include <sys/stat.h>
#endif
//about the files - the lib is compiled libgit2
//the uGitForBCB.h is ripped things from git2.h because I was not able to incliude them properly (and the idea is based on git for delphi)
//from here on it should be compilable with anything...
//TODO:
//-cache pointers to repos
//-more unicodes, now iam just lucky to have it working
//-
//rest of normal code:
#define _detectstring ""
#define fieldcount 22
//general info groups:
enum EGitWantSrc{ENone,EThisCommit,ELastAffecting,ELastFallthrough};//the checkout-commit where we stand, the last commit affected file OR empty, the last commit affected file OR currentcheckout
enum EGitWant{EMsg,EAuthor,EMail,EDate,EAge, //can be specified from EGitWantSrc
EBranch,EFirstRemoteURL, //no specifiers, just general info
EFileStatus,//Status for file, empty for dirc
EGeneralStatus,
ESizeBranch,
EFallIsThis,
EDescription
};
//specific fields: cant be done by formatting because of datetime
enum EFields{
EFSizeBranch=0,
EFThisMsg,EFThisAuthor,EFThisMail,EFThisDate,EFThisAge, //commit where we stand
EFLastMsg,EFLastAuthor,EFLastMail,EFLastDate,EFLastAge, //last commit affecting file or empty string
EFFallMsg,EFFallAuthor,EFFallMail,EFFallDate,EFFallAge, //last commit affecting file or commit where we stand
EFBranch,EFFirstRemoteURL, //no specifiers, just general info
EFFileStatus,//Status for file, empty for dirc
EFGeneralStatus, //File+Status for files, "GIT dir." for dirs.
EFFallIsThis,
EFDescription
};
char* fieldnames[fieldcount]={
"SizeAndBranch", //Branch instead of <DIR>
"CommitMessage","CommitAuthor","CommitMail","CommitDate", "CommitAge",
"LastMessage","LastAuthor","LastMail","LastDate", "LastAge",
"FallMessage","FallAuthor","FallMail","FallDate", "FallAge",
"Branch","FirstRemoteUrl", //functionality for whole repo, commit we are standing at
"FileStatus","GeneralStatus",
"FallIsLast",
"FullDescription"
};
int fieldtypes[fieldcount]={
ft_numeric_floating,
ft_string,ft_string,ft_string,ft_datetime,ft_string,
ft_string,ft_string,ft_string,ft_datetime,ft_string,
ft_string,ft_string,ft_string,ft_datetime,ft_string,
ft_string,ft_string,
ft_string,
ft_string,
ft_string,
ft_string
};
char* fieldunits_and_multiplechoicestrings[fieldcount]={
"bytes|kbytes|Mbytes|Gbytes",
"","","","","",
"","","","","",
"","","","","",
"","",
"",
"",
"",
""};
int fieldflags[fieldcount]={
contflags_passthrough_size_float,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,
0,0,
0,
0
};
int sortorders[fieldcount]={
-1,
1,1,1,1,1,
1,1,1,1,1,
1,1,1,1,1,
1,1,
1,
1,
1
};
BOOL GetValueAborted=false;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
if (ul_reason_for_call==DLL_PROCESS_ATTACH)
{
srand( (unsigned)time( NULL ) );
git_libgit2_init();
}
if (ul_reason_for_call==DLL_PROCESS_DETACH)
{
git_libgit2_shutdown();
}
return TRUE;
}
void path_strip_filename(TCHAR *Path) {
size_t Len = _tcslen(Path);
if (Len==0) {return;};
size_t Idx = Len-1;
while (TRUE) {
TCHAR Chr = Path[Idx];
if (Chr==TEXT('\\')||Chr==TEXT('/')) {
if (Idx==0||Path[Idx-1]==':') {Idx++;};
break;
} else if (Chr==TEXT(':')) {
Idx++; break;
} else {
if (Idx==0) {break;} else {Idx--;};
};
};
Path[Idx] = TEXT('\0');
};
char* strlcpy(char* p,const char* p2,int maxlen)
{
if ((int)strlen(p2)>=maxlen) {
strncpy(p,p2,maxlen);
p[maxlen]=0;
} else
strcpy(p,p2);
return p;
}
TCHAR* _tcslcpy(TCHAR* p,const TCHAR* p2,int maxlen)
{
if ((int)_tcslen(p2)>=maxlen) {
_tcsncpy(p,p2,maxlen);
p[maxlen]=0;
} else
_tcscpy(p,p2);
return p;
}
TCHAR* _etcslcpy(TCHAR* p,const TCHAR* p2,int maxlen)
{
int xL=(int)_tcslen(p2);
if (xL>=maxlen)
{
xL=maxlen;
_tcsncpy(p,p2,maxlen);
p[maxlen]=0;
}
else
_tcscpy(p,p2);
return (TCHAR*)(p+xL);
}
PLUGFUNC int __stdcall ContentGetDetectString(char* DetectString,int maxlen)
{
strlcpy(DetectString,_detectstring,maxlen);
return 0;
}
PLUGFUNC int __stdcall ContentGetSupportedField(int FieldIndex,char* FieldName,char* Units,int maxlen)
{
if (FieldIndex==10000) {
strlcpy(FieldName,"Compare as text",maxlen-1);
Units[0]=0;
return ft_comparecontent;
}
if (FieldIndex<0 || FieldIndex>=fieldcount)
return ft_nomorefields;
strlcpy(FieldName,fieldnames[FieldIndex],maxlen-1);
strlcpy(Units,fieldunits_and_multiplechoicestrings[FieldIndex],maxlen-1);
return fieldtypes[FieldIndex];
}
int monthdays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
BOOL LeapYear(int year)
{
return ((year % 4)==0) && ((year % 100)!=0 || (year % 400==0));
}
bool dirExists(const char *path)
{
struct stat info;
if(stat( path, &info ) != 0)
return false;
else if(info.st_mode & S_IFDIR)
return true;
else
return false;
}
//times & howto .. at windows...
/* print as string: */
struct tm FILETIME_to_tm(const FILETIME *lpFileTime)
{
SYSTEMTIME st;
struct tm result;
FileTimeToSystemTime(lpFileTime,&st);
memset(&result,0,sizeof(struct tm));
result.tm_mday = st.wDay;
result.tm_mon = st.wMonth - 1;
result.tm_year = st.wYear - 1900;
result.tm_sec = st.wSecond;
result.tm_min = st.wMinute;
result.tm_hour = st.wHour;
return result;
}
TCHAR *ePrintTime(TCHAR *pTrgt,FILETIME *pIn, int pMaxLen)
{
struct tm xTm=FILETIME_to_tm(pIn);
return pTrgt+strftime(pTrgt, pMaxLen, "%d.%m.%Y %H:%I", &xTm);
}
/*TCHAR* PrintTime(TCHAR *pTarget,double pAgo,unsigned int pMaxLen)
{
int offset=origoffset;
char sign, out[32], msg[1024];
struct tm *intm;
int hours, minutes;
time_t t;
if (offset < 0) {
sign = '-';
offset = -offset;
} else {
sign = '+';
}
hours = offset / 60;
minutes = offset % 60;
t = (time_t)comtim + (origoffset * 60);
intm = gmtime(&t);
strftime(out, sizeof(out), "%a %b %e %T %Y", intm);
sprintf(msg,"%s%s %c%02d%02d\n", "Date: ", out, sign, hours, minutes);
return _etcslcpy((TCHAR*)FieldValue,msg,maxlen-1);
} */
TCHAR* PrintTimeAgo(TCHAR *pTarget,double pAgo,unsigned int pMaxLen)
{
#define NUMFMTSAGE 6
int xDifs[NUMFMTSAGE]={1,60,3600,24*3600,2629743,31556926};//seconds,minutes,hours,days,approxmonths,years
char *xNam[NUMFMTSAGE]={"secs.","mins","hrs.","days","months","years"};
int iDo=0;
while (iDo<NUMFMTSAGE)
{
if (pAgo/double(xDifs[iDo])<1.0)
break;
iDo++;
}
if (iDo >= 1)//the prev one
iDo--;
int xRes=int(0.5+pAgo/xDifs[iDo]);
char msg[512];
sprintf(msg,"%d %s", xRes, xNam[iDo]);
return _etcslcpy((TCHAR*)pTarget,msg,pMaxLen-1);
}
string number_fmt(__int64 n)
{
// cout << "(" << n << ")" << endl;
char s[128];
sprintf(s, "%Li", n);
string r(s);
reverse(r.begin(), r.end());
int space_inserted = 0;
size_t how_many_spaces = r.length() / 3;
if(r.length() % 3 != 0)
how_many_spaces += 1;
for(int i = 1; i < how_many_spaces; ++i)
{
r.insert(3 * i + space_inserted, " ");
space_inserted += 1;
}
reverse(r.begin(), r.end());
return r;
}
void TimetToFileTime( time_t t, LPFILETIME pft )
{
LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
pft->dwLowDateTime = (DWORD) ll;
pft->dwHighDateTime = ll >>32;
}
//git and caching repos (to come)
enum EPositionType{EPTNothing, //not git;
EPTGitOK, //The viewed directory has .git subfolder (filename is a directory)
EPTGitDirect, //this is the .git folder (filename is a directory)
EPTInside}; //this folder is some folder inside current repository
git_repository *CheckRepo(WIN32_FIND_DATA fd, TCHAR* FileName, EPositionType &pRet, bool pDoSearch, TCHAR *pRelFilePath)
{
char xTemp[2048];
strcpy(xTemp,FileName);
pRet=EPTNothing;
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) //for files - check the directory we are in
{
//strcpy(xTemp,FileName);
path_strip_filename(xTemp);
}
int xOriglen=strlen(xTemp);
git_repository *repo = NULL;
if (strstr(xTemp,"\\.git")==xTemp+xOriglen-5) //now we are INSIDE a repo and this is exactly the information for the invisible git dir
{
pRet=EPTGitDirect;
if (0!=git_repository_open(&repo, xTemp))
repo=NULL;
}
else
{
strcat(xTemp,"\\.git");
if (dirExists(xTemp)) // we are looking at the repo
{
pRet=EPTGitOK;
if (0!=git_repository_open(&repo, xTemp))
repo=NULL;
}
//else let git find it.... ..
else if (pDoSearch)
{
xTemp[xOriglen]=0;
if (0!=git_repository_open_ext(&repo, xTemp,0,NULL))
repo=NULL;
else
pRet=EPTInside;
}
}
if (pRelFilePath) //we want to know the relative path in git repo of this file.
{
if (repo && !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
const char *xRepdir=git_repository_path(repo); //returns different than windows slashes!!!
int xLastK=0;
for (int i=0;i<sizeof(xTemp);i++)
{
if (((FileName[i]=='\\' && xRepdir[i]=='/') || (FileName[i]=='/' && xRepdir[i]=='\\')))
xLastK=i;
if (FileName[i]==0 || xRepdir==0 ||
( tolower(FileName[i])!=tolower(xRepdir[i]) &&
((FileName[i]!='\\' || xRepdir[i]!='/') && (FileName[i]!='/' || xRepdir[i]!='\\')) ))
{
break;
}
}
xLastK=xLastK+1;//just after the "\\" or /
//(*pRelFilePath)=FileName+i;
strcpy(pRelFilePath,FileName+xLastK); //alternatively we can modify the original FileName, because xPtrFN points there. But we know it, ok.
for (int ii=0;ii<strlen(pRelFilePath);ii++)
{
if (pRelFilePath[ii]=='\\') pRelFilePath[ii]='/';//for git
}
}
else
pRelFilePath[0]=0;
}
return repo;
}
void ByeRepo(git_repository *pBye)
{
git_repository_free(pBye);
}
//end git
PLUGFUNC int __stdcall ContentGetValue(TCHAR* FileName,int FieldIndex,int UnitIndex,void* FieldValue,int maxlen,int flags)
{
WIN32_FIND_DATA fd;
FILETIME lt;
SYSTEMTIME st;
HANDLE fh;
DWORD handle;
DWORD dwSize,dayofyear;
int i;
__int64 filesize;
GetValueAborted=false;
if (_tcslen(FileName)<=3)
return ft_fileerror;
/*if (flags & CONTENT_DELAYIFSLOW) {
if (FieldIndex==17)
return ft_delayed;
if (FieldIndex==18)
return ft_ondemand;
} */
if (flags & CONTENT_PASSTHROUGH) {
switch (FieldIndex) {
case EFSizeBranch:
filesize=(__int64)*(double*)FieldValue;
switch (UnitIndex) {
case 1:
filesize/=1024;
break;
case 2:
filesize/=(1024*1024);
break;
case 3:
filesize/=(1024*1024*1024);
break;
}
*(double*)FieldValue=(double)filesize;
strlcpy(((char*)FieldValue)+8,number_fmt(filesize).c_str(),maxlen-8-1);
return ft_numeric_floating;
default:
return ft_nosuchfield;
}
}
//parsing what we want:
EGitWant xWant;
EGitWantSrc xWantFrom=ENone;
switch (FieldIndex)
{
default:
return ft_nosuchfield;
break;
case EFSizeBranch: xWant=ESizeBranch; break;
case EFBranch: xWant=EBranch; break;
case EFFirstRemoteURL: xWant=EFirstRemoteURL; break;
case EFFileStatus: xWant=EFileStatus; break;
case EFGeneralStatus: xWant=EGeneralStatus; break;
case EFFallIsThis: xWant=EFallIsThis; xWantFrom=ELastFallthrough; break;
case EFThisMsg:
case EFThisAuthor:
case EFThisMail:
case EFThisDate:
case EFThisAge:
xWant=EMsg+(FieldIndex-EFThisMsg);
xWantFrom=EThisCommit;
break;
case EFLastMsg:
case EFLastAuthor:
case EFLastMail:
case EFLastDate:
case EFLastAge:
xWant=EMsg+(FieldIndex-EFLastMsg);
xWantFrom=ELastAffecting;
break;
case EFFallMsg:
case EFFallAuthor:
case EFFallMail:
case EFFallDate:
case EFFallAge:
xWant=EMsg+(FieldIndex-EFFallMsg);
xWantFrom=ELastFallthrough;
break;
case EFDescription:
xWant=EDescription;
break;
}
bool xNeedfd=true; //everybody needs the information if file or directory
bool xStdGit= FieldIndex!=EFSizeBranch && xWant!=EDescription;//only size and allinfo is treated differently
bool xNeedRepo=true; //everybody (in case xStdGit) needs repo
bool xNeedCommit = xWantFrom!=ENone;
bool xNeedLastAffCommit= (xWantFrom==ELastFallthrough || xWantFrom==ELastAffecting);
//finito, now to actually do it.
if (xNeedfd)
fh=FindFirstFile(FileName,&fd);
else
fh=0;
if (fh!=INVALID_HANDLE_VALUE)
{
if (xNeedfd)
FindClose(fh);
if (xStdGit)
{
EPositionType xRet; //false -> let these functions return something ONLY on directories
git_repository *repo = NULL;
int rc=0;
git_commit * commit = NULL; /* the result */
git_oid oid_parent_commit; /* the SHA1 for last commit */
git_oid xLastAffecting;
git_commit *xFilesCommit = NULL;//the result commit last affecting given file
const char *branch = NULL;
bool xReturnEmpty=false;
bool xDirectory=fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
char xBufFN[2048];//filename for git
if (xNeedRepo)
repo=CheckRepo(fd,FileName,xRet,true,xBufFN);
if (repo) //possibility to display different things for different xRet;
{ //if (xBufFN[0]!=0)
//get commit and commit message
/* resolve HEAD into a SHA1 */
bool xDidError=false;
if (xNeedCommit || xNeedLastAffCommit)
{
rc = git_reference_name_to_id( &oid_parent_commit, repo, "HEAD" );
if ( rc == 0 )
{
/* get the actual commit structure */
rc = git_commit_lookup( &commit, repo, &oid_parent_commit );
if ( rc == 0 && xNeedLastAffCommit)
{
//we have the last commit. Now to walk it:
if ((xBufFN && xBufFN[0]!=0) && 0==git_commit_entry_last_commit_id(&xLastAffecting,repo,commit,xBufFN))
{
rc = git_commit_lookup( &xFilesCommit, repo, &xLastAffecting );
}
}
}
}
if (rc!=0)//error in getting what we wanted
{
xDidError=true;
if (fieldtypes[FieldIndex]==ft_string)
{
const git_error *error = giterr_last();
if (error)
_tcslcpy((TCHAR*)FieldValue,error->message,maxlen-1);
else
xReturnEmpty=true;
}
else
xReturnEmpty=true;
}
//special first and then commit infos.
if (!xDidError)//pre error
{
if (xWant==EBranch) //"CurrentBranch"
{
int error = 0;
const char *branch = NULL;
git_reference *head = NULL;
error = git_repository_head(&head, repo);
if (error == GIT_EUNBORNBRANCH || error == GIT_ENOTFOUND)
branch = NULL;
else if (!error) {
branch = git_reference_shorthand(head);
}
if (branch)
strlcpy(((char*)FieldValue),branch,maxlen-1);
else
strlcpy(((char*)FieldValue),"[HEAD]",maxlen-1);
git_reference_free(head);
}
else if (xWant==EFirstRemoteURL)
{
git_strarray xRs;
if (0==git_remote_list(&xRs,repo))
{
if (xRs.count>=1)
{
git_remote *remote=NULL;
if (0==git_remote_lookup(&remote,repo,xRs.strings[0]))//first repo
{
_tcslcpy((TCHAR*)FieldValue,git_remote_url(remote),maxlen-1);
git_remote_free(remote);
}
else
_tcslcpy((TCHAR*)FieldValue,xRs.strings[0],maxlen-1); //at least the remote name
}
else
_tcslcpy((TCHAR*)FieldValue,"Local rep.",maxlen-1); //at least the remote name
git_strarray_free(&xRs);
}
else
xReturnEmpty=true;
}
else if (xWant==EFileStatus || xWant==EGeneralStatus)
{
TCHAR *xTrgt;
if (xWant==EGeneralStatus && xDirectory)
{
_tcslcpy((TCHAR*)FieldValue,"File ",maxlen-1);
xTrgt=(TCHAR*)FieldValue + strlen((TCHAR*)FieldValue);
}
else xTrgt=(TCHAR*)FieldValue;
if ((!xDirectory && xWant==EGeneralStatus) || xWant==EFileStatus)
{
unsigned int xStatF=0;
if ((xBufFN && xBufFN[0]!=0) && 0==git_status_file(&xStatF,repo,xBufFN))
{
if (xStatF & GIT_STATUS_IGNORED)
_tcslcpy(xTrgt,"ignored",maxlen-1);
else if (xStatF & GIT_STATUS_WT_NEW)
_tcslcpy(xTrgt,"new",maxlen-1);
else if (xStatF & GIT_STATUS_WT_MODIFIED)
_tcslcpy(xTrgt,"modified",maxlen-1);
else if (xStatF & GIT_STATUS_WT_DELETED)
_tcslcpy(xTrgt,"deleted",maxlen-1);
else if (xStatF & GIT_STATUS_WT_TYPECHANGE)
_tcslcpy(xTrgt,"typechange",maxlen-1);
else if (xStatF & GIT_STATUS_WT_RENAMED)
_tcslcpy(xTrgt,"renamed",maxlen-1);
else if (xStatF & GIT_STATUS_WT_UNREADABLE)
_tcslcpy(xTrgt,"unreadable",maxlen-1);
}
if (xStatF==0)//nothing printed or error
{
//_tcslcpy((TCHAR*)FieldValue,xBufFN,maxlen-1);
//_tcslcpy((TCHAR*)FieldValue,git_repository_path(repo),maxlen-1);//debug del.
const git_error *error = giterr_last();
if (error)
_tcslcpy(xTrgt,error->message,maxlen-1);
else
_tcslcpy(xTrgt,"unchanged",maxlen-1);
}
}
else if (xDirectory && xWant==EGeneralStatus)
{
_tcslcpy((TCHAR*)FieldValue,"GIT dir",maxlen-1);
}
}
else //info o commitu
{
//EMsg,EAuthor,EMail,EDate,EAge
//EThisCommit,ELastAffecting,ELastFallthrough
if (xWantFrom==ELastAffecting && xFilesCommit==NULL)
{
xReturnEmpty=true;
}
else
{
git_commit * xPtrUse;
if (xWantFrom==EThisCommit || (xWantFrom==ELastFallthrough && xFilesCommit==NULL))
xPtrUse=commit;
else
xPtrUse=xFilesCommit;
if ( !xPtrUse )
xReturnEmpty=true;
else
{
switch (xWant)
{ //EMsg,EAuthor,EMail,EDate,EAge
case EFallIsThis:
{
if (xPtrUse==commit)
_tcslcpy((TCHAR*)FieldValue,"This commit",maxlen-1);
else
_tcslcpy((TCHAR*)FieldValue,"Last affecting",maxlen-1);
}
break;
case EMsg: //git last commitmessage
{
char *xCMsg=(char*)git_commit_summary(xPtrUse);//whole message: git_commit_message
_tcslcpy((TCHAR*)FieldValue,xCMsg,maxlen-1);
}
break;
case EAuthor: //"CommitAuthor"
case EMail:
{
const git_signature *xSig=git_commit_author(xPtrUse);
if (xSig)
{
switch (xWant)
{
case EAuthor: //name
_tcslcpy((TCHAR*)FieldValue,xSig->name,maxlen-1);
break;
case EMail: //email
_tcslcpy((TCHAR*)FieldValue,xSig->email,maxlen-1);
break;
}
}
}
break;
case EDate: //"CommitDatetime"
{
git_time_t comtim=git_commit_time(xPtrUse);
int origoffset=git_commit_time_offset(xPtrUse);
TimetToFileTime(comtim, ((FILETIME*)FieldValue));
}
break;
case EAge: //"CommitAge"
{
git_time_t comtim=git_commit_time(xPtrUse);
time_t xNow;
time(&xNow);
double xAgo=difftime(xNow, comtim);//difference in seconds
PrintTimeAgo((TCHAR*)FieldValue,xAgo,maxlen);
}
break;
}
}//else if not null
}
}
}//was not a predetectionof error
//cleanup:
if (xFilesCommit)
git_commit_free(xFilesCommit);
if (commit)
git_commit_free(commit);
ByeRepo(repo);
if (xReturnEmpty)
return ft_fieldempty;
}
else //else neni repo nevracime nic.
return ft_fieldempty;
}
else//operations out of git or special:
{
switch (FieldIndex)
{
case EFSizeBranch: // "size" + SPECIAL FOR GIT
{
filesize=fd.nFileSizeHigh;
filesize=(filesize<<32) + fd.nFileSizeLow;
switch (UnitIndex) {
case 1:
filesize/=1024;
break;
case 2:
filesize/=(1024*1024);
break;
case 3:
filesize/=(1024*1024*1024);
break;
}
*(__int64*)FieldValue=filesize;
//*(double*)FieldValue=filesize;
// alternate string
if (maxlen>12)
{
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) //for files:
{
strlcpy(((char*)FieldValue)+8,number_fmt(filesize).c_str(),maxlen-8-1);
}
else // if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//String to display... now this is the magic
{
EPositionType xRet;
git_repository *repo = CheckRepo(fd,FileName,xRet,false,NULL);//false - will not slow down if needs to find repo
bool xGitPrinted=false;
if (repo) //possibility to display different things for different xRet;
{
int error = 0;
const char *branch = NULL;
git_reference *head = NULL;
error = git_repository_head(&head, repo);
if (error == GIT_EUNBORNBRANCH || error == GIT_ENOTFOUND)
branch = NULL;
else if (!error) {
branch = git_reference_shorthand(head);
}
if (branch)
strlcpy(((char*)FieldValue)+8,branch,maxlen-8-1);
else
strlcpy(((char*)FieldValue)+8,"[HEAD]",maxlen-8-1);
git_reference_free(head);
ByeRepo(repo);
xGitPrinted=true;
}
if (!xGitPrinted)
strlcpy(((char*)FieldValue)+8,"<DIR>",maxlen-8-1);
}
}
}
break;
case EFDescription: //for hints in TC ...generally
{ //for future when allowed more than 2 lines per one call to getvalue
EPositionType xRet;
int rc=0;
git_commit * commit = NULL; /* the result */
git_oid oid_parent_commit; /* the SHA1 for last commit */
git_oid xLastAffecting;
git_commit *xFilesCommit = NULL;//the result commit last affecting given file
const char *branch = NULL;
bool xReturnEmpty=false;
bool xDirectory=fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
char xBufFN[2048];//filename for git
TCHAR *xOrigVal=(TCHAR*)FieldValue;
git_repository *repo=CheckRepo(fd,FileName,xRet,true,xBufFN);
bool xGitPrinted=false;
if (repo) //possibility to display different things for different xRet;
{
const char *branch = NULL;
git_reference *head = NULL;
//branch:
rc = git_repository_head(&head, repo);
if (rc == GIT_EUNBORNBRANCH || rc == GIT_ENOTFOUND)
branch = NULL;
else if (!rc) {
branch = git_reference_shorthand(head);
}
if (branch)
FieldValue=_etcslcpy((TCHAR*)FieldValue,branch,maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
else
FieldValue=_etcslcpy((TCHAR*)FieldValue,"[HEAD]",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
FieldValue=_etcslcpy((TCHAR*)FieldValue,", ",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
//first remote URL:
git_strarray xRs;
if (0==git_remote_list(&xRs,repo))
{
if (xRs.count>=1)
{
git_remote *remote=NULL;
if (0==git_remote_lookup(&remote,repo,xRs.strings[0]))//first repo
{
FieldValue=_etcslcpy((TCHAR*)FieldValue,git_remote_url(remote),maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
git_remote_free(remote);
}
else
FieldValue=_etcslcpy((TCHAR*)FieldValue,xRs.strings[0],maxlen-1-(xOrigVal-(TCHAR*)FieldValue));//at least the remote name
}
else
FieldValue=_etcslcpy((TCHAR*)FieldValue,"Local",maxlen-1-(xOrigVal-(TCHAR*)FieldValue)); //at least the remote name
git_strarray_free(&xRs);
FieldValue=_etcslcpy((TCHAR*)FieldValue,"\n",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
}
//about commits:
rc = git_reference_name_to_id( &oid_parent_commit, repo, "HEAD" );
if ( rc == 0 )
{
/* get the actual commit structure */
rc = git_commit_lookup( &commit, repo, &oid_parent_commit );
if ( rc == 0)
{
//we have the last commit. Now to walk it:
if ((xBufFN && xBufFN[0]!=0) && 0==git_commit_entry_last_commit_id(&xLastAffecting,repo,commit,xBufFN))
{
rc = git_commit_lookup( &xFilesCommit, repo, &xLastAffecting );
}
}
}
if (rc!=0)//error in getting what we wanted
{
if (fieldtypes[FieldIndex]==ft_string)//should be
{
const git_error *error = giterr_last();
if (error)
{
FieldValue=_etcslcpy((TCHAR*)FieldValue,error->message,maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
FieldValue=_etcslcpy((TCHAR*)FieldValue,"\n",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
}
}
}
else //can print more about commits
{
git_commit *xPtrUse;
if (xWantFrom==EThisCommit || (xWantFrom==ELastFallthrough && xFilesCommit==NULL))
xPtrUse=commit;
else
xPtrUse=xFilesCommit;
if ( xPtrUse )
{
git_time_t comtim=git_commit_time(xPtrUse);
if (xPtrUse==commit)
FieldValue=_etcslcpy((TCHAR*)FieldValue,"This ",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
else
FieldValue=_etcslcpy((TCHAR*)FieldValue,"Last ",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
time_t xNow;
time(&xNow);
double xAgo=difftime(xNow, comtim);//difference in seconds
FieldValue=PrintTimeAgo((TCHAR*)FieldValue,xAgo,maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
FieldValue=_etcslcpy((TCHAR*)FieldValue,"\n",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
//git last commitmessage
char *xCMsg=(char*)git_commit_summary(xPtrUse);//whole message: git_commit_message
FieldValue=_etcslcpy((TCHAR*)FieldValue,xCMsg,maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
FieldValue=_etcslcpy((TCHAR*)FieldValue,"\n",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
//CommitDatetime
//int origoffset=git_commit_time_offset(xPtrUse); todo...
FILETIME fT;
TimetToFileTime(comtim, &fT);
FieldValue=ePrintTime((TCHAR*)FieldValue,&fT,maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
//author
const git_signature *xSig=git_commit_author(xPtrUse);
if (xSig)
{
//name
FieldValue=_etcslcpy((TCHAR*)FieldValue,xSig->name,maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
FieldValue=_etcslcpy((TCHAR*)FieldValue," ",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
//email
FieldValue=_etcslcpy((TCHAR*)FieldValue,xSig->email,maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
}
FieldValue=_etcslcpy((TCHAR*)FieldValue,"\n",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
}//else if not null
} //end commit info
//about file and such:
TCHAR *xTrgt;
if (!xDirectory)
{
unsigned int xStatF=0;
if ((xBufFN && xBufFN[0]!=0) && 0==git_status_file(&xStatF,repo,xBufFN))
{
if (xStatF & GIT_STATUS_IGNORED)
FieldValue=_etcslcpy((TCHAR*)FieldValue,"ignored",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
else if (xStatF & GIT_STATUS_WT_NEW)
FieldValue=_etcslcpy((TCHAR*)FieldValue,"new",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
else if (xStatF & GIT_STATUS_WT_MODIFIED)
FieldValue=_etcslcpy((TCHAR*)FieldValue,"modified",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
else if (xStatF & GIT_STATUS_WT_DELETED)
FieldValue=_etcslcpy((TCHAR*)FieldValue,"deleted",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
else if (xStatF & GIT_STATUS_WT_TYPECHANGE)
FieldValue=_etcslcpy((TCHAR*)FieldValue,"typechange",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
else if (xStatF & GIT_STATUS_WT_RENAMED)
FieldValue=_etcslcpy((TCHAR*)FieldValue,"renamed",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
else if (xStatF & GIT_STATUS_WT_UNREADABLE)
FieldValue=_etcslcpy((TCHAR*)FieldValue,"unreadable",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
}
if (xStatF==0)//nothing printed or error
{
/*const git_error *error = giterr_last();
if (error)
_tcslcpy(xTrgt,error->message,maxlen-1);
else*/
FieldValue=_etcslcpy((TCHAR*)FieldValue,"unchanged",maxlen-1-(xOrigVal-(TCHAR*)FieldValue));
}
}
//[=gitcmd.Branch] [=gitcmd.FirstRemoteUrl]\n[=gitcmd.FallIsLast] [=gitcmd.FallAge]\n[=gitcmd.FallMessage]\n[=gitcmd.FallAuthor] [=gitcmd.FallMail] [=gitcmd.FallDate.D.M.Y h:m:s]\n[=gitcmd.GeneralStatus]
if (xFilesCommit)
git_commit_free(xFilesCommit);
if (commit)
git_commit_free(commit);
git_reference_free(head);
ByeRepo(repo);
xGitPrinted=true;
}