Make geanyvc compatible with document pointer changes in Geany
[geanyvc.git] / vc_git.c
blob6b5bd4f951cc328880ceaefe089a2825d9f1d5b6
1 /*
2 * Copyright 2007 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
3 * Copyright 2007 Enrico Tröger <enrico.troeger@uvena.de>
4 * Copyright 2007 Nick Treleaven <nick.treleaven@btinternet.com>
5 * Copyright 2007 Yura Siamashka <yurand2@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <string.h>
24 #include "geany.h"
25 #include "support.h"
26 #include "plugindata.h"
27 #include "document.h"
28 #include "filetypes.h"
29 #include "utils.h"
30 #include "project.h"
31 #include "pluginmacros.h"
33 #include "geanyvc.h"
35 extern GeanyData *geany_data;
38 static gint
39 git_commit(G_GNUC_UNUSED gchar ** std_out, G_GNUC_UNUSED gchar ** std_err, const gchar * filename,
40 GSList * list, const gchar * message)
42 gchar *base_dir = find_subdir_path(filename, ".git");
43 gint len = strlen(base_dir);
44 GSList *commit = NULL;
45 GSList *tmp = NULL;
46 const gchar *argv[] = { "git", "commit", "-m", MESSAGE, "--", FILE_LIST, NULL };
47 gint ret;
49 g_return_val_if_fail(base_dir, -1);
51 for (tmp = list; tmp != NULL; tmp = g_slist_next(tmp))
53 commit = g_slist_prepend(commit, (gchar *) tmp->data + len + 1);
56 ret = execute_custom_command(argv, NULL, NULL, NULL, base_dir, commit, message);
57 g_slist_free(commit);
58 g_free(base_dir);
59 return ret;
62 static const gchar *GIT_ENV_SHOW[] = { "PAGER=cat", NULL };
63 static gint
64 git_show(gchar ** std_out, gchar ** std_err, const gchar * filename,
65 GSList * list, const gchar * message)
67 gchar *base_dir = find_subdir_path(filename, ".git");
68 gint len = strlen(base_dir);
69 const gchar *argv[] = { "git", "show", NULL, NULL };
70 gint ret;
72 g_return_val_if_fail(base_dir, -1);
74 argv[2] = g_strdup_printf("HEAD:%s", filename + len + 1);
76 ret = execute_custom_command(argv, GIT_ENV_SHOW, std_out, std_err, base_dir, list, message);
77 g_free(base_dir);
78 g_free((gchar *) argv[2]);
79 return ret;
84 static const gchar *GIT_CMD_DIFF_FILE[] = { "git", "diff", "HEAD", "--", BASE_FILENAME, NULL };
85 static const gchar *GIT_CMD_DIFF_DIR[] = { "git", "diff", "HEAD", NULL };
86 static const gchar *GIT_CMD_REVERT_FILE[] = { "git", "checkout", "--", BASE_FILENAME, NULL };
87 static const gchar *GIT_CMD_STATUS[] = { "git", "status", NULL };
88 static const gchar *GIT_CMD_ADD[] = { "git", "add", "--", BASE_FILENAME, NULL };
89 static const gchar *GIT_CMD_REMOVE[] =
90 { "git", "rm", "-f", "--", BASE_FILENAME, CMD_SEPARATOR, "git", "reset", "HEAD", "--",
91 BASE_FILENAME, NULL
93 static const gchar *GIT_CMD_LOG_FILE[] = { "git", "log", "--", BASE_FILENAME, NULL };
94 static const gchar *GIT_CMD_LOG_DIR[] = { "git", "log", NULL };
95 static const void *GIT_CMD_COMMIT[] = { CMD_FUNCTION, git_commit };
96 static const gchar *GIT_CMD_BLAME[] = { "git", "blame", "--", BASE_FILENAME, NULL };
97 static const void *GIT_CMD_SHOW[] = { CMD_FUNCTION, git_show };
99 static const gchar *GIT_ENV_DIFF_FILE[] = { "PAGER=cat", NULL };
100 static const gchar *GIT_ENV_DIFF_DIR[] = { "PAGER=cat", NULL };
101 static const gchar *GIT_ENV_REVERT_FILE[] = { "PAGER=cat", NULL };
102 static const gchar *GIT_ENV_STATUS[] = { "PAGER=cat", NULL };
103 static const gchar *GIT_ENV_ADD[] = { "PAGER=cat", NULL };
104 static const gchar *GIT_ENV_REMOVE[] = { "PAGER=cat", NULL };
105 static const gchar *GIT_ENV_LOG_FILE[] = { "PAGER=cat", NULL };
106 static const gchar *GIT_ENV_LOG_DIR[] = { "PAGER=cat", NULL };
107 static const gchar *GIT_ENV_BLAME[] = { "PAGER=cat", NULL };
109 #define GIT_ENV_COMMIT NULL
111 static void *GIT_COMMANDS[VC_COMMAND_COUNT] = { GIT_CMD_DIFF_FILE,
112 GIT_CMD_DIFF_DIR,
113 GIT_CMD_REVERT_FILE,
114 GIT_CMD_STATUS,
115 GIT_CMD_ADD,
116 GIT_CMD_REMOVE,
117 GIT_CMD_LOG_FILE,
118 GIT_CMD_LOG_DIR,
119 GIT_CMD_COMMIT,
120 GIT_CMD_BLAME,
121 GIT_CMD_SHOW
124 static void *GIT_ENV[] = { GIT_ENV_DIFF_FILE,
125 GIT_ENV_DIFF_DIR,
126 GIT_ENV_REVERT_FILE,
127 GIT_ENV_STATUS,
128 GIT_ENV_ADD,
129 GIT_ENV_REMOVE,
130 GIT_ENV_LOG_FILE,
131 GIT_ENV_LOG_DIR,
132 GIT_ENV_COMMIT,
133 GIT_ENV_BLAME,
134 GIT_ENV_SHOW
137 static gboolean
138 in_vc_git(const gchar * filename)
140 gint exit_code;
141 gchar *argv[] = { "git", "ls-files", "--", NULL, NULL };
142 gchar *dir;
143 gchar *base_name;
144 gboolean ret = FALSE;
145 gchar *std_output;
147 if (!find_dir(filename, ".git", TRUE))
148 return FALSE;
150 if (g_file_test(filename, G_FILE_TEST_IS_DIR))
151 return TRUE;
153 dir = g_path_get_dirname(filename);
154 base_name = g_path_get_basename(filename);
155 argv[3] = base_name;
157 exit_code = execute_custom_command((const gchar **) argv, NULL, &std_output, NULL,
158 dir, NULL, NULL);
159 if (NZV(std_output))
161 ret = TRUE;
162 g_free(std_output);
165 g_free(base_name);
166 g_free(dir);
168 return ret;
171 static GSList *
172 parse_git_status(GSList * lst, const gchar * base_dir, const gchar * txt, const gchar * s_out,
173 const gchar * status)
175 gchar *start, *end;
176 gchar *base_name;
177 gchar *filename;
178 CommitItem *item;
180 start = strstr(txt, s_out);
181 while (start)
183 start += strlen(s_out);
184 while (*start == ' ' || *start == '\t')
186 start++;
188 g_return_val_if_fail(*start, NULL);
190 end = strchr(start, '\n');
191 g_return_val_if_fail(start, NULL);
193 base_name = g_malloc0(end - start + 1);
194 memcpy(base_name, start, end - start);
195 filename = g_build_filename(base_dir, base_name, NULL);
196 g_free(base_name);
198 item = g_new(CommitItem, 1);
199 item->status = status;
200 item->path = filename;
202 lst = g_slist_append(lst, item);
203 start = strstr(start, s_out);
205 return lst;
208 static GSList *
209 get_commit_files_git(const gchar * file)
211 gint exit_code;
212 gchar *argv[] = { "git", "status", NULL };
213 gchar *env[] = { "PAGES=cat", NULL };
214 gchar *std_out = NULL;
215 gchar *base_dir = find_subdir_path(file, ".git");
216 GSList *ret = NULL;
218 g_return_val_if_fail(base_dir, NULL);
220 exit_code =
221 execute_custom_command((const gchar **) argv, (const gchar **) env, &std_out, NULL,
222 base_dir, NULL, NULL);
223 g_return_val_if_fail(std_out, NULL);
225 ret = parse_git_status(ret, base_dir, std_out, "modified:", FILE_STATUS_MODIFIED);
226 ret = parse_git_status(ret, base_dir, std_out, "deleted:", FILE_STATUS_DELETED);
227 ret = parse_git_status(ret, base_dir, std_out, "new file:", FILE_STATUS_ADDED);
229 g_free(std_out);
230 g_free(base_dir);
232 return ret;
235 VC_RECORD VC_GIT = { GIT_COMMANDS, GIT_ENV, "git", in_vc_git, get_commit_files_git };