forked from pieter/gitx
/
gitx.mm
146 lines (122 loc) · 4.32 KB
/
gitx.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// gitx.mm
// GitX
//
// Created by Ciarán Walsh on 15/08/2008.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBCLIProxy.h"
#import "PBGitBinary.h"
#import "PBEasyPipe.h"
NSDistantObject* connect()
{
id proxy = [NSConnection rootProxyForConnectionWithRegisteredName:ConnectionName host:nil];
[proxy setProtocolForProxy:@protocol(GitXCliToolProtocol)];
return proxy;
}
NSDistantObject *createProxy()
{
NSDistantObject *proxy = connect();
if (proxy)
return proxy;
// The connection failed, so try to launch the app
[[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier: @"nl.frim.GitX"
options: NSWorkspaceLaunchWithoutActivation
additionalEventParamDescriptor: nil
launchIdentifier: nil];
// Now attempt to connect, allowing the app time to startup
for (int attempt = 0; proxy == nil && attempt < 50; ++attempt) {
if (proxy = connect())
return proxy;
usleep(15000);
}
// not succesful!
fprintf(stderr, "Couldn't connect to app server!\n");
exit(1);
return nil;
}
void usage(char const *programName)
{
printf("Usage: %s (--help|--version)\n", programName);
printf(" or: %s (--commit|-h)\n", programName);
printf(" or: %s <revlist options>\n", programName);
printf("\n");
printf("\t-h, --help print this help\n");
printf("\t--commit, -c start GitX in commit mode\n");
printf("\n");
printf("RevList options\n");
printf("\tSee 'man git-log' and 'man git-rev-list' for options you can pass to gitx\n");
printf("\n");
printf("\t--all show all branches\n");
printf("\t<branch> show specific branch\n");
printf("\t -- <path> show commits touching paths\n");
exit(1);
}
void version_info()
{
NSString *version = [[[NSBundle bundleForClass:[PBGitBinary class]] infoDictionary] valueForKey:@"CFBundleVersion"];
printf("This is GitX version %s\n", [version UTF8String]);
if ([PBGitBinary path])
printf("Using git found at %s, version %s\n", [[PBGitBinary path] UTF8String], [[PBGitBinary version] UTF8String]);
else
printf("GitX cannot find a git binary\n");
exit(1);
}
void handleSTDINDiff(id<GitXCliToolProtocol> proxy)
{
NSFileHandle *handle = [NSFileHandle fileHandleWithStandardInput];
NSData *data = [handle readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (string && [string length] > 0) {
[proxy openDiffWindowWithDiff:string];
exit(0);
}
}
void handleDiffWithArguments(NSArray *arguments, NSString *directory, id<GitXCliToolProtocol> proxy)
{
int ret;
arguments = [[NSArray arrayWithObject:@"diff"] arrayByAddingObjectsFromArray:arguments];
NSString *diff = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:directory retValue:&ret];
if (ret) {
printf("Invalid diff command\n");
exit(3);
}
[proxy openDiffWindowWithDiff:diff];
exit(0);
}
int main(int argc, const char** argv)
{
if (argc >= 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
usage(argv[0]);
if (argc >= 2 && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-v")))
version_info();
if (![PBGitBinary path]) {
printf("%s\n", [[PBGitBinary notFoundError] cStringUsingEncoding:NSUTF8StringEncoding]);
exit(2);
}
// Attempt to connect to the app
id proxy = createProxy();
// Create arguments
argv++; argc--;
NSMutableArray* arguments = [NSMutableArray arrayWithCapacity:argc];
int i = 0;
for (i = 0; i < argc; i++)
[arguments addObject: [NSString stringWithCString:argv[i]]];
if (!isatty(STDIN_FILENO) && fdopen(STDIN_FILENO, "r"))
handleSTDINDiff(proxy);
// From this point, we require a working directory
NSString *pwd = [[[NSProcessInfo processInfo] environment] objectForKey:@"PWD"];
if (!pwd)
exit(2);
if ([arguments count] > 0 && ([[arguments objectAtIndex:0] isEqualToString:@"--diff"] ||
[[arguments objectAtIndex:0] isEqualToString:@"-d"]))
handleDiffWithArguments([arguments subarrayWithRange:NSMakeRange(1, [arguments count] - 1)], pwd, proxy);
// No diff, just open the current dir
NSURL* url = [NSURL fileURLWithPath:pwd];
NSError* error = nil;
if (![proxy openRepository:url arguments: arguments error:&error]) {
fprintf(stderr, "Error opening repository at %s\n", [[url path] UTF8String]);
if (error)
fprintf(stderr, "\t%s\n", [[error localizedFailureReason] UTF8String]);
}
}