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.
26 #include "plugindata.h"
28 #include "filetypes.h"
31 #include "pluginmacros.h"
35 extern GeanyData
*geany_data
;
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
;
46 const gchar
*argv
[] = { "git", "commit", "-m", MESSAGE
, "--", FILE_LIST
, NULL
};
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
);
62 static const gchar
*GIT_ENV_SHOW
[] = { "PAGER=cat", NULL
};
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
};
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
);
78 g_free((gchar
*) argv
[2]);
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", "--",
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
,
124 static void *GIT_ENV
[] = { GIT_ENV_DIFF_FILE
,
138 in_vc_git(const gchar
* filename
)
141 gchar
*argv
[] = { "git", "ls-files", "--", NULL
, NULL
};
144 gboolean ret
= FALSE
;
147 if (!find_dir(filename
, ".git", TRUE
))
150 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
153 dir
= g_path_get_dirname(filename
);
154 base_name
= g_path_get_basename(filename
);
157 exit_code
= execute_custom_command((const gchar
**) argv
, NULL
, &std_output
, NULL
,
172 parse_git_status(GSList
* lst
, const gchar
* base_dir
, const gchar
* txt
, const gchar
* s_out
,
173 const gchar
* status
)
180 start
= strstr(txt
, s_out
);
183 start
+= strlen(s_out
);
184 while (*start
== ' ' || *start
== '\t')
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
);
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
);
209 get_commit_files_git(const gchar
* file
)
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");
218 g_return_val_if_fail(base_dir
, NULL
);
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
);
235 VC_RECORD VC_GIT
= { GIT_COMMANDS
, GIT_ENV
, "git", in_vc_git
, get_commit_files_git
};