Git.pm: Implement Git::version()
[git/debian.git] / perl / Git.xs
blobd4608eb1cb198f0c5426d90270e202d441c7da82
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"
16 #include "ppport.h"
19 MODULE = Git            PACKAGE = Git
21 PROTOTYPES: DISABLE
23 # /* TODO: xs_call_gate(). See Git.pm. */
26 const char *
27 xs_version()
28 CODE:
30         RETVAL = GIT_VERSION;
32 OUTPUT:
33         RETVAL
36 const char *
37 xs_exec_path()
38 CODE:
40         RETVAL = git_exec_path();
42 OUTPUT:
43         RETVAL
46 void
47 xs__execv_git_cmd(...)
48 CODE:
50         const char **argv;
51         int i;
53         argv = malloc(sizeof(const char *) * (items + 1));
54         if (!argv)
55                 croak("malloc failed");
56         for (i = 0; i < items; i++)
57                 argv[i] = strdup(SvPV_nolen(ST(i)));
58         argv[i] = NULL;
60         execv_git_cmd(argv);
62         for (i = 0; i < items; i++)
63                 if (argv[i])
64                         free((char *) argv[i]);
65         free((char **) argv);
68 char *
69 xs_hash_object(file, type = "blob")
70         SV *file;
71         char *type;
72 CODE:
74         unsigned char sha1[20];
76         if (SvTYPE(file) == SVt_RV)
77                 file = SvRV(file);
79         if (SvTYPE(file) == SVt_PVGV) {
80                 /* Filehandle */
81                 PerlIO *pio;
83                 pio = IoIFP(sv_2io(file));
84                 if (!pio)
85                         croak("You passed me something weird - a dir glob?");
86                 /* XXX: I just hope PerlIO didn't read anything from it yet.
87                  * --pasky */
88                 if (index_pipe(sha1, PerlIO_fileno(pio), type, 0))
89                         croak("Unable to hash given filehandle");
90                 /* Avoid any nasty surprises. */
91                 PerlIO_close(pio);
93         } else {
94                 /* String */
95                 char *path = SvPV_nolen(file);
96                 int fd = open(path, O_RDONLY);
97                 struct stat st;
99                 if (fd < 0 ||
100                     fstat(fd, &st) < 0 ||
101                     index_fd(sha1, fd, &st, 0, type))
102                         croak("Unable to hash %s", path);
103                 close(fd);
104         }
105         RETVAL = sha1_to_hex(sha1);
107 OUTPUT:
108         RETVAL