Git.pm: Don't #define around die
[git/gitweb.git] / perl / Git.xs
blob2bbec4365f9c244a7a905df702393bca591d9d67
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 /* XS and Perl interface */
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
17 static char *
18 report_xs(const char *prefix, const char *err, va_list params)
20         static char buf[4096];
21         strcpy(buf, prefix);
22         vsnprintf(buf + strlen(prefix), 4096 - strlen(prefix), err, params);
23         return buf;
26 static void NORETURN
27 die_xs(const char *err, va_list params)
29         char *str;
30         str = report_xs("fatal: ", err, params);
31         croak(str);
34 static void
35 error_xs(const char *err, va_list params)
37         char *str;
38         str = report_xs("error: ", err, params);
39         warn(str);
43 MODULE = Git            PACKAGE = Git
45 PROTOTYPES: DISABLE
48 BOOT:
50         set_error_routine(error_xs);
51         set_die_routine(die_xs);
55 # /* TODO: xs_call_gate(). See Git.pm. */
58 char *
59 xs_version()
60 CODE:
62         RETVAL = GIT_VERSION;
64 OUTPUT:
65         RETVAL
68 char *
69 xs_exec_path()
70 CODE:
72         RETVAL = (char *)git_exec_path();
74 OUTPUT:
75         RETVAL
78 void
79 xs__execv_git_cmd(...)
80 CODE:
82         const char **argv;
83         int i;
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;
92         execv_git_cmd(argv);
94         for (i = 0; i < items; i++)
95                 if (argv[i])
96                         free((char *) argv[i]);
97         free((char **) argv);
100 char *
101 xs_hash_object_pipe(type, fd)
102         char *type;
103         int fd;
104 CODE:
106         unsigned char sha1[20];
108         if (index_pipe(sha1, fd, type, 0))
109                 croak("Unable to hash given filehandle");
110         RETVAL = sha1_to_hex(sha1);
112 OUTPUT:
113         RETVAL
115 char *
116 xs_hash_object_file(type, path)
117         char *type;
118         char *path;
119 CODE:
121         unsigned char sha1[20];
122         int fd = open(path, O_RDONLY);
123         struct stat st;
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);
131         RETVAL = sha1_to_hex(sha1);
133 OUTPUT:
134         RETVAL