-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTrackGit.cpp
228 lines (193 loc) · 6.02 KB
/
TrackGit.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
/*
* Copyright 2018, Hrishikesh Hiraskar <hrishihiraskar@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include <stdio.h>
#include <strings.h>
#include <vector>
#include <Alert.h>
#include <Application.h>
#include <AppKit.h>
#include <Catalog.h>
#include <InterfaceKit.h>
#include <StorageKit.h>
#include <SupportKit.h>
#include <MenuItem.h>
#include <add-ons/tracker/TrackerAddOn.h>
#include <git2.h>
#include "TrackGitApp.h"
#include "Utils.h"
#define B_TRANSLATION_CONTEXT "TrackGit"
extern "C" {
void populate_menu (BMessage* msg, BMenu* menu, BHandler* handler);
void message_received (BMessage* msg);
}
/**
* process_ref definition for addon.
* @param dir_ref The current directory ref.
* @param msg BMessage containing refs to selected files.
*/
void
process_refs(entry_ref dir_ref, BMessage* msg, void*)
{
TrackGitApp::AboutWindow();
}
/**
* Populates menu for this addon.
* @param msg The passed BMessage. Contains refs to current dir and selected
* files.
* @param menu The pointer to Tracker menu.
* @param handler The BHandler of Tracker. This should be target of added items.
*/
void
populate_menu(BMessage* msg, BMenu* menu, BHandler* handler)
{
if (menu == NULL){
BString buffer("Null menu");
BAlert *alert = new BAlert("", buffer.String(), "Cancel",
0, 0, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
alert->Go();
return;
}
// Remove Menu item if already exists.
BMenuItem* item = menu->FindItem(ADDON_NAME);
if (item != NULL)
menu->RemoveItem(item);
BMenu* submenu = new BMenu(ADDON_NAME);
vector<char*> selected;
extract_selected_paths(msg, selected);
BString dirPath = extract_current_directory(msg);
git_libgit2_init();
git_buf buf = GIT_BUF_INIT_CONST(NULL, 0);
// Check if current directory is in git repo.
if (git_repository_discover(&buf, dirPath.String(), 0, NULL) == 0) {
// buf is git repo
git_repository* repo = NULL;
git_index* index;
// Init repo
git_repository_open_ext(&repo, dirPath.String(), 0, NULL);
// Init index
git_repository_index(&index, repo);
if (git_index_has_conflicts(index)) {
// Show conflicts menu item
BMessage* showConflictsMsg = new BMessage(*msg);
showConflictsMsg->AddInt32("addon_item_id", kShowConflicts);
BMenuItem* showConflictsItem = new BMenuItem(
B_TRANSLATE("Show conflicts" B_UTF8_ELLIPSIS),
showConflictsMsg);
submenu->AddItem(showConflictsItem);
}
// Add Status menu item
BMessage* statusMsg = new BMessage(*msg);
statusMsg->AddInt32("addon_item_id", kStatus);
BMenuItem* statusItem = new BMenuItem(
B_TRANSLATE("Status" B_UTF8_ELLIPSIS), statusMsg);
submenu->AddItem(statusItem);
// Add Commit menu item
BMessage* commitMsg = new BMessage(*msg);
commitMsg->AddInt32("addon_item_id", kCommit);
BMenuItem* commitItem = new BMenuItem(
B_TRANSLATE("Commit" B_UTF8_ELLIPSIS), commitMsg);
submenu->AddItem(commitItem);
// Add Pull menu item
BMessage* pullMsg = new BMessage(*msg);
pullMsg->AddInt32("addon_item_id", kPull);
BMenuItem* pullItem = new BMenuItem(
B_TRANSLATE("Pull" B_UTF8_ELLIPSIS), pullMsg);
submenu->AddItem(pullItem);
// Add Push menu item
BMessage* pushMsg = new BMessage(*msg);
pushMsg->AddInt32("addon_item_id", kPush);
BMenuItem* pushItem = new BMenuItem(
B_TRANSLATE("Push" B_UTF8_ELLIPSIS), pushMsg);
submenu->AddItem(pushItem);
// Add Create Branch menu item
BMessage* createBranchMsg = new BMessage(*msg);
createBranchMsg->AddInt32("addon_item_id", kCreateBranch);
BMenuItem* createBranchItem = new BMenuItem(
B_TRANSLATE("Create branch" B_UTF8_ELLIPSIS), createBranchMsg);
submenu->AddItem(createBranchItem);
// Add Switch Branch menu item
BMessage* switchBranchMsg = new BMessage(*msg);
switchBranchMsg->AddInt32("addon_item_id", kSwitchBranch);
BMenuItem* switchBranchItem = new BMenuItem(
B_TRANSLATE("Switch branch" B_UTF8_ELLIPSIS), switchBranchMsg);
submenu->AddItem(switchBranchItem);
// Add Log menu item
BMessage* logMsg = new BMessage(*msg);
logMsg->AddInt32("addon_item_id", kLog);
BMenuItem* logItem = new BMenuItem(
B_TRANSLATE("Log" B_UTF8_ELLIPSIS), logMsg);
submenu->AddItem(logItem);
if (selected.size() > 0) {
// Add "Add Files" menu item
BMessage* addMsg = new BMessage(*msg);
addMsg->AddInt32("addon_item_id", kAdd);
BMenuItem* addItem = new BMenuItem(
B_TRANSLATE("Add files"), addMsg);
submenu->AddItem(addItem);
} else {
// Add "Add All Files" menu item
BMessage* addMsg = new BMessage(*msg);
addMsg->AddInt32("addon_item_id", kAddAll);
BMenuItem* addItem = new BMenuItem(
B_TRANSLATE("Add all files"), addMsg);
submenu->AddItem(addItem);
}
} else if (strcmp(menu->Name(), "MenuBar") == 0) {
// For git-less dirPath, don't add items to menubar
delete submenu;
submenu = NULL;
} else {
// dirPath does not belong to git repo, menu isn't menubar
// Add Clone menu item
BMessage* cloneMsg = new BMessage(*msg);
cloneMsg->AddInt32("addon_item_id", kClone);
BMenuItem* cloneItem = new BMenuItem(
B_TRANSLATE("Clone" B_UTF8_ELLIPSIS), cloneMsg);
submenu->AddItem(cloneItem);
// Add "Init Here" only if no files are selected.
if (selected.size() == 0) {
// Add Init here
BMessage* initMsg = new BMessage(*msg);
initMsg->AddInt32("addon_item_id", kInitHere);
BMenuItem* initItem = new BMenuItem(B_TRANSLATE("Init Here"),
initMsg);
submenu->AddItem(initItem);
}
}
if (submenu != NULL) {
menu->AddItem(submenu);
submenu->SetTargetForItems(handler);
}
git_buf_free(&buf);
git_libgit2_shutdown();
}
/**
* Handler for received messages.
* @param msg The passed BMessage.
*/
void
message_received(BMessage* msg)
{
int32 itemId;
if (msg->FindInt32("addon_item_id", &itemId) != B_OK)
return;
BMessenger messenger(APP_SIGN);
if (!messenger.IsValid()) {
be_roster->Launch(APP_SIGN);
messenger = BMessenger(APP_SIGN);
}
messenger.SendMessage(msg);
}
/**
* The main function of TrackGit.
* This function launches the TrackGit App.
*/
int
main()
{
TrackGitApp app;
app.Run();
return 0;
}