| 187 | } |
| 188 | |
| 189 | static char *slug_from_branch(const char *branch, bool detached) { |
| 190 | const char *fallback = detached ? "detached" : "working-tree"; |
| 191 | const char *src = detached ? fallback : (branch && branch[0] ? branch : fallback); |
| 192 | size_t len = strlen(src); |
| 193 | char *slug = (char *)malloc(len + 1); |
| 194 | if (!slug) { |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | size_t j = 0; |
| 199 | bool in_dash = false; |
| 200 | for (size_t i = 0; i < len; i++) { |
| 201 | unsigned char c = (unsigned char)src[i]; |
| 202 | if (isalnum(c) || c == '-' || c == '_' || c == '.') { |
| 203 | if (j == 0 && c == '-') { |
| 204 | in_dash = true; |
| 205 | continue; |
| 206 | } |
| 207 | slug[j++] = (char)c; |
| 208 | in_dash = false; |
| 209 | } else if (j > 0 && !in_dash) { |
| 210 | slug[j++] = '-'; |
| 211 | in_dash = true; |
| 212 | } |
| 213 | } |
| 214 | while (j > 0 && slug[j - 1] == '-') { |
| 215 | j--; |
| 216 | } |
| 217 | slug[j] = '\0'; |
| 218 | |
| 219 | if (slug[0] == '\0') { |
| 220 | free(slug); |
| 221 | return git_strdup(fallback); |
| 222 | } |
| 223 | return slug; |
| 224 | } |
| 225 | |
| 226 | void cbm_git_context_free(cbm_git_context_t *ctx) { |
| 227 | if (!ctx) { |
no test coverage detected