generate dependencies of modules automatically
[git-cheetah/kirill.git] / cheetahmenu.c
blob115c838233ab04742a08e14af1644d83d1856424
2 #include "cache.h"
3 #include "exec.h"
4 #include "menuengine.h"
5 #include "cheetahmenu.h"
6 #include "debug.h"
8 char *wd_from_path(const char *path, BOOL *is_path_dir)
10 BOOL is_directory = TRUE;
11 char *cheetah_wd = strdup(path);
12 if (!(FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(cheetah_wd))) {
13 char *c = strrchr(cheetah_wd, '\\');
14 if (c) /* sanity check in case it's a weird directory */
15 *c = 0;
17 is_directory = FALSE;
20 if (is_path_dir)
21 *is_path_dir = is_directory;
23 return cheetah_wd;
27 * Cheetah-specific menu
30 static void menu_gui(struct git_data *this_, UINT id)
32 char *wd = wd_from_path(this_->name, NULL);
33 exec_program(wd, NULL, NULL, HIDDENMODE, "git", "gui", NULL);
34 free(wd);
37 static void menu_init(struct git_data *this_, UINT id)
39 char *wd = wd_from_path(this_->name, NULL);
40 exec_program(wd, NULL, NULL, HIDDENMODE, "git", "init", NULL);
41 free(wd);
44 static void menu_history(struct git_data *this_, unsigned int id)
46 BOOL is_directory;
47 char *wd = wd_from_path(this_->name, &is_directory);
48 char *name = "";
50 if (!is_directory)
51 name = this_->name + strlen(wd) + 1;
53 exec_program(wd, NULL, NULL, HIDDENMODE, "sh", "--login", "-i",
54 "/bin/gitk", "HEAD", "--", name, NULL);
55 free(wd);
58 static void menu_bash(struct git_data *this_, UINT id)
60 char *wd = wd_from_path(this_->name, NULL);
61 /* start is required because exec_program does not create a window */
62 exec_program(wd, NULL, NULL, NORMALMODE,
63 "start", "sh", "--login", "-i", NULL);
64 free(wd);
67 static void menu_blame(struct git_data *this_, UINT id)
69 BOOL is_directory;
70 char *wd = wd_from_path(this_->name, &is_directory);
71 char *name = "";
73 if (!is_directory) {
74 name = this_->name + strlen(wd) + 1;
75 exec_program(wd, NULL, NULL, HIDDENMODE,
76 "git", "gui", "blame", name, NULL);
79 free(wd);
82 static void menu_citool(struct git_data *this_, UINT id)
84 char *wd = wd_from_path(this_->name, NULL);
85 exec_program(wd, NULL, NULL, HIDDENMODE, "git", "citool", NULL);
86 free(wd);
89 static void menu_addall(struct git_data *this_, UINT id)
91 char *wd = wd_from_path(this_->name, NULL);
92 exec_program(wd, NULL, NULL, HIDDENMODE, "git", "add", "--all", NULL);
93 free(wd);
96 static void menu_branch(struct git_data *this_, UINT id)
98 int status;
99 char *wd = wd_from_path(this_->name, NULL);
100 struct strbuf err;
101 strbuf_init(&err, 0);
103 status = exec_program(wd, NULL, &err, HIDDENMODE,
104 "git", "checkout", get_menu_item_text(id), NULL);
106 /* if nothing, terribly wrong happened, show the confirmation */
107 if (-1 != status)
108 /* strangely enough even success message is on stderr */
109 debug_git_mbox(err.buf);
111 free(wd);
114 static BOOL build_branch_menu(struct git_data *data,
115 const struct menu_item *item,
116 void *platform)
118 void *submenu;
120 int status;
121 char *wd = wd_from_path(data->name, NULL);
123 struct strbuf output;
124 struct strbuf **lines, **it;
125 strbuf_init(&output, 0);
127 status = exec_program(wd, &output, NULL, WAITMODE,
128 "git", "branch", NULL);
129 free(wd);
130 if (status)
131 return FALSE;
133 submenu = start_submenu(data, item, platform);
135 lines = strbuf_split(&output, '\n');
136 for (it = lines; *it; it++) {
137 struct menu_item item = {
138 MENU_ITEM_CLEANUP,
139 NULL, NULL,
140 NULL, menu_branch
143 strbuf_rtrim(*it);
144 item.string = strdup((*it)->buf + 2);
145 item.helptext = strdup((*it)->buf + 2);
146 if (build_item(data, &item, submenu)) {
147 check_menu_item(submenu, '*' == (*it)->buf[0]);
148 append_active_menu(item);
149 } else
151 * if the platform failed to create an item
152 * there is no point to try other items
154 break;
157 end_submenu(platform, submenu);
159 /* technically, there is nothing to track for the menu engine */
160 return FALSE;
163 UINT cheetah_menu_mask(struct git_data *this_)
165 BOOL is_directory;
166 char *wd = wd_from_path(this_->name, &is_directory);
167 UINT selection = is_directory ? MENU_ITEM_DIR : MENU_ITEM_FILE;
168 int status;
170 struct strbuf output;
171 char *eol;
172 strbuf_init(&output, 0);
174 status = exec_program(wd, &output, NULL, WAITMODE,
175 "git", "rev-parse", "--show-prefix", NULL);
176 eol = strchr(output.buf, '\n');
177 if (eol)
178 *eol = 0;
180 if (status < 0) /* something went terribly wrong */
181 selection = MENU_ITEM_LAST;
182 else if (status)
183 selection |= MENU_ITEM_NOREPO;
184 else {
185 char head_path[MAX_PATH] = "HEAD";
186 if (!is_directory)
187 sprintf(head_path, "HEAD:%s%s",
188 output.buf,
189 this_->name + strlen(wd) + 1);
191 status = exec_program(wd, NULL, NULL, WAITMODE,
192 "git", "rev-parse", "--verify", head_path, NULL);
193 if (status < 0) /* something went terribly wrong */
194 selection = MENU_ITEM_LAST;
195 else
196 selection |= MENU_ITEM_REPO |
197 (status ?
198 MENU_ITEM_NOTRACK : MENU_ITEM_TRACK);
201 strbuf_release(&output);
202 free(wd);
203 return selection;
206 const struct menu_item cheetah_menu[] = {
207 { MENU_ITEM_ALWAYS, NULL, NULL, build_separator, NULL },
209 { MENU_ITEM_REPO, "Git &Add all files now",
210 "Add all files from this folder now",
211 build_item, menu_addall },
212 { MENU_ITEM_REPO, "Git &Commit Tool",
213 "Launch the GIT commit tool in the local or chosen directory.",
214 build_item, menu_citool },
215 { MENU_ITEM_TRACK, "Git &History",
216 "Show GIT history of the chosen file or directory.",
217 build_item,
218 menu_history },
219 { MENU_ITEM_TRACK | MENU_ITEM_FILE, "Git &Blame",
220 "Start a blame viewer on the specified file.",
221 build_item, menu_blame },
223 { MENU_ITEM_REPO, "Git &Gui",
224 "Launch the GIT Gui in the local or chosen directory.",
225 build_item, menu_gui },
227 { MENU_ITEM_REPO, "Git Bra&nch",
228 "Checkout a branch",
229 build_branch_menu, NULL },
231 { MENU_ITEM_NOREPO, "Git I&nit Here",
232 "Initialize GIT repo in the local directory.",
233 build_item, menu_init },
234 { MENU_ITEM_NOREPO | MENU_ITEM_DIR, "Git &Gui",
235 "Launch the GIT Gui in the local or chosen directory.",
236 build_item, menu_gui },
238 { MENU_ITEM_ALWAYS, "Git Ba&sh",
239 "Start GIT shell in the local or chosen directory",
240 build_item, menu_bash },
241 { MENU_ITEM_ALWAYS, NULL, NULL, build_separator, NULL },
244 void build_cheetah_menu(struct git_data *data, void *platform_data)
246 reset_platform(platform_data);
247 build_menu_items(data, cheetah_menu_mask,
248 cheetah_menu,
249 sizeof(cheetah_menu) / sizeof(cheetah_menu[0]),
250 platform_data);