-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitInfo.cpp
152 lines (131 loc) · 4.11 KB
/
GitInfo.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
#include "GitInfo.h"
bool is_git_configured()
{
//checks that git is configured
std::string str;
int ret_val = system("git config --global user.email > .nsm-git-email.txt");
if(ret_val)
{
start_err(std::cout) << "is_git_configured(): system('git config --global user.email > .nsm-git-email.txt') failed in " << quote(get_pwd()) << std::endl;
remove_file(Path("./", ".nsm-git-email.txt"));
return 0;
}
std::ifstream ifs(".nsm-git-email.txt");
ifs >> str;
ifs.close();
remove_file(Path("./", ".nsm-git-email.txt"));
if(str == "")
{
std::cout << "*** Please tell me who you are." << std::endl;
std::cout << std::endl;
std::cout << "Run" << std::endl;
std::cout << std::endl;
std::cout << " nsm config --global user.email \"you@example.com\"" << std::endl;
std::cout << " nsm config --global user.name \"Your Username\"" << std::endl;
std::cout << std::endl;
std::cout << "to set your account's default identity." << std::endl;
return 0;
}
ret_val = system("git config --global user.name > .nsm-git-username.txt");
if(ret_val)
{
start_err(std::cout) << "is_git_configured(): system('git config --global user.name > .nsm-git-username.txt') failed in " << quote(get_pwd()) << std::endl;
remove_file(Path("./", ".nsm-git-username.txt"));
return 0;
}
ifs.open(".nsm-git-username.txt");
ifs >> str;
ifs.close();
remove_file(Path("./", ".nsm-git-username.txt"));
if(str == "")
{
std::cout << "*** Please tell me who you are." << std::endl;
std::cout << std::endl;
std::cout << "Run" << std::endl;
std::cout << std::endl;
std::cout << " nsm config --global user.email \"you@example.com\"" << std::endl;
std::cout << " nsm config --global user.name \"Your Username\"" << std::endl;
std::cout << std::endl;
std::cout << "to set your account's default identity." << std::endl;
return 0;
}
return 1;
}
bool is_git_remote_set()
{
//checks that remote git url is set
int ret_val = system("git config --get remote.origin.url > .nsm-git-remote-url.txt");
if(ret_val)
{
start_err(std::cout) << "is_git_remote_set(): system('git config --get remote.origin.url > .nsm-git-remote-url.txt') failed in " << quote(get_pwd()) << std::endl;
remove_file(Path("./", ".nsm-git-remote-url.txt"));
return 0;
}
std::ifstream ifs(".nsm-git-remote-url.txt");
std::string str="";
ifs >> str;
ifs.close();
remove_file(Path("./", ".nsm-git-remote-url.txt"));
if(str == "")
{
start_err(std::cout) << "no remote git url set" << std::endl;
return 0;
}
return 1;
}
//get present git branch
int get_pb(std::string& branch)
{
int ret_val = system("git symbolic-ref HEAD --short > .nsm-present-branch.txt");
if(ret_val)
{
start_err(std::cout) << "get_pb(): system('git symbolic-ref HEAD --short > .nsm-present-branch.txt') failed in " << quote(get_pwd()) << std::endl;
remove_file(Path("./", ".nsm-present-branch.txt"));
return ret_val;
}
std::ifstream ifs(".nsm-present-branch.txt");
ifs >> branch;
ifs.close();
remove_file(Path("./", ".nsm-present-branch.txt"));
return 0;
}
//get present git remote
int get_remote(std::string& remote)
{
int ret_val = system("git remote > .nsm-git-remote.txt");
if(ret_val)
{
start_err(std::cout) << "get_remote(): system('git remote > .nsm-git-remote.txt') failed in " << quote(get_pwd()) << std::endl;
remove_file(Path("./", ".nsm-git-remote.txt"));
return ret_val;
}
std::ifstream ifs(".nsm-git-remote.txt");
ifs >> remote;
ifs.close();
remove_file(Path("./", ".nsm-git-remote.txt"));
return 0;
}
//get set of git branches
int get_git_branches(std::set<std::string>& branches)
{
branches.clear();
std::string branch;
int ret_val = system("git show-ref > .nsm-git-branches.txt");
if(ret_val)
{
start_err(std::cout) << "system(\"git show-ref > .nsm-git-branches.txt\") failed" << std::endl;
return 1;
}
std::ifstream ifs(".nsm-git-branches.txt");
while(ifs >> branch)
{
ifs >> branch;
int pos = branch.find_last_of('/') + 1;
branch = branch.substr(pos, branch.size() - pos);
if(branch != "*" && branch != "->" && branch != "HEAD")
branches.insert(branch);
}
ifs.close();
remove_file(Path("./", ".nsm-git-branches.txt"));
return 0;
}