Make geanyvc compatible with document pointer changes in Geany
[geanyvc.git] / vc_hg.c
blob5b14f854deecc18a0639a8552c2cd7ad4484f886
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 const gchar *HG_CMD_DIFF_FILE[] = { "hg", "diff", FILENAME, NULL };
39 static const gchar *HG_CMD_DIFF_DIR[] = { "hg", "diff", DIRNAME, NULL };
40 static const gchar *HG_CMD_REVERT_FILE[] = { "hg", "revert", BASE_FILENAME, NULL };
41 static const gchar *HG_CMD_STATUS[] = { "hg", "status", NULL };
42 static const gchar *HG_CMD_ADD[] = { "hg", "add", BASE_FILENAME, NULL };
43 static const gchar *HG_CMD_REMOVE[] = { "hg", "remove", BASE_FILENAME, NULL };
44 static const gchar *HG_CMD_LOG_FILE[] = { "hg", "log", BASE_FILENAME, NULL };
45 static const gchar *HG_CMD_LOG_DIR[] = { "hg", "log", DIRNAME, NULL };
46 static const gchar *HG_CMD_COMMIT[] = { "hg", "commit", "-m", MESSAGE, FILE_LIST, NULL };
47 static const gchar *HG_CMD_BLAME[] = { "hg", "annotate", BASE_FILENAME, NULL };
48 static const gchar *HG_CMD_SHOW[] = { "hg", "cat", BASE_FILENAME, NULL };
50 static void *HG_COMMANDS[] = { HG_CMD_DIFF_FILE,
51 HG_CMD_DIFF_DIR,
52 HG_CMD_REVERT_FILE,
53 HG_CMD_STATUS,
54 HG_CMD_ADD,
55 HG_CMD_REMOVE,
56 HG_CMD_LOG_FILE,
57 HG_CMD_LOG_DIR,
58 HG_CMD_COMMIT,
59 HG_CMD_BLAME,
60 HG_CMD_SHOW
63 static gboolean
64 in_vc_hg(const gchar * filename)
66 gint exit_code;
67 gchar *argv[] = { "hg", "status", "-mac", NULL, NULL };
68 gchar *dir;
69 gchar *base_name;
70 gboolean ret = FALSE;
71 gchar *std_output;
73 if (!find_dir(filename, ".hg", TRUE))
74 return FALSE;
76 if (g_file_test(filename, G_FILE_TEST_IS_DIR))
77 return TRUE;
79 dir = g_path_get_dirname(filename);
80 base_name = g_path_get_basename(filename);
81 argv[3] = base_name;
83 exit_code = execute_custom_command((const gchar **) argv, NULL, &std_output, NULL,
84 dir, NULL, NULL);
85 if (NZV(std_output))
87 ret = TRUE;
88 g_free(std_output);
91 g_free(base_name);
92 g_free(dir);
94 return ret;
97 static GSList *
98 get_commit_files_hg(const gchar * dir)
100 enum
102 FIRST_CHAR,
103 SKIP_SPACE,
104 FILE_NAME,
107 gchar *txt;
108 GSList *ret = NULL;
109 gint pstatus = FIRST_CHAR;
110 const gchar *p;
111 gchar *base_name;
112 gchar *base_dir = find_subdir_path(dir, ".hg");
113 const gchar *start = NULL;
114 CommitItem *item;
116 const gchar *status;
117 gchar *filename;
118 const char *argv[] = { "hg", "status", NULL };
120 g_return_val_if_fail(base_dir, NULL);
122 execute_custom_command(argv, NULL, &txt, NULL, base_dir, NULL, NULL);
123 if (!NZV(txt))
125 g_free(base_dir);
126 g_free(txt);
127 return NULL;
129 p = txt;
131 while (*p)
133 if (*p == '\r')
136 else if (pstatus == FIRST_CHAR)
138 if (*p == 'A')
139 status = FILE_STATUS_ADDED;
140 else if (*p == 'R')
141 status = FILE_STATUS_DELETED;
142 else if (*p == 'M')
143 status = FILE_STATUS_MODIFIED;
144 else if (*p == '?')
145 status = FILE_STATUS_UNKNOWN;
146 pstatus = SKIP_SPACE;
148 else if (pstatus == SKIP_SPACE)
150 if (*p == ' ' || *p == '\t')
153 else
155 start = p;
156 pstatus = FILE_NAME;
159 else if (pstatus == FILE_NAME)
161 if (*p == '\n')
163 if (status != FILE_STATUS_UNKNOWN)
165 base_name = g_malloc0(p - start + 1);
166 memcpy(base_name, start, p - start);
167 filename = g_build_filename(base_dir, base_name, NULL);
168 g_free(base_name);
169 item = g_new(CommitItem, 1);
170 item->status = status;
171 item->path = filename;
172 ret = g_slist_append(ret, item);
174 pstatus = FIRST_CHAR;
177 p++;
179 g_free(txt);
180 g_free(base_dir);
181 return ret;
184 VC_RECORD VC_HG = { HG_COMMANDS, NO_ENV, "hg", in_vc_hg, get_commit_files_hg };