This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitpluscloc.cpp
102 lines (86 loc) · 2.35 KB
/
gitpluscloc.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
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <algorithm>
#include "preferences.h"
using namespace std;
#define VERSION "1.2.1"
void pause() {
cin.clear();
cin.get();
}
string exec(const char* cmd) {
cout << "$ " << cmd << endl;
char buffer[1024];
string result = "";
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
FILE* pipe = _popen(cmd, "r");
#else
FILE* pipe = popen(cmd, "r");
#endif
if (!pipe)
throw runtime_error("_popen() failed");
try {
while (!feof(pipe)) {
if (fgets(buffer, 1024, pipe) != NULL)
result += buffer;
}
}
catch (...) {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
_pclose(pipe);
#else
pclose(pipe);
#endif
throw;
}
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
_pclose(pipe);
#else
pclose(pipe);
#endif
return result;
}
bool cmd_option_exists(char** begin, char** end, const std::string& option) {
return std::find(begin, end, option) != end;
}
void create_cloc() {
cout << "Creating '" << CLOC_LOG_FILE << "' file...\n";
string cloccmd = "cloc ./ ";
cloccmd += CLOC_ARGUMENTS;
string execout = exec(cloccmd.c_str());
ofstream fw(CLOC_LOG_FILE);
fw << execout;
fw.close();
}
int main(int argc, char** argv) {
if (cmd_option_exists(argv, argv + argc, "--help")) {
cout << endl
<< "gitpluscloc (cgit) v." << VERSION << endl
<< "(c) 2018 Ringo Hoffmann (zekro Development)\n\n"
<< "This tool does just pass all arguments given through to git\n"
<< "and extending commit with adding cloc document.\n";
return 0;
}
string git_cmd = "git ";
for (int i = 1; i < argc; i++) {
string tmp = argv[i];
if (tmp.find(" ", 0) < 4294967295) {
git_cmd += "\"";
git_cmd += argv[i];
git_cmd += "\"";
}
else
git_cmd += argv[i];
git_cmd += " ";
}
if (cmd_option_exists(argv, argv + argc, "commit")) {
create_cloc();
string _cmd = "git add ";
_cmd += CLOC_LOG_FILE;
cout << exec(_cmd.c_str());
}
cout << exec(git_cmd.c_str());
}