Rev 3: Get it working for a single windows-like path in http://bazaar.launchpad.net/~jameinel/my_nick

John Arbash Meinel john at arbash-meinel.com
Thu May 20 19:14:18 BST 2010


At http://bazaar.launchpad.net/~jameinel/my_nick

------------------------------------------------------------
revno: 3
revision-id: john at arbash-meinel.com-20100520181406-ytz3511wx8hz8soy
parent: john at arbash-meinel.com-20100520064722-chcm3m2m63qg9zh3
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: my_nick
timestamp: Thu 2010-05-20 13:14:06 -0500
message:
  Get it working for a single windows-like path
-------------- next part --------------
=== modified file 'my_nick.cpp'
--- a/my_nick.cpp	2010-05-20 06:47:22 +0000
+++ b/my_nick.cpp	2010-05-20 18:14:06 +0000
@@ -9,12 +9,26 @@
 using namespace std;
 
 
+size_t
+get_last_slash(string const path, size_t n=string::npos)
+{
+    size_t last_for_slash, last_back_slash;
+    last_for_slash = path.rfind('/', n);
+    last_back_slash = path.rfind('\\', n);
+    if ((last_back_slash != string::npos && last_back_slash > last_for_slash)
+        || last_for_slash == string::npos) {
+        return last_back_slash;
+    }
+    return last_for_slash;
+}
+
 string
 dirname(string const path)
 {
-    size_t last_slash = path.rfind('/');
+    size_t last_slash = get_last_slash(path);
+    // cerr << "dirname(" << path << ") => " << last_slash << endl;
     if (last_slash == path.size() - 1) {
-        last_slash = path.rfind('/', last_slash-1);
+        last_slash = get_last_slash(path, last_slash-1);
     }
     if (last_slash == string::npos) {
         return string("");
@@ -101,6 +115,28 @@
 }
 
 /**
+ * Convert a given path to a short name.
+ */
+string
+path_to_short(string const path)
+{
+    string parent_dir = dirname(path);
+    // cerr << "path: '" << path << "' parent_dir: " << parent_dir << endl;
+    size_t last = path.size();
+    if (path[last-1] == '/' || path[last-1] == '\\') {
+        last--;
+    }
+    if (!is_repository(path)) {
+        // Not a branch+repo, so get the containing repo
+        // cerr << "parent_dir: " << parent_dir << endl;
+        parent_dir = find_containing_bzrdir(parent_dir);
+        // cerr << "parent_dir: " << parent_dir << endl;
+    }
+    // This should be the containing repository
+    return path.substr(parent_dir.size()+1, last - parent_dir.size() - 1);
+}
+
+/**
  * Given a location_url, try to find its short name.
  */
 string
@@ -131,50 +167,35 @@
         return pre + location_url.substr(last_slash+1, end-last_slash-1);
     }
     string path = file_url_to_path(location_url);
-    string parent_dir = dirname(path);
-    size_t last = path.size();
-    if (path[last-1] == '/') {
-        last--;
-    }
-    if (!is_repository(path)) {
-        // Not a branch+repo, so get the containing repo
-        parent_dir = find_containing_bzrdir(parent_dir);
-    }
-    // This should be the containing repository
-    return path.substr(parent_dir.size()+1, last - parent_dir.size() - 1);
+    return path_to_short(path);
 }
 
 int
 main(int argc, char **argv)
 {
-    for (int i = 1; i < argc; ++i) {
-        string path(argv[i]);
-        cout << "Path: " << path;
-        if (is_bzrdir(path)) {
-            cout << " has a bzrdir and";
-            if (is_branch(path)) {
-                cout << " is a branch";
-                string location;
-                location = is_checkout_of(path);
-                if (!location.empty()) {
-                    cout << " which is a checkout of "
-                        << checkout_to_short(location);
-                }
-            } else {
-                cout << " is not a branch";
-            }
-            if (is_repository(path)) {
-                cout << " and is a repository";
-            }
-        } else {
-            cout << " has no bzrdir";
-            string containing = find_containing_bzrdir(path);
-            if (!containing.empty()) {
-                cout << " but has containing bzrdir: "
-                    << find_containing_bzrdir(path);
-            }
-        }
-        cout << endl;
-    }
+    if (argc != 2) {
+        cout << "<invalid request>" << endl;
+        return 2;
+    }
+    string path(argv[1]);
+    path = find_containing_bzrdir(path);
+    if (path.empty()) {
+        return 0;
+    }
+    if (!is_branch(path)) {
+        return 0;
+    }
+    string checkout_location = is_checkout_of(path);
+    string short_name;
+    if (checkout_location.empty()) {
+        // cerr << "path to short" << endl;
+        short_name = path_to_short(path);
+    } else {
+        short_name = checkout_to_short(checkout_location);
+    }
+    if (short_name.empty()) {
+        return 1;
+    }
+    cout << short_name << endl;
     return 0;
 }



More information about the bazaar-commits mailing list