summaryrefslogtreecommitdiff
path: root/SparkleLib/Git/SparkleFetcherGit.cs
blob: 79ad2a8430e021a1faf771c0e57c9a84983a0426 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//   SparkleShare, a collaboration and sharing tool.
//   Copyright (C) 2010  Hylke Bons <hylkebons@gmail.com>
//
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, see <http://www.gnu.org/licenses/>.


using System;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace SparkleLib {

    // Sets up a fetcher that can get remote folders
    public class SparkleFetcherGit : SparkleFetcherBase {

        private SparkleGit git;


        public SparkleFetcherGit (string server, string remote_folder, string target_folder) :
            base (server, remote_folder, target_folder)
        {
            remote_folder = remote_folder.Trim ("/".ToCharArray ());

            if (server.StartsWith ("http")) {
                base.target_folder = target_folder;
                base.remote_url    = server;
                return;
            }

            // Gitorious formatting
            if (server.Contains ("gitorious.org")) {
                server = "ssh://git@gitorious.org";

                if (!remote_folder.EndsWith (".git")) {

                    if (!remote_folder.Contains ("/"))
                        remote_folder = remote_folder + "/" + remote_folder;

                    remote_folder += ".git";
                }

            } else if (server.Contains ("github.com")) {
                server = "ssh://git@github.com";

            } else if (server.Contains ("gnome.org")) {
                server = "ssh://git@gnome.org/git";

            } else {
                server = server.TrimEnd ("/".ToCharArray ());

                string protocol = "ssh://";

                if (server.StartsWith ("ssh://"))
                    server   = server.Substring (6);

                if (server.StartsWith ("git://")) {
                    server = server.Substring (6);
                    protocol = "git://";
                }

                if (!server.Contains ("@"))
                    server = "git@" + server;

                server = protocol + server;
            }

            base.target_folder = target_folder;
            base.remote_url    = server + "/" + remote_folder;
        }


        public override bool Fetch ()
        {
            this.git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath,
                "clone " +
                "--progress " + // Redirects progress stats to standarderror
                "\"" + base.remote_url + "\" " + "\"" + base.target_folder + "\"");
            
            this.git.StartInfo.RedirectStandardError = true;
            this.git.Start ();
            
            double percentage = 1.0;
            Regex progress_regex = new Regex (@"([0-9]+)%", RegexOptions.Compiled);
            
            while (!this.git.StandardError.EndOfStream) {
                string line = this.git.StandardError.ReadLine ();
                Match match = progress_regex.Match (line);
                
                double number = 0.0;
                if (match.Success) {
                    number = double.Parse (match.Groups [1].Value);
                    
                    // The cloning progress consists of two stages: the "Compressing 
                    // objects" stage which we count as 20% of the total progress, and 
                    // the "Receiving objects" stage which we count as the last 80%
                    if (line.Contains ("|"))
                        // "Receiving objects" stage
                        number = (number / 100 * 75 + 20);    
                    else
                        // "Compressing objects" stage
                        number = (number / 100 * 20);
                }
                
                if (number >= percentage) {
                    percentage = number;
                    
                    // FIXME: for some reason it doesn't go above 95%
                    base.OnProgressChanged (percentage);
                }
                
                System.Threading.Thread.Sleep (100);        
            }
            
            this.git.WaitForExit ();

            SparkleHelpers.DebugInfo ("Git", "Exit code " + this.git.ExitCode.ToString ());

            if (this.git.ExitCode != 0) {
                return false;
            } else {
                InstallConfiguration ();
                InstallExcludeRules ();
                return true;
            }
        }


        public override string [] Warnings {
            get {
                SparkleGit git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath,
                    "config --global core.excludesfile");

                git.Start ();

                // Reading the standard output HAS to go before
                // WaitForExit, or it will hang forever on output > 4096 bytes
                string output = git.StandardOutput.ReadToEnd ().Trim ();
                git.WaitForExit ();

                if (string.IsNullOrEmpty (output)) {
                    return null;

                } else {
                    return new string [] {
                        string.Format ("You seem to have configured a system ‘gitignore’ file. " +
                                       "This may interfere with SparkleShare.\n({0})", output)
                    };
                }
            }
        }

        public override void Stop ()
        {
            if (this.git != null) {
                this.git.Kill ();
                this.git.Dispose ();
            }

            base.Stop ();
        }


        // Install the user's name and email and some config into
        // the newly cloned repository
        private void InstallConfiguration ()
        {
            string repo_config_file_path = SparkleHelpers.CombineMore (base.target_folder, ".git", "config");
            string config = String.Join (Environment.NewLine, File.ReadAllLines (repo_config_file_path));

            string n = Environment.NewLine;

            // Show special characters in the logs
            config = config.Replace ("[core]" + n,
                "[core]" + n + "quotepath = false" + n);

            // Be case sensitive explicitly to work on Mac
            config = config.Replace ("ignorecase = true", "ignorecase = false");

            // Ignore permission changes
            config = config.Replace ("filemode = true", "filemode = false");

            // Write the config to the file
            TextWriter writer = new StreamWriter (repo_config_file_path);
            writer.WriteLine (config);
            writer.Close ();

            SparkleHelpers.DebugInfo ("Config", "Added configuration to '" + repo_config_file_path + "'");
        }


        // Add a .gitignore file to the repo
        private void InstallExcludeRules ()
        {
            DirectoryInfo info = Directory.CreateDirectory (SparkleHelpers.CombineMore (
                this.target_folder, ".git", "info"));

            string exlude_rules_file_path = Path.Combine (info.FullName, "exclude");
            TextWriter writer = new StreamWriter (exlude_rules_file_path);

                // gedit and emacs
                writer.WriteLine ("*~");

                // Firefox and Chromium temporary download files
                writer.WriteLine ("*.part");
                writer.WriteLine ("*.crdownload");

                // vi(m)
                writer.WriteLine (".*.sw[a-z]");
                writer.WriteLine ("*.un~");
                writer.WriteLine ("*.swp");
                writer.WriteLine ("*.swo");
                
                // KDE
                writer.WriteLine (".directory");
    
                // Mac OSX
                writer.WriteLine (".DS_Store");
                writer.WriteLine ("Icon?");
                writer.WriteLine ("._*");
                writer.WriteLine (".Spotlight-V100");
                writer.WriteLine (".Trashes");

                // Mac OSX
                writer.WriteLine ("*(Autosaved).graffle");
            
                // Windows
                writer.WriteLine ("Thumbs.db");
                writer.WriteLine ("Desktop.ini");

                // MS Office
                writer.WriteLine ("~*.tmp");
                writer.WriteLine ("~*.TMP");
                writer.WriteLine ("*~*.tmp");
                writer.WriteLine ("*~*.TMP");
                writer.WriteLine ("~*.ppt");
                writer.WriteLine ("~*.pptx");
                writer.WriteLine ("~*.PPT");
                writer.WriteLine ("~*.PPTX");
                writer.WriteLine ("~*.xls");
                writer.WriteLine ("~*.xlsx");
                writer.WriteLine ("~*.XLS");
                writer.WriteLine ("~*.XLSX");
                writer.WriteLine ("~*.doc");
                writer.WriteLine ("~*.docx");
                writer.WriteLine ("~*.DOC");
                writer.WriteLine ("~*.DOCX");

                // CVS
                writer.WriteLine ("*/CVS/*");
                writer.WriteLine (".cvsignore");
                writer.WriteLine ("*/.cvsignore");
                
                // Subversion
                writer.WriteLine ("/.svn/*");
                writer.WriteLine ("*/.svn/*");

            writer.Close ();
        }
    }

    public class SparkleGit : Process {

        public SparkleGit (string path, string args) : base ()
        {
            EnableRaisingEvents              = true;
            StartInfo.FileName               = SparkleBackend.DefaultBackend.Path;
            StartInfo.Arguments              = args;
            StartInfo.RedirectStandardOutput = true;
            StartInfo.UseShellExecute        = false;
            StartInfo.WorkingDirectory       = path;
        }


        new public void Start ()
        {
            SparkleHelpers.DebugInfo ("Cmd", StartInfo.FileName + " " + StartInfo.Arguments);
            base.Start ();
        }
    }
}