Git.pm: Add config() method
[git] / perl / Git.xs
1 /* By carefully stacking #includes here (even if WE don't really need them)
2  * we strive to make the thing actually compile. Git header files aren't very
3  * nice. Perl headers are one of the signs of the coming apocalypse. */
4 #include <ctype.h>
5 /* Ok, it hasn't been so bad so far. */
6
7 /* libgit interface */
8 #include "../cache.h"
9 #include "../exec_cmd.h"
10
11 /* XS and Perl interface */
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
15
16
17 static char *
18 report_xs(const char *prefix, const char *err, va_list params)
19 {
20         static char buf[4096];
21         strcpy(buf, prefix);
22         vsnprintf(buf + strlen(prefix), 4096 - strlen(prefix), err, params);
23         return buf;
24 }
25
26 static void NORETURN
27 die_xs(const char *err, va_list params)
28 {
29         char *str;
30         str = report_xs("fatal: ", err, params);
31         croak(str);
32 }
33
34 static void
35 error_xs(const char *err, va_list params)
36 {
37         char *str;
38         str = report_xs("error: ", err, params);
39         warn(str);
40 }
41
42
43 MODULE = Git            PACKAGE = Git
44
45 PROTOTYPES: DISABLE
46
47
48 BOOT:
49 {
50         set_error_routine(error_xs);
51         set_die_routine(die_xs);
52 }
53
54
55 # /* TODO: xs_call_gate(). See Git.pm. */
56
57
58 char *
59 xs_version()
60 CODE:
61 {
62         RETVAL = GIT_VERSION;
63 }
64 OUTPUT:
65         RETVAL
66
67
68 char *
69 xs_exec_path()
70 CODE:
71 {
72         RETVAL = (char *)git_exec_path();
73 }
74 OUTPUT:
75         RETVAL
76
77
78 void
79 xs__execv_git_cmd(...)
80 CODE:
81 {
82         const char **argv;
83         int i;
84
85         argv = malloc(sizeof(const char *) * (items + 1));
86         if (!argv)
87                 croak("malloc failed");
88         for (i = 0; i < items; i++)
89                 argv[i] = strdup(SvPV_nolen(ST(i)));
90         argv[i] = NULL;
91
92         execv_git_cmd(argv);
93
94         for (i = 0; i < items; i++)
95                 if (argv[i])
96                         free((char *) argv[i]);
97         free((char **) argv);
98 }
99
100 char *
101 xs_hash_object_pipe(type, fd)
102         char *type;
103         int fd;
104 CODE:
105 {
106         unsigned char sha1[20];
107
108         if (index_pipe(sha1, fd, type, 0))
109                 croak("Unable to hash given filehandle");
110         RETVAL = sha1_to_hex(sha1);
111 }
112 OUTPUT:
113         RETVAL
114
115 char *
116 xs_hash_object_file(type, path)
117         char *type;
118         char *path;
119 CODE:
120 {
121         unsigned char sha1[20];
122         int fd = open(path, O_RDONLY);
123         struct stat st;
124
125         if (fd < 0 ||
126             fstat(fd, &st) < 0 ||
127             index_fd(sha1, fd, &st, 0, type))
128                 croak("Unable to hash %s", path);
129         close(fd);
130
131         RETVAL = sha1_to_hex(sha1);
132 }
133 OUTPUT:
134         RETVAL