- commit
- f53f481b3f02431b151ae9953998d7c7b3138585
- parent
- 20c37e4fe4bfef13642542f3fa15aa73c92a037c
- Author
- Hiltjo Posthuma <hiltjo@codemadness.org>
- Date
- 2018-03-18 14:34
optimization: only diff the tree when it is needed for the diffstat... ... also clear all fields in the structure on failure. This is not as big an optimization as stagit-gopher, because the diffstat is displayed in the log, but the difference is still measurable.
Diffstat
M | stagit.c | 44 | +++++++++++++++++++++++++++----------------- |
1 files changed, 27 insertions, 17 deletions
diff --git a/stagit.c b/stagit.c
@@ -87,7 +87,7 @@ deltainfo_free(struct deltainfo *di) 87 87 if (!di) 88 88 return; 89 89 git_patch_free(di->patch);90 -1 di->patch = NULL;-1 90 memset(di, 0, sizeof(*di)); 91 91 free(di); 92 92 } 93 93 @@ -95,6 +95,7 @@ int 95 95 commitinfo_getstats(struct commitinfo *ci) 96 96 { 97 97 struct deltainfo *di; -1 98 git_diff_options opts; 98 99 const git_diff_delta *delta; 99 100 const git_diff_hunk *hunk; 100 101 const git_diff_line *line; @@ -102,6 +103,20 @@ commitinfo_getstats(struct commitinfo *ci) 102 103 size_t ndeltas, nhunks, nhunklines; 103 104 size_t i, j, k; 104 105 -1 106 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit))) -1 107 goto err; -1 108 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) { -1 109 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) { -1 110 ci->parent = NULL; -1 111 ci->parent_tree = NULL; -1 112 } -1 113 } -1 114 -1 115 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION); -1 116 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH; -1 117 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts)) -1 118 goto err; -1 119 105 120 ndeltas = git_diff_num_deltas(ci->diff); 106 121 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *)))) 107 122 err(1, "calloc"); @@ -143,6 +158,15 @@ commitinfo_getstats(struct commitinfo *ci) 143 158 return 0; 144 159 145 160 err: -1 161 git_diff_free(ci->diff); -1 162 ci->diff = NULL; -1 163 git_tree_free(ci->commit_tree); -1 164 ci->commit_tree = NULL; -1 165 git_tree_free(ci->parent_tree); -1 166 ci->parent_tree = NULL; -1 167 git_commit_free(ci->parent); -1 168 ci->parent = NULL; -1 169 146 170 if (ci->deltas) 147 171 for (i = 0; i < ci->ndeltas; i++) 148 172 deltainfo_free(ci->deltas[i]); @@ -166,13 +190,14 @@ commitinfo_free(struct commitinfo *ci) 166 190 if (ci->deltas) 167 191 for (i = 0; i < ci->ndeltas; i++) 168 192 deltainfo_free(ci->deltas[i]); -1 193 169 194 free(ci->deltas);170 -1 ci->deltas = NULL;171 195 git_diff_free(ci->diff); 172 196 git_tree_free(ci->commit_tree); 173 197 git_tree_free(ci->parent_tree); 174 198 git_commit_free(ci->commit); 175 199 git_commit_free(ci->parent); -1 200 memset(ci, 0, sizeof(*ci)); 176 201 free(ci); 177 202 } 178 203 @@ -180,7 +205,6 @@ struct commitinfo * 180 205 commitinfo_getbyoid(const git_oid *id) 181 206 { 182 207 struct commitinfo *ci;183 -1 git_diff_options opts;184 208 185 209 if (!(ci = calloc(1, sizeof(struct commitinfo)))) 186 210 err(1, "calloc"); @@ -197,20 +221,6 @@ commitinfo_getbyoid(const git_oid *id) 197 221 ci->summary = git_commit_summary(ci->commit); 198 222 ci->msg = git_commit_message(ci->commit); 199 223200 -1 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))201 -1 goto err;202 -1 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {203 -1 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {204 -1 ci->parent = NULL;205 -1 ci->parent_tree = NULL;206 -1 }207 -1 }208 -1209 -1 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);210 -1 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;211 -1 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))212 -1 goto err;213 -1214 224 return ci; 215 225 216 226 err: