Greg Ward avatar Greg Ward committed 15ffdd8

git: simplify git_get_info() by de-nesting a big "else" block

Comments (0)

Files changed (2)

         debug("unable to read .git/HEAD: assuming not a git repo");
         return NULL;
     }
-    else {
-        char *prefix = "ref: refs/heads/";
-        int prefixlen = strlen(prefix);
 
-        if (context->options->show_branch || context->options->show_revision) {
-            int found_branch = 0;
-            if (strncmp(prefix, buf, prefixlen) == 0) {
-                /* yep, we're on a known branch */
-                debug("read a head ref from .git/HEAD: '%s'", buf);
-                if (result_set_branch(result, buf + prefixlen))
-                    found_branch = 1;
-            }
-            else {
-                /* if it's not a branch name, assume it is a commit ID */
-                debug(".git/HEAD doesn't look like a head ref: unknown branch");
-                result_set_branch(result, "(unknown)");
+    char *prefix = "ref: refs/heads/";
+    int prefixlen = strlen(prefix);
+
+    if (context->options->show_branch || context->options->show_revision) {
+        int found_branch = 0;
+        if (strncmp(prefix, buf, prefixlen) == 0) {
+            /* yep, we're on a known branch */
+            debug("read a head ref from .git/HEAD: '%s'", buf);
+            if (result_set_branch(result, buf + prefixlen))
+                found_branch = 1;
+        }
+        else {
+            /* if it's not a branch name, assume it is a commit ID */
+            debug(".git/HEAD doesn't look like a head ref: unknown branch");
+            result_set_branch(result, "(unknown)");
+            result_set_revision(result, buf, 12);
+        }
+        if (context->options->show_revision && found_branch) {
+            char buf[1024];
+            char filename[1024] = ".git/refs/heads/";
+            int nchars = sizeof(filename) - strlen(filename) - 1;
+            strncat(filename, result->branch, nchars);
+            if (read_first_line(filename, buf, 1024)) {
                 result_set_revision(result, buf, 12);
             }
-            if (context->options->show_revision && found_branch) {
-                char buf[1024];
-                char filename[1024] = ".git/refs/heads/";
-                int nchars = sizeof(filename) - strlen(filename) - 1;
-                strncat(filename, result->branch, nchars);
-                if (read_first_line(filename, buf, 1024)) {
-                    result_set_revision(result, buf, 12);
-                }
-            }
         }
-        if (context->options->show_modified) {
-            char *argv[] = {
-                "git", "diff", "--no-ext-diff", "--quiet", "--exit-code", NULL};
-            capture_t *capture = capture_child("git", argv);
-            result->modified = (capture->status == 1);
+    }
+    if (context->options->show_modified) {
+        char *argv[] = {
+            "git", "diff", "--no-ext-diff", "--quiet", "--exit-code", NULL};
+        capture_t *capture = capture_child("git", argv);
+        result->modified = (capture->status == 1);
 
-            /* any other outcome (including failure to fork/exec,
-               failure to run git, or diff error): assume no
-               modifications */
-            free_capture(capture);
-        }
-        if (context->options->show_unknown) {
-            char *argv[] = {
-                "git", "ls-files", "--others", "--exclude-standard", NULL};
-            capture_t *capture = capture_child("git", argv);
-            result->unknown = (capture != NULL && capture->stdout.len > 0);
+        /* any other outcome (including failure to fork/exec,
+           failure to run git, or diff error): assume no
+           modifications */
+        free_capture(capture);
+    }
+    if (context->options->show_unknown) {
+        char *argv[] = {
+            "git", "ls-files", "--others", "--exclude-standard", NULL};
+        capture_t *capture = capture_child("git", argv);
+        result->unknown = (capture != NULL && capture->stdout.len > 0);
 
-            /* again, ignore other errors and assume no unknown files */
-            free_capture(capture);
-        }
+        /* again, ignore other errors and assume no unknown files */
+        free_capture(capture);
     }
 
     return result;
     status=$?
 
     if [ $status -ne 0 ]; then
-        echo "fail: child process terminated with exit status $status; command was:" >&2
+        echo "fail: $message: child process terminated with exit status $status; command was:" >&2
         eval echo $cmd >&2
         failed="y"
         return $status
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.