so://phie

soso

My pretty neat website
git clone git://soophie.de/soso

blob: 5d9b854bb1d4972cec392c9b12844d5e8adf85a4 (2728 bytes)[raw]

1#include <git2.h>

2#include "git_repo.h"

3#include "git_repo_log.h"

4

5#include "../templ/duc_git_repo_log.h"

6

7static orb_rc_t fetch(orb_module_t *self) {

8 duc_git_repo_log_t *data = self->data;

9 git_repo_log_param_list_t *param_list = self->param_list;

10 if (!param_list) return ORB_ERR(1);

11 if (!param_list->repo) return ORB_ERR(2);

12 data->repo = param_list->repo;

13 git_revwalk *walker = NULL;

14 git_revwalk_new(&walker, param_list->repo->handle);

15 git_revwalk_sorting(walker, GIT_SORT_TIME);

16 int code = 0;

17 if (param_list->repo->ref) {

18 git_oid ref_oid;

19 git_oid_fromstr(&ref_oid, param_list->repo->ref->oid);

20 code = git_revwalk_push(walker, &ref_oid);

21 }

22 else {

23 code = git_revwalk_push_head(walker);

24 }

25 if (code != 0) {

26 git_revwalk_free(walker);

27 return ORB_ERR(code);

28 }

29 git_oid oid;

30 while (git_revwalk_next(&oid, walker) == 0) {

31 git_commit *commit_handle = NULL;

32 git_commit_lookup(&commit_handle, param_list->repo->handle, &oid);

33 data->commit_list = realloc(data->commit_list, sizeof(git_repo_commit_t) * (data->commit_list_len + 1));

34 git_repo_commit_t *commit = &data->commit_list[data->commit_list_len];

35 memset(commit, 0, sizeof(git_repo_commit_t));

36 commit->oid = malloc(sizeof(char) * (GIT_OID_HEXSZ + 1));

37 git_oid_tostr(commit->oid, GIT_OID_HEXSZ + 1, &oid);

38 commit->message = strdup(git_commit_message(commit_handle));

39 commit->summary = strdup(git_commit_summary(commit_handle));

40 const char *body = git_commit_body(commit_handle);

41 if (body) commit->body = strdup(body);

42 commit->author_name = strdup(git_commit_author(commit_handle)->name);

43 commit->author_email = strdup(git_commit_author(commit_handle)->email);

44 commit->time = (time_t) git_commit_time(commit_handle);

45 git_commit_free(commit_handle);

46 data->commit_list_len++;

47 }

48 git_revwalk_free(walker);

49 return ORB_OK;

50}

51

52static orb_rc_t render(orb_module_t *self, void *out) {

53 return ORB_ERR(duc_git_repo_log_render(out, (duc_git_repo_log_t *) self->data));

54}

55

56static void cleanup(orb_module_t *self) {

57 duc_git_repo_log_t *data = self->data;

58 if (data->commit_list && data->commit_list_len > 0) {

59 for (size_t i = 0; i < data->commit_list_len; i++) {

60 git_repo_commit_t *commit = &data->commit_list[i];

61 free(commit->oid);

62 free(commit->message);

63 free(commit->summary);

64 if (commit->body) free(commit->body);

65 free(commit->author_name);

66 free(commit->author_email);

67 }

68 free(data->commit_list);

69 data->commit_list = NULL;

70 data->commit_list_len = 0;

71 }

72}

73

74const orb_module_type_t GIT_REPO_LOG_TYPE = {

75 .size = sizeof(duc_git_repo_log_t),

76 .fetch = fetch,

77 .render = render,

78 .cleanup = cleanup,

79};