GitX v0.4.1
[GitX.git] / gitx.mm
blob2d1d3faf74a168ccaf4dc937ade4d644fbbf737c
1 //
2 //  gitx.mm
3 //  GitX
4 //
5 //  Created by CiarĂ¡n Walsh on 15/08/2008.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBCLIProxy.h"
10 #import "PBGitBinary.h"
12 NSDistantObject* connect()
14         id proxy = [NSConnection rootProxyForConnectionWithRegisteredName:ConnectionName host:nil];
15         [proxy setProtocolForProxy:@protocol(GitXCliToolProtocol)];
16         return proxy;
19 void usage(char const *programName)
21         
22         printf("Usage: %s --help\n", programName);
23         printf("   or: %s (--commit|-h)\n", programName);
24         printf("   or: %s <revlist options>\n", programName);
25         printf("\n");
26         printf("\t-h, --help          print this help\n");
27         printf("\t--commit, -c        start GitX in commit mode\n");
28         printf("\n");
29         printf("RevList options\n");
30         printf("\tSee 'man git-log' and 'man git-rev-list' for options you can pass to gitx\n");
31         printf("\n");
32         printf("\t--all                  show all branches\n");
33         printf("\t<branch>               show specific branch\n");
34         printf("\t -- <path>             show commits touching paths\n");
35         exit(1);
38 int main(int argc, const char** argv)
40         if (argc >= 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
41                 usage(argv[0]);
43         if (![PBGitBinary path]) {
44                 printf("%s\n", [[PBGitBinary notFoundError] cStringUsingEncoding:NSUTF8StringEncoding]);
45                 exit(2);
46         }
48         // Attempt to connect to the app
49         id proxy = connect();
51         if (!proxy) {
52                 // If the connection failed, try to launch the app
53                 [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier: @"nl.frim.GitX"
54                                                                                                                          options: NSWorkspaceLaunchWithoutActivation
55                                                                           additionalEventParamDescriptor: nil
56                                                                                                         launchIdentifier: nil];
58                 // Now attempt to connect, allowing the app time to startup
59                 for (int attempt = 0; proxy == nil && attempt < 50; ++attempt){
60                         if (proxy = connect())
61                                 break;
62                         usleep(15000);
63                 }
64         }
65         if (!proxy) {
66                 fprintf(stderr, "Couldn't connect to app server!\n");
67                 exit(1);
68         }
70         if ([[[NSProcessInfo processInfo] environment] objectForKey:@"PWD"]) {
71                 int i;
72                 argv++; argc--;
73                 NSURL* url     = [NSURL fileURLWithPath:[[[NSProcessInfo processInfo] environment] objectForKey:@"PWD"]];
75                 NSMutableArray* arguments = [NSMutableArray arrayWithCapacity:argc];
76                 for (i = 0; i < argc; i++)
77                         [arguments addObject: [NSString stringWithCString:argv[i]]];
79                 NSError* error = nil;
80                 if (![proxy openRepository:url arguments: arguments error:&error]) {
81                         fprintf(stderr, "Error opening repository at %s", [[url path] UTF8String]);
82                         if (error) {
83                                 fprintf(stderr, ": %s", [[error localizedFailureReason] UTF8String]);
84                         }
85                         fprintf(stderr, "\n");
86                 }
87         }