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