Documentation/config.txt: Correct info about subsection name
[gitweb.git] / builtin-rev-parse.c
index c353a481b0c8590689c38b46525dc3217b15efbe..3b716fba133ae2f0375ef73d6a50a4d93112b5e4 100644 (file)
 #define DO_NONFLAGS    8
 static int filter = ~0;
 
-static char *def = NULL;
+static const char *def;
 
 #define NORMAL 0
 #define REVERSED 1
 static int show_type = NORMAL;
-static int symbolic = 0;
-static int abbrev = 0;
-static int output_sq = 0;
+static int symbolic;
+static int abbrev;
+static int output_sq;
 
-static int revs_count = 0;
+static int revs_count;
 
 /*
  * Some arguments are relevant "revision" arguments,
@@ -111,7 +111,7 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
 }
 
 /* Output a flag, only if filter allows it. */
-static int show_flag(char *arg)
+static int show_flag(const char *arg)
 {
        if (!(filter & DO_FLAGS))
                return 0;
@@ -124,7 +124,7 @@ static int show_flag(char *arg)
 
 static void show_default(void)
 {
-       char *s = def;
+       const char *s = def;
 
        if (s) {
                unsigned char sha1[20];
@@ -137,7 +137,7 @@ static void show_default(void)
        }
 }
 
-static int show_reference(const char *refname, const unsigned char *sha1)
+static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 {
        show_rev(NORMAL, sha1, refname);
        return 0;
@@ -164,17 +164,60 @@ static int show_file(const char *arg)
        return 0;
 }
 
-int cmd_rev_parse(int argc, const char **argv, char **envp)
+static int try_difference(const char *arg)
+{
+       char *dotdot;
+       unsigned char sha1[20];
+       unsigned char end[20];
+       const char *next;
+       const char *this;
+       int symmetric;
+
+       if (!(dotdot = strstr(arg, "..")))
+               return 0;
+       next = dotdot + 2;
+       this = arg;
+       symmetric = (*next == '.');
+
+       *dotdot = 0;
+       next += symmetric;
+
+       if (!*next)
+               next = "HEAD";
+       if (dotdot == arg)
+               this = "HEAD";
+       if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
+               show_rev(NORMAL, end, next);
+               show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
+               if (symmetric) {
+                       struct commit_list *exclude;
+                       struct commit *a, *b;
+                       a = lookup_commit_reference(sha1);
+                       b = lookup_commit_reference(end);
+                       exclude = get_merge_bases(a, b, 1);
+                       while (exclude) {
+                               struct commit_list *n = exclude->next;
+                               show_rev(REVERSED,
+                                        exclude->item->object.sha1,NULL);
+                               free(exclude);
+                               exclude = n;
+                       }
+               }
+               return 1;
+       }
+       *dotdot = '.';
+       return 0;
+}
+
+int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 {
        int i, as_is = 0, verify = 0;
        unsigned char sha1[20];
-       const char *prefix = setup_git_directory();
 
        git_config(git_default_config);
 
        for (i = 1; i < argc; i++) {
-               char *arg = argv[i];
-               char *dotdot;
+               const char *arg = argv[i];
 
                if (as_is) {
                        if (show_file(arg) && as_is < 2)
@@ -256,19 +299,19 @@ int cmd_rev_parse(int argc, const char **argv, char **envp)
                                continue;
                        }
                        if (!strcmp(arg, "--all")) {
-                               for_each_ref(show_reference);
+                               for_each_ref(show_reference, NULL);
                                continue;
                        }
                        if (!strcmp(arg, "--branches")) {
-                               for_each_branch_ref(show_reference);
+                               for_each_branch_ref(show_reference, NULL);
                                continue;
                        }
                        if (!strcmp(arg, "--tags")) {
-                               for_each_tag_ref(show_reference);
+                               for_each_tag_ref(show_reference, NULL);
                                continue;
                        }
                        if (!strcmp(arg, "--remotes")) {
-                               for_each_remote_ref(show_reference);
+                               for_each_remote_ref(show_reference, NULL);
                                continue;
                        }
                        if (!strcmp(arg, "--show-prefix")) {
@@ -326,23 +369,8 @@ int cmd_rev_parse(int argc, const char **argv, char **envp)
                }
 
                /* Not a flag argument */
-               dotdot = strstr(arg, "..");
-               if (dotdot) {
-                       unsigned char end[20];
-                       char *next = dotdot + 2;
-                       char *this = arg;
-                       *dotdot = 0;
-                       if (!*next)
-                               next = "HEAD";
-                       if (dotdot == arg)
-                               this = "HEAD";
-                       if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
-                               show_rev(NORMAL, end, next);
-                               show_rev(REVERSED, sha1, this);
-                               continue;
-                       }
-                       *dotdot = '.';
-               }
+               if (try_difference(arg))
+                       continue;
                if (!get_sha1(arg, sha1)) {
                        show_rev(NORMAL, sha1, arg);
                        continue;