File: git_change.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (189 lines) | stat: -rw-r--r-- 6,120 bytes parent folder | download | duplicates (4)
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
// Copyright (C) 2009 Derek Scherger <derek@echologic.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include "git_change.hh"
#include "parallel_iter.hh"
#include "roster.hh"

#include <stack>

using std::make_pair;
using std::map;
using std::stack;
using std::string;
using std::vector;

void
get_change(roster_t const & left, roster_t const & right,
           git_change & change)
{
  typedef attr_map_t::const_iterator attr_iterator;
  static attr_key exe_attr("mtn:execute");

  parallel::iter<node_map> i(left.all_nodes(), right.all_nodes());
  while (i.next())
    {
      MM(i);
      switch (i.state())
        {
        case parallel::invalid:
          I(false);

        case parallel::in_left:
          // deleted
          if (is_file_t(i.left_data()))
            {
              file_path path;
              left.get_name(i.left_key(), path);
              change.deletions.push_back(path);
            }
          break;

        case parallel::in_right:
          // added
          if (is_file_t(i.right_data()))
            {
              file_t file = downcast_to_file_t(i.right_data());

              attr_iterator exe = file->attrs.find(exe_attr);

              string mode = "100644";
              if (exe != file->attrs.end() &&
                  exe->second.first && // live attr
                  exe->second.second() == "true")
                mode = "100755";

              file_path path;
              right.get_name(i.right_key(), path);
              change.additions.push_back(git_add(path,
                                                 file->content,
                                                 mode));
            }
          break;

        case parallel::in_both:
          // moved/renamed/patched/attribute changes
          if (is_file_t(i.left_data()))
            {
              file_t left_file = downcast_to_file_t(i.left_data());
              file_t right_file = downcast_to_file_t(i.right_data());

              attr_iterator left_attr = left_file->attrs.find(exe_attr);
              attr_iterator right_attr = right_file->attrs.find(exe_attr);

              string left_mode = "100644";
              string right_mode = "100644";

              if (left_attr != left_file->attrs.end() &&
                  left_attr->second.first && // live attr
                  left_attr->second.second() == "true")
                left_mode = "100755";

              if (right_attr != right_file->attrs.end() &&
                  right_attr->second.first && // live attr
                  right_attr->second.second() == "true")
                right_mode = "100755";

              file_path left_path, right_path;
              left.get_name(i.left_key(), left_path);
              right.get_name(i.right_key(), right_path);

              if (left_path != right_path)
                change.renames.push_back(make_pair(left_path,
                                                   right_path));

              // git handles content changes as additions
              if (left_file->content != right_file->content ||
                  left_mode != right_mode)
                change.additions.push_back(git_add(right_path,
                                                   right_file->content,
                                                   right_mode));
            }
          break;
        }
    }
}

// re-order renames so that they occur in the correct order
// i.e. rename a->b + rename b->c will be re-ordered as
//      rename b->c + rename a->b
// this will also insert temporary names to resolve circular
// renames and name swaps:
// i.e. rename a->b + rename b->a will be re-ordered as
//      rename a->tmp + rename b->a + rename tmp->b
void
reorder_renames(vector<git_rename> const & renames,
                vector<git_rename> & reordered_renames)
{
  typedef map<file_path, file_path> map_type;

  map_type rename_map;

  for (rename_iterator i = renames.begin(); i != renames.end(); ++i)
    rename_map.insert(*i);

  while (!rename_map.empty())
    {
      map_type::iterator i = rename_map.begin();
      I(i != rename_map.end());
      git_rename base(*i);
      rename_map.erase(i);

      map_type::iterator next = rename_map.find(base.second);
      stack<git_rename> rename_stack;

      // stack renames so their order can be reversed
      while (next != rename_map.end())
        {
          git_rename rename(*next);
          rename_stack.push(rename);
          rename_map.erase(next);
          next = rename_map.find(rename.second);
        }

      // break rename loops
      if (!rename_stack.empty())
        {
          git_rename const & top = rename_stack.top();
          // if there is a loop push another rename onto the stack that
          // renames the old base to a temporary and adjust the base
          // rename to account for this
          if (base.first == top.second)
            {
              // the temporary path introduced here is pretty weak in
              // terms of random filenames but should suffice for the
              // already rare situations where any of this is required.
              string path = top.second.as_internal();
              path += ".tmp.break-rename-loop";
              file_path tmp = file_path_internal(path);
              rename_stack.push(git_rename(base.first, tmp));
              base.first = tmp;
            }
        }

      // insert the stacked renames in reverse order
      while (!rename_stack.empty())
        {
          git_rename rename = rename_stack.top();
          rename_stack.pop();
          reordered_renames.push_back(rename);
        }

      reordered_renames.push_back(base);
    }
}

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: