Git.xs: older perl do not know const char *
[git.git] / perl / Git.xs
blobc8242103b5f571990161a8e69861c3d4c6b19d3a
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. */
7 /* libgit interface */
8 #include "../cache.h"
9 #include "../exec_cmd.h"
11 #define die perlyshadow_die__
13 /* XS and Perl interface */
14 #include "EXTERN.h"
15 #include "perl.h"
16 #include "XSUB.h"
18 #undef die
21 static char *
22 report_xs(const char *prefix, const char *err, va_list params)
24         static char buf[4096];
25         strcpy(buf, prefix);
26         vsnprintf(buf + strlen(prefix), 4096 - strlen(prefix), err, params);
27         return buf;
30 static void NORETURN
31 die_xs(const char *err, va_list params)
33         char *str;
34         str = report_xs("fatal: ", err, params);
35         croak(str);
38 static void
39 error_xs(const char *err, va_list params)
41         char *str;
42         str = report_xs("error: ", err, params);
43         warn(str);
47 MODULE = Git            PACKAGE = Git
49 PROTOTYPES: DISABLE
52 BOOT:
54         set_error_routine(error_xs);
55         set_die_routine(die_xs);
59 # /* TODO: xs_call_gate(). See Git.pm. */
62 char *
63 xs_version()
64 CODE:
66         RETVAL = GIT_VERSION;
68 OUTPUT:
69         RETVAL
72 char *
73 xs_exec_path()
74 CODE:
76         RETVAL = (char *)git_exec_path();
78 OUTPUT:
79         RETVAL
82 void
83 xs__execv_git_cmd(...)
84 CODE:
86         const char **argv;
87         int i;
89         argv = malloc(sizeof(const char *) * (items + 1));
90         if (!argv)
91                 croak("malloc failed");
92         for (i = 0; i < items; i++)
93                 argv[i] = strdup(SvPV_nolen(ST(i)));
94         argv[i] = NULL;
96         execv_git_cmd(argv);
98         for (i = 0; i < items; i++)
99                 if (argv[i])
100                         free((char *) argv[i]);
101         free((char **) argv);
104 char *
105 xs_hash_object_pipe(type, fd)
106         char *type;
107         int fd;
108 CODE:
110         unsigned char sha1[20];
112         if (index_pipe(sha1, fd, type, 0))
113                 croak("Unable to hash given filehandle");
114         RETVAL = sha1_to_hex(sha1);
116 OUTPUT:
117         RETVAL
119 char *
120 xs_hash_object_file(type, path)
121         char *type;
122         char *path;
123 CODE:
125         unsigned char sha1[20];
126         int fd = open(path, O_RDONLY);
127         struct stat st;
129         if (fd < 0 ||
130             fstat(fd, &st) < 0 ||
131             index_fd(sha1, fd, &st, 0, type))
132                 croak("Unable to hash %s", path);
133         close(fd);
135         RETVAL = sha1_to_hex(sha1);
137 OUTPUT:
138         RETVAL