Git.pm: Introduce fast get_object() method
[git/repo.git] / perl / Git.xs
blob226dd4f681ae99bb5c53a4e63394161d92c6ff76
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 void
56 xs__call_gate(repoid, git_dir)
57         long repoid;
58         char *git_dir;
59 CODE:
61         static long last_repoid;
62         if (repoid != last_repoid) {
63                 setup_git(git_dir,
64                           getenv(DB_ENVIRONMENT),
65                           getenv(INDEX_ENVIRONMENT),
66                           getenv(GRAFT_ENVIRONMENT));
67                 last_repoid = repoid;
68         }
72 char *
73 xs_version()
74 CODE:
76         RETVAL = GIT_VERSION;
78 OUTPUT:
79         RETVAL
82 char *
83 xs_exec_path()
84 CODE:
86         RETVAL = (char *)git_exec_path();
88 OUTPUT:
89         RETVAL
92 void
93 xs__execv_git_cmd(...)
94 CODE:
96         const char **argv;
97         int i;
99         argv = malloc(sizeof(const char *) * (items + 1));
100         if (!argv)
101                 croak("malloc failed");
102         for (i = 0; i < items; i++)
103                 argv[i] = strdup(SvPV_nolen(ST(i)));
104         argv[i] = NULL;
106         execv_git_cmd(argv);
108         for (i = 0; i < items; i++)
109                 if (argv[i])
110                         free((char *) argv[i]);
111         free((char **) argv);
115 SV *
116 xs_get_object(type, id)
117         char *type;
118         char *id;
119 CODE:
121         unsigned char sha1[20];
122         unsigned long size;
123         void *buf;
125         if (strlen(id) != 40 || get_sha1_hex(id, sha1) < 0)
126                 XSRETURN_UNDEF;
128         buf = read_sha1_file(sha1, type, &size);
129         if (!buf)
130                 XSRETURN_UNDEF;
131         RETVAL = newSVpvn(buf, size);
132         free(buf);
134 OUTPUT:
135         RETVAL
138 char *
139 xs_hash_object_pipe(type, fd)
140         char *type;
141         int fd;
142 CODE:
144         unsigned char sha1[20];
146         if (index_pipe(sha1, fd, type, 0))
147                 croak("Unable to hash given filehandle");
148         RETVAL = sha1_to_hex(sha1);
150 OUTPUT:
151         RETVAL
153 char *
154 xs_hash_object_file(type, path)
155         char *type;
156         char *path;
157 CODE:
159         unsigned char sha1[20];
160         int fd = open(path, O_RDONLY);
161         struct stat st;
163         if (fd < 0 ||
164             fstat(fd, &st) < 0 ||
165             index_fd(sha1, fd, &st, 0, type))
166                 croak("Unable to hash %s", path);
167         close(fd);
169         RETVAL = sha1_to_hex(sha1);
171 OUTPUT:
172         RETVAL