git: You asked me to pull without telling me which branch …

July 1, 2009 – 8:17 pm

Received this error when trying to pull from a remote origin:

You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me either.

To fix, I added this to .git/config:

[branch "master"]
    remote = origin
    merge = refs/heads/org.openembedded.dev 

Tags:

git: failed to push some refs

June 26, 2009 – 1:23 pm

I’m really digging git, but its error messages are less than helpful at times.

git push origin master
error: failed to push some refs to origin

Most likely, there are changes in the remote repo that you need to pull first:

git pull origin master

Resolve any conflicts, then you can push to the remote git repo.

I’m not sure why “failed to push some refs” couldn’t include “(do you need to pull?)”


Tags:

Remove ERB files after upgrading to haml

June 10, 2009 – 7:43 pm

I converted an application to haml and wanted to get rid of my previous ERb templates.

find ./app/views -name "*.html.erb" -print0 | xargs -0 rm

Explanation

  • find() ./app/views -name "*.html.erb" returns a list of all ERb view templates.
  • -print0 formats the output specially.
  • xargs is a program that takes a list and runs a command for each item in that list.
  • -0 means the input format is specially formatted by -print0
  • rm is the command we want to run for each found file

So, each found file will get deleted. Bask in your newfound haml-ness.


Clearing a stuck POP mailbox with Ruby Net::Telnet

May 18, 2009 – 11:12 pm

A client’s mailbox was full. Outlook tried to download all messages before deleting them, and I don’t know a setting to make it retrieve/delete a set number.

So, knowing that you can telnet to a POP server, I wrote up a little ruby script to delete the first 1000 messages without bothering to retrieve them.

require 'net/telnet'

pop = Net::Telnet::new("Host" => "pop.server.com",
  "Port" => 110,
  "Telnetmode" => false,
  "Timeout"    => false,
  "Prompt" => /^\+OK/n)
pop.cmd("user " + "name@domain.com") { |c| print c }
pop.cmd("pass " + "P@ssword") { |c| print c }
pop.cmd("stat") { |c| print c }
1.upto(1000) do |i|
  pop.cmd("dele #{i}")
  puts i if 0 == i % 100
end
pop.cmd("stat") { |c| print c }

pop.cmd("quit") { |c| print c }

Here’s the output:

+OK hello from popgate 2.43 on pop.server.com
+OK password required.
+OK maildrop ready, 50139 messages (1474122084 octets) (1477758478)
100
200
300
400
500
600
700
800
900
1000
+OK message 1000 marked deleted
+OK 49139 1462508238

I put the STAT lines in there so I could see how many messages were there at the start and finish of the script running.

The Ruby POP3 library has a delete_all(), but the last day or so of mail hadn’t been retrieved at all, so I couldn’t just nuke the mailbox and start over. I only needed to clear some space so that I could connect normally (and set the client to delete-on-retrieve).  You could mimic that by parsing the STAT response for the number of messages. I leave that as an exercise to the reader.

I made some more tweaks after writing this post and added support for growl:

system "growlnotify -m '#{i} of 1000 deleted'
 -d popdelete" if 0 == i % 25

That way I could just watch the growl window in the corner to see how it was doing. The -d flag groups the notifications so that I didn’t get a separate notification window for each message; the new message updates the previous notification window.

When I was searching for a solution before writing my own, I looked for “pop delete all” and “pop clear” and “pop commands” and “pop command delete all”.  Hope that helps someone else find this article.


Rails render partial counters: displaying a record’s index in a collection

April 9, 2009 – 3:34 pm

I always forget that you can reference the index of an item in a collection when you’re using a partial:

<%= render :partial => 'item', :o bject => @items %>
# in _item.html.erb
<%= item_counter %>

This is zero-based, so you might want to +1 if you want the count to start at 1.

It’s an easy way to number lines in a partial, which I find I do fairly often and I can never remember the syntax for getting that index counter!


Upgrading to Rails 2.3 with git

March 30, 2009 – 1:19 am

I upgraded an application to Rails 2.3, starting with making a new empty branch and using that merge back to a staging branch, which is very well described by Olly at the Bamboo Blog.

I had a few issues, which I thought I would collect here in case anyone else has them.

  • A merge conflict means that ‘git status’ will show the file as unmerged, if you need to find which files failed the merge.
  • This nuked my copy of restful_authentication that I’d cloned into vendor/plugins which I downloaded from the tarball instead.
  • I hadn’t renamed application.rb to application_controller.rb, so I didn’t get a merge conflict but I also didn’t have any of my original code in the new application_controller.rb that was auto-generated. My symptom of this was “current_user” didn’t work because restful_authentication wasn’t being loaded.

All in all, there were a lot of changes to various Rails files that I was glad I caught using this method.


Updating textarea with Ajax: unterminated string literal

March 18, 2009 – 1:04 pm

If you try to update a textarea with newlines in it via Ajax, you may get an “Unterminated string literal” error.  The update coming back may look like this:

$('textarea_to_update').value='line one
line two
line three';

This will fail because the javascript will interpret the newline as the end of the command.

So, replace those newlines with the newline character and the textarea will get updated correctly:

textarea_value.gsub!(/\n/, '\n')

Visualizing git history

March 10, 2009 – 2:33 pm

I’ve been using git for version control on a new project. Instead of needing a connection to the server (as CVS and SVN do), changes are stored locally.

Every once in a while, I want to poke back the history. git ships with a graphical repository viewer, but I like gitx better.

git makes branching off to try something super easy, which is quickly becoming one of my favorite features. I can create a branch quickly, try something out, switch away and come back later if I want.

Using gitx to view my repository history, I can quickly find where branches diverged and it’s just kinda neat to look at.


TemplateError: wrong number of arguments in default_url_options()

January 30, 2009 – 11:18 am

During an app upgrade to Rails 2.2.2, I came across this error that baffled me for a few minutes:

ActionView::TemplateError
    (wrong number of arguments (0 for 1)) on line #34
    of app/views/account/_dashboard_header.rhtml:

34: <%= link_to 'My Dashboard', my_dashboard_url >

Checking out the backtrace:

(eval):3:in `default_url_options'
(eval):3:in `my_dashboard_url'
app/views/account/_dashboard_header.rhtml:34

Confusing. Why would this named route suddenly stop working? I did a search for default_url_options and found that I had overridden that function:

if production_mode?
  def default_url_options()
    { :protocol => 'https://' }
  end
end

Aha! Back in the day, default_url_options didn’t have any default options, so I wasn’t bothering to merge my desired options with the options parameter. I fixed it with a reverse_merge so any passed-in options would still exist.


XP password reset

January 23, 2009 – 10:29 am

Someone asked me to fix their computer, but they didn’t know any of the passwords for it.  Short of formatting it, I didn’t really know what to do.

After some searching, I found the Offline NT Password & Registry Editor, Bootdisk / CD which worked perfectly.

  • Download the CD image
  • Burn to a CD
  • Boot to the CD
  • A small linux program runs
  • Answer a few questions about what partition and the user(s) you need to clear and/or reset the password for
  • Write your changes to the disk

You can also edit the registry if you need to hack some bad keys out of there, like spyware startup entries.

Super handy tool!