Git.pm: Implement Git::exec_path()
[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 #include "ppport.h"
17
18
19 MODULE = Git            PACKAGE = Git
20
21 PROTOTYPES: DISABLE
22
23 # /* TODO: xs_call_gate(). See Git.pm. */
24
25
26 const char *
27 xs_exec_path()
28 CODE:
29 {
30         RETVAL = git_exec_path();
31 }
32 OUTPUT:
33         RETVAL
34
35
36 char *
37 xs_hash_object(file, type = "blob")
38         SV *file;
39         char *type;
40 CODE:
41 {
42         unsigned char sha1[20];
43
44         if (SvTYPE(file) == SVt_RV)
45                 file = SvRV(file);
46
47         if (SvTYPE(file) == SVt_PVGV) {
48                 /* Filehandle */
49                 PerlIO *pio;
50
51                 pio = IoIFP(sv_2io(file));
52                 if (!pio)
53                         croak("You passed me something weird - a dir glob?");
54                 /* XXX: I just hope PerlIO didn't read anything from it yet.
55                  * --pasky */
56                 if (index_pipe(sha1, PerlIO_fileno(pio), type, 0))
57                         croak("Unable to hash given filehandle");
58                 /* Avoid any nasty surprises. */
59                 PerlIO_close(pio);
60
61         } else {
62                 /* String */
63                 char *path = SvPV_nolen(file);
64                 int fd = open(path, O_RDONLY);
65                 struct stat st;
66
67                 if (fd < 0 ||
68                     fstat(fd, &st) < 0 ||
69                     index_fd(sha1, fd, &st, 0, type))
70                         croak("Unable to hash %s", path);
71                 close(fd);
72         }
73         RETVAL = sha1_to_hex(sha1);
74 }
75 OUTPUT:
76         RETVAL