~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Storage.File/RefDirectoryUpdate.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
5
This program and the accompanying materials are made available
 
6
under the terms of the Eclipse Distribution License v1.0 which
 
7
accompanies this distribution, is reproduced below, and is
 
8
available at http://www.eclipse.org/org/documents/edl-v10.php
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
19
- Redistributions in binary form must reproduce the above
 
20
  copyright notice, this list of conditions and the following
 
21
  disclaimer in the documentation and/or other materials provided
 
22
  with the distribution.
 
23
 
 
24
- Neither the name of the Eclipse Foundation, Inc. nor the
 
25
  names of its contributors may be used to endorse or promote
 
26
  products derived from this software without specific prior
 
27
  written permission.
 
28
 
 
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
42
*/
 
43
 
 
44
using NGit;
 
45
using NGit.Storage.File;
 
46
using Sharpen;
 
47
 
 
48
namespace NGit.Storage.File
 
49
{
 
50
        /// <summary>
 
51
        /// Updates any reference stored by
 
52
        /// <see cref="RefDirectory">RefDirectory</see>
 
53
        /// .
 
54
        /// </summary>
 
55
        internal class RefDirectoryUpdate : RefUpdate
 
56
        {
 
57
                private readonly RefDirectory database;
 
58
 
 
59
                private LockFile Lock;
 
60
 
 
61
                internal RefDirectoryUpdate(RefDirectory r, Ref @ref) : base(@ref)
 
62
                {
 
63
                        database = r;
 
64
                }
 
65
 
 
66
                protected internal override RefDatabase GetRefDatabase()
 
67
                {
 
68
                        return database;
 
69
                }
 
70
 
 
71
                protected internal override Repository GetRepository()
 
72
                {
 
73
                        return database.GetRepository();
 
74
                }
 
75
 
 
76
                /// <exception cref="System.IO.IOException"></exception>
 
77
                protected internal override bool TryLock(bool deref)
 
78
                {
 
79
                        Ref dst = GetRef();
 
80
                        if (deref)
 
81
                        {
 
82
                                dst = dst.GetLeaf();
 
83
                        }
 
84
                        string name = dst.GetName();
 
85
                        Lock = new LockFile(database.FileFor(name), GetRepository().FileSystem);
 
86
                        if (Lock.Lock())
 
87
                        {
 
88
                                dst = database.GetRef(name);
 
89
                                SetOldObjectId(dst != null ? dst.GetObjectId() : null);
 
90
                                return true;
 
91
                        }
 
92
                        else
 
93
                        {
 
94
                                return false;
 
95
                        }
 
96
                }
 
97
 
 
98
                protected internal override void Unlock()
 
99
                {
 
100
                        if (Lock != null)
 
101
                        {
 
102
                                Lock.Unlock();
 
103
                                Lock = null;
 
104
                        }
 
105
                }
 
106
 
 
107
                /// <exception cref="System.IO.IOException"></exception>
 
108
                protected internal override RefUpdate.Result DoUpdate(RefUpdate.Result status)
 
109
                {
 
110
                        WriteConfig wc = database.GetRepository().GetConfig().Get(WriteConfig.KEY);
 
111
                        Lock.SetFSync(wc.GetFSyncRefFiles());
 
112
                        Lock.SetNeedStatInformation(true);
 
113
                        Lock.Write(GetNewObjectId());
 
114
                        string msg = GetRefLogMessage();
 
115
                        if (msg != null)
 
116
                        {
 
117
                                if (IsRefLogIncludingResult())
 
118
                                {
 
119
                                        string strResult = ToResultString(status);
 
120
                                        if (strResult != null)
 
121
                                        {
 
122
                                                if (msg.Length > 0)
 
123
                                                {
 
124
                                                        msg = msg + ": " + strResult;
 
125
                                                }
 
126
                                                else
 
127
                                                {
 
128
                                                        msg = strResult;
 
129
                                                }
 
130
                                        }
 
131
                                }
 
132
                                database.Log(this, msg, true);
 
133
                        }
 
134
                        if (!Lock.Commit())
 
135
                        {
 
136
                                return RefUpdate.Result.LOCK_FAILURE;
 
137
                        }
 
138
                        database.Stored(this, Lock.GetCommitLastModified());
 
139
                        return status;
 
140
                }
 
141
 
 
142
                private string ToResultString(RefUpdate.Result status)
 
143
                {
 
144
                        switch (status)
 
145
                        {
 
146
                                case RefUpdate.Result.FORCED:
 
147
                                {
 
148
                                        return "forced-update";
 
149
                                }
 
150
 
 
151
                                case RefUpdate.Result.FAST_FORWARD:
 
152
                                {
 
153
                                        return "fast forward";
 
154
                                }
 
155
 
 
156
                                case RefUpdate.Result.NEW:
 
157
                                {
 
158
                                        return "created";
 
159
                                }
 
160
 
 
161
                                default:
 
162
                                {
 
163
                                        return null;
 
164
                                        break;
 
165
                                }
 
166
                        }
 
167
                }
 
168
 
 
169
                /// <exception cref="System.IO.IOException"></exception>
 
170
                protected internal override RefUpdate.Result DoDelete(RefUpdate.Result status)
 
171
                {
 
172
                        if (GetRef().GetLeaf().GetStorage() != RefStorage.NEW)
 
173
                        {
 
174
                                database.Delete(this);
 
175
                        }
 
176
                        return status;
 
177
                }
 
178
 
 
179
                /// <exception cref="System.IO.IOException"></exception>
 
180
                protected internal override RefUpdate.Result DoLink(string target)
 
181
                {
 
182
                        WriteConfig wc = database.GetRepository().GetConfig().Get(WriteConfig.KEY);
 
183
                        Lock.SetFSync(wc.GetFSyncRefFiles());
 
184
                        Lock.SetNeedStatInformation(true);
 
185
                        Lock.Write(Constants.Encode(RefDirectory.SYMREF + target + '\n'));
 
186
                        string msg = GetRefLogMessage();
 
187
                        if (msg != null)
 
188
                        {
 
189
                                database.Log(this, msg, false);
 
190
                        }
 
191
                        if (!Lock.Commit())
 
192
                        {
 
193
                                return RefUpdate.Result.LOCK_FAILURE;
 
194
                        }
 
195
                        database.StoredSymbolicRef(this, Lock.GetCommitLastModified(), target);
 
196
                        if (GetRef().GetStorage() == RefStorage.NEW)
 
197
                        {
 
198
                                return RefUpdate.Result.NEW;
 
199
                        }
 
200
                        return RefUpdate.Result.FORCED;
 
201
                }
 
202
        }
 
203
}