aboutsummaryrefslogtreecommitdiff
path: root/git.c
blob: cea097e25926e73324ea8e4ec331e473e48514e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// File Version Control with GIT
//
#ifdef WFMGIT
#include <git2.h>
#include "wfm.h"

enum { ADD, DEL };

git_repository *repo;

void wfm_git_stage(int op, char *filename) {
    git_index *index;
    
    git_repository_index(&index, repo);   
    
    if(op==ADD)
        git_index_add_bypath(index, filename);                   
    else if(op==DEL)
        git_index_remove_bypath(index, filename);                   
    else
        return;

    git_index_write(index);

    git_index_free(index);

    return;
}


void wfm_git_commit(char *msg) {
    git_oid tree_oid, head_oid, commit_oid;
    git_tree *tree;
    git_commit *head_commit;
    git_index *index;
    git_signature *sig;
    int ret;
    char username[1024]={0};
    char email[1024]={0};

    snprintf(username, sizeof(username), "%s %s", basename(cgiScriptName), (strlen(rt.loggedinuser)) ? rt.loggedinuser:"(none)");
    snprintf(email, sizeof(email), "%s@%s.wfm", (strlen(rt.loggedinuser)) ? rt.loggedinuser:"(none)", cgiRemoteAddr);

    git_signature_now(&sig, username, email);        
    

    git_repository_index(&index, repo);                     
    git_index_write_tree(&tree_oid, index);                  
    git_tree_lookup(&tree, repo, &tree_oid);                 
    ret=git_reference_name_to_id(&head_oid, repo, "HEAD");     

    if(ret) {
        git_commit_create_v(&commit_oid, repo, "HEAD", sig, sig, NULL, msg, tree, 0); 
    }
    else {
        git_commit_lookup(&head_commit, repo, &head_oid);           
        git_commit_create_v(&commit_oid, repo, "HEAD", sig, sig, NULL, msg, tree, 1, head_commit); 
    }
    
    git_index_write(index);

    git_tree_free(tree);
    git_index_free(index);
    git_signature_free(sig);
    return;
}
#endif

int wfm_commit(int op, char *fname) {
#ifdef WFMGIT
    int ret;
    char repodir[sizeof(cfg.homedir)+10]={0};
    char msg[1024];
    char stage_filename_buf[8192]={0};
    char *stage_filename=stage_filename_buf;
    char stage_newname_buf[8192]={0};
    char *stage_newname=stage_newname_buf;
    char *opstr[]={ "Change", "Delete", "Move" };

    if(op>2)
        return 0;

    git_libgit2_init();

    snprintf(repodir, sizeof(repodir), "%s/.git", cfg.homedir);
    ret=git_repository_open(&repo, repodir);                
    if(ret)
        return ret;

    if(fname && strlen(fname)) 
        strncpy(stage_filename_buf, fname, sizeof(stage_filename_buf)-1);
    else
        strncpy(stage_filename_buf, wp.phys_filename, sizeof(stage_filename_buf));
    stage_filename+=strlen(cfg.homedir);
    while(*stage_filename=='/')
        stage_filename++;

    if(op==MOVE) {
        strncpy(stage_newname_buf, wp.final_destination, sizeof(stage_newname_buf));
        stage_newname+=strlen(cfg.homedir);
        while(*stage_newname=='/')
            stage_newname++;
    }

    snprintf(msg, sizeof(msg), 
        "WFM %s Commit: "
        "Filename=[%s%s%s] "
        "Instance=[%s] "
        "User=[%s] "
        "RemoteIP=[%s]\n", 
        opstr[op], 
        stage_filename,
        (op==MOVE) ? " => " : "",
        (op==MOVE) ? stage_newname : "",
        basename(cgiScriptName),
        (strlen(rt.loggedinuser)) ? rt.loggedinuser:"(none)",
        cgiRemoteAddr
    );

    if(op==CHANGE) {
        wfm_git_stage(ADD, stage_filename);
    }
    else if(op==DELETE) {
        wfm_git_stage(DEL, stage_filename);
    }
    else if(op==MOVE) {
        wfm_git_stage(DEL, stage_filename);
        wfm_git_stage(ADD, stage_newname);
    }

    wfm_git_commit(msg);
    git_repository_free(repo);
#endif
    return 0;
}



int repo_check() {
    int ret=1;
#ifdef WFMGIT
    char repodir[sizeof(cfg.homedir)+10]={0};

	git_libgit2_init();

    snprintf(repodir, sizeof(repodir), "%s/.git", cfg.homedir);
    ret=git_repository_open(&repo, repodir);                
    if(ret==0)
        git_repository_free(repo);

#endif
    return ret;
}